9 * streq - Are two strings equal?
13 * This macro is arguably more readable than "!strcmp(a, b)".
16 * if (streq(somestring, ""))
17 * printf("String is empty!\n");
19 #define streq(a,b) (strcmp((a),(b)) == 0)
22 * strstarts - Does this string start with this prefix?
23 * @str: string to test
24 * @prefix: prefix to look for at start of str
27 * if (strstarts(somestring, "foo"))
28 * printf("String %s begins with 'foo'!\n", somestring);
30 #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
33 * strends - Does this string end with this postfix?
34 * @str: string to test
35 * @postfix: postfix to look for at end of str
38 * if (strends(somestring, "foo"))
39 * printf("String %s end with 'foo'!\n", somestring);
41 static inline bool strends(const char *str, const char *postfix)
43 if (strlen(str) < strlen(postfix))
46 return streq(str + strlen(str) - strlen(postfix), postfix);
50 * stringify - Turn expression into a string literal
51 * @expr: any C expression
54 * #define PRINT_COND_IF_FALSE(cond) \
55 * ((cond) || printf("%s is false!", stringify(cond)))
57 #define stringify(expr) stringify_1(expr)
58 /* Double-indirection required to stringify expansions */
59 #define stringify_1(expr) #expr
62 * strcount - Count number of (non-overlapping) occurrences of a substring.
63 * @haystack: a C string
64 * @needle: a substring
68 * i = strcount("aaa aaa", "a"); // i = 6;
69 * i = strcount("aaa aaa", "ab"); // i = 0;
70 * i = strcount("aaa aaa", "aa"); // i = 2;
72 size_t strcount(const char *haystack, const char *needle);
75 * cisalnum - isalnum() which takes a char (and doesn't accept EOF)
78 * Surprisingly, the standard ctype.h isalnum() takes an int, which
79 * must have the value of EOF (-1) or an unsigned char. This variant
80 * takes a real char, and doesn't accept EOF.
82 static inline bool cisalnum(char c)
84 return isalnum((unsigned char)c);
86 static inline bool cisalpha(char c)
88 return isalpha((unsigned char)c);
90 static inline bool cisascii(char c)
92 return isascii((unsigned char)c);
95 static inline bool cisblank(char c)
97 return isblank((unsigned char)c);
100 static inline bool ciscntrl(char c)
102 return iscntrl((unsigned char)c);
104 static inline bool cisdigit(char c)
106 return isdigit((unsigned char)c);
108 static inline bool cisgraph(char c)
110 return isgraph((unsigned char)c);
112 static inline bool cislower(char c)
114 return islower((unsigned char)c);
116 static inline bool cisprint(char c)
118 return isprint((unsigned char)c);
120 static inline bool cispunct(char c)
122 return ispunct((unsigned char)c);
124 static inline bool cisspace(char c)
126 return isspace((unsigned char)c);
128 static inline bool cisupper(char c)
130 return isupper((unsigned char)c);
132 static inline bool cisxdigit(char c)
134 return isxdigit((unsigned char)c);
137 #include <ccan/str/str_debug.h>
139 /* These checks force things out of line, hence they are under DEBUG. */
140 #ifdef CCAN_STR_DEBUG
141 #include <ccan/build_assert/build_assert.h>
143 /* These are commonly misused: they take -1 or an *unsigned* char value. */
158 /* You can use a char if char is unsigned. */
159 #if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
160 #define str_check_arg_(i) \
161 ((i) + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(i), \
165 #define str_check_arg_(i) (i)
168 #define isalnum(i) str_isalnum(str_check_arg_(i))
169 #define isalpha(i) str_isalpha(str_check_arg_(i))
170 #define isascii(i) str_isascii(str_check_arg_(i))
172 #define isblank(i) str_isblank(str_check_arg_(i))
174 #define iscntrl(i) str_iscntrl(str_check_arg_(i))
175 #define isdigit(i) str_isdigit(str_check_arg_(i))
176 #define isgraph(i) str_isgraph(str_check_arg_(i))
177 #define islower(i) str_islower(str_check_arg_(i))
178 #define isprint(i) str_isprint(str_check_arg_(i))
179 #define ispunct(i) str_ispunct(str_check_arg_(i))
180 #define isspace(i) str_isspace(str_check_arg_(i))
181 #define isupper(i) str_isupper(str_check_arg_(i))
182 #define isxdigit(i) str_isxdigit(str_check_arg_(i))
185 /* With GNU magic, we can make const-respecting standard string functions. */
190 /* + 0 is needed to decay array into pointer. */
191 #define strstr(haystack, needle) \
192 ((typeof((haystack) + 0))str_strstr((haystack), (needle)))
193 #define strchr(haystack, c) \
194 ((typeof((haystack) + 0))str_strchr((haystack), (c)))
195 #define strrchr(haystack, c) \
196 ((typeof((haystack) + 0))str_strrchr((haystack), (c)))
198 #endif /* CCAN_STR_DEBUG */
200 #endif /* CCAN_STR_H */