str.h 5.8 KB

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