ptrlen.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Functions to deal with ptrlens.
  3. */
  4. #include "defs.h"
  5. #include "misc.h"
  6. bool ptrlen_eq_string(ptrlen pl, const char *str)
  7. {
  8. size_t len = strlen(str);
  9. return (pl.len == len && !memcmp(pl.ptr, str, len));
  10. }
  11. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
  12. {
  13. return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
  14. }
  15. int ptrlen_strcmp(ptrlen pl1, ptrlen pl2)
  16. {
  17. size_t minlen = pl1.len < pl2.len ? pl1.len : pl2.len;
  18. if (minlen) { /* tolerate plX.ptr==NULL as long as plX.len==0 */
  19. int cmp = memcmp(pl1.ptr, pl2.ptr, minlen);
  20. if (cmp)
  21. return cmp;
  22. }
  23. return pl1.len < pl2.len ? -1 : pl1.len > pl2.len ? +1 : 0;
  24. }
  25. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
  26. {
  27. if (whole.len >= prefix.len &&
  28. !memcmp(whole.ptr, prefix.ptr, prefix.len)) {
  29. if (tail) {
  30. tail->ptr = (const char *)whole.ptr + prefix.len;
  31. tail->len = whole.len - prefix.len;
  32. }
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
  38. {
  39. if (whole.len >= suffix.len &&
  40. !memcmp((char *)whole.ptr + (whole.len - suffix.len),
  41. suffix.ptr, suffix.len)) {
  42. if (tail) {
  43. tail->ptr = whole.ptr;
  44. tail->len = whole.len - suffix.len;
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. bool ptrlen_contains(ptrlen input, const char *characters)
  51. {
  52. for (const char *p = input.ptr, *end = p + input.len; p < end; p++)
  53. if (strchr(characters, *p))
  54. return true;
  55. return false;
  56. }
  57. bool ptrlen_contains_only(ptrlen input, const char *characters)
  58. {
  59. for (const char *p = input.ptr, *end = p + input.len; p < end; p++)
  60. if (!strchr(characters, *p))
  61. return false;
  62. return true;
  63. }
  64. ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
  65. {
  66. const char *p = input->ptr, *end = p + input->len;
  67. ptrlen toret;
  68. while (p < end && strchr(separators, *p))
  69. p++;
  70. toret.ptr = p;
  71. while (p < end && !strchr(separators, *p))
  72. p++;
  73. toret.len = p - (const char *)toret.ptr;
  74. size_t to_consume = p - (const char *)input->ptr;
  75. assert(to_consume <= input->len);
  76. input->ptr = (const char *)input->ptr + to_consume;
  77. input->len -= to_consume;
  78. return toret;
  79. }
  80. char *mkstr(ptrlen pl)
  81. {
  82. char *p = snewn(pl.len + 1, char);
  83. memcpy(p, pl.ptr, pl.len);
  84. p[pl.len] = '\0';
  85. return p;
  86. }
  87. bool strstartswith(const char *s, const char *t)
  88. {
  89. return !strncmp(s, t, strlen(t));
  90. }
  91. bool strendswith(const char *s, const char *t)
  92. {
  93. size_t slen = strlen(s), tlen = strlen(t);
  94. return slen >= tlen && !strcmp(s + (slen - tlen), t);
  95. }