str.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_STR_H
  3. #define CCAN_STR_H
  4. #include "config.h"
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <ctype.h>
  8. /**
  9. * streq - Are two strings equal?
  10. * @a: first string
  11. * @b: first string
  12. *
  13. * This macro is arguably more readable than "!strcmp(a, b)".
  14. *
  15. * Example:
  16. * if (streq(somestring, ""))
  17. * printf("String is empty!\n");
  18. */
  19. #define streq(a,b) (strcmp((a),(b)) == 0)
  20. /**
  21. * strstarts - Does this string start with this prefix?
  22. * @str: string to test
  23. * @prefix: prefix to look for at start of str
  24. *
  25. * Example:
  26. * if (strstarts(somestring, "foo"))
  27. * printf("String %s begins with 'foo'!\n", somestring);
  28. */
  29. #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
  30. /**
  31. * strends - Does this string end with this postfix?
  32. * @str: string to test
  33. * @postfix: postfix to look for at end of str
  34. *
  35. * Example:
  36. * if (strends(somestring, "foo"))
  37. * printf("String %s end with 'foo'!\n", somestring);
  38. */
  39. static inline bool strends(const char *str, const char *postfix)
  40. {
  41. if (strlen(str) < strlen(postfix))
  42. return false;
  43. return streq(str + strlen(str) - strlen(postfix), postfix);
  44. }
  45. /**
  46. * stringify - Turn expression into a string literal
  47. * @expr: any C expression
  48. *
  49. * Example:
  50. * #define PRINT_COND_IF_FALSE(cond) \
  51. * ((cond) || printf("%s is false!", stringify(cond)))
  52. */
  53. #define stringify(expr) stringify_1(expr)
  54. /* Double-indirection required to stringify expansions */
  55. #define stringify_1(expr) #expr
  56. /**
  57. * strcount - Count number of (non-overlapping) occurrences of a substring.
  58. * @haystack: a C string
  59. * @needle: a substring
  60. *
  61. * Example:
  62. * int i;
  63. * i = strcount("aaa aaa", "a"); // i = 6;
  64. * i = strcount("aaa aaa", "ab"); // i = 0;
  65. * i = strcount("aaa aaa", "aa"); // i = 2;
  66. */
  67. size_t strcount(const char *haystack, const char *needle);
  68. /**
  69. * cisalnum - isalnum() which takes a char (and doesn't accept EOF)
  70. * @c: a character
  71. *
  72. * Surprisingly, the standard ctype.h isalnum() takes an int, which
  73. * must have the value of EOF (-1) or an unsigned char. This variant
  74. * takes a real char, and doesn't accept EOF.
  75. */
  76. static inline bool cisalnum(char c)
  77. {
  78. return isalnum((unsigned char)c);
  79. }
  80. static inline bool cisalpha(char c)
  81. {
  82. return isalpha((unsigned char)c);
  83. }
  84. static inline bool cisascii(char c)
  85. {
  86. return isascii((unsigned char)c);
  87. }
  88. #if HAVE_ISBLANK
  89. static inline bool cisblank(char c)
  90. {
  91. return isblank((unsigned char)c);
  92. }
  93. #endif
  94. static inline bool ciscntrl(char c)
  95. {
  96. return iscntrl((unsigned char)c);
  97. }
  98. static inline bool cisdigit(char c)
  99. {
  100. return isdigit((unsigned char)c);
  101. }
  102. static inline bool cisgraph(char c)
  103. {
  104. return isgraph((unsigned char)c);
  105. }
  106. static inline bool cislower(char c)
  107. {
  108. return islower((unsigned char)c);
  109. }
  110. static inline bool cisprint(char c)
  111. {
  112. return isprint((unsigned char)c);
  113. }
  114. static inline bool cispunct(char c)
  115. {
  116. return ispunct((unsigned char)c);
  117. }
  118. static inline bool cisspace(char c)
  119. {
  120. return isspace((unsigned char)c);
  121. }
  122. static inline bool cisupper(char c)
  123. {
  124. return isupper((unsigned char)c);
  125. }
  126. static inline bool cisxdigit(char c)
  127. {
  128. return isxdigit((unsigned char)c);
  129. }
  130. #include <ccan/str/str_debug.h>
  131. /* These checks force things out of line, hence they are under DEBUG. */
  132. #ifdef CCAN_STR_DEBUG
  133. #include <ccan/build_assert/build_assert.h>
  134. /* These are commonly misused: they take -1 or an *unsigned* char value. */
  135. #undef isalnum
  136. #undef isalpha
  137. #undef isascii
  138. #undef isblank
  139. #undef iscntrl
  140. #undef isdigit
  141. #undef isgraph
  142. #undef islower
  143. #undef isprint
  144. #undef ispunct
  145. #undef isspace
  146. #undef isupper
  147. #undef isxdigit
  148. /* You can use a char if char is unsigned. */
  149. #if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
  150. #define str_check_arg_(i) \
  151. ((i) + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(i), \
  152. char) \
  153. || (char)255 > 0))
  154. #else
  155. #define str_check_arg_(i) (i)
  156. #endif
  157. #define isalnum(i) str_isalnum(str_check_arg_(i))
  158. #define isalpha(i) str_isalpha(str_check_arg_(i))
  159. #define isascii(i) str_isascii(str_check_arg_(i))
  160. #if HAVE_ISBLANK
  161. #define isblank(i) str_isblank(str_check_arg_(i))
  162. #endif
  163. #define iscntrl(i) str_iscntrl(str_check_arg_(i))
  164. #define isdigit(i) str_isdigit(str_check_arg_(i))
  165. #define isgraph(i) str_isgraph(str_check_arg_(i))
  166. #define islower(i) str_islower(str_check_arg_(i))
  167. #define isprint(i) str_isprint(str_check_arg_(i))
  168. #define ispunct(i) str_ispunct(str_check_arg_(i))
  169. #define isspace(i) str_isspace(str_check_arg_(i))
  170. #define isupper(i) str_isupper(str_check_arg_(i))
  171. #define isxdigit(i) str_isxdigit(str_check_arg_(i))
  172. #if HAVE_TYPEOF
  173. /* With GNU magic, we can make const-respecting standard string functions. */
  174. #undef strstr
  175. #undef strchr
  176. #undef strrchr
  177. /* + 0 is needed to decay array into pointer. */
  178. #define strstr(haystack, needle) \
  179. ((typeof((haystack) + 0))str_strstr((haystack), (needle)))
  180. #define strchr(haystack, c) \
  181. ((typeof((haystack) + 0))str_strchr((haystack), (c)))
  182. #define strrchr(haystack, c) \
  183. ((typeof((haystack) + 0))str_strrchr((haystack), (c)))
  184. #endif
  185. #endif /* CCAN_STR_DEBUG */
  186. #endif /* CCAN_STR_H */