1 /* Placed into the public domain. */
11 * streq - Are two strings equal?
15 * This macro is arguably more readable than "!strcmp(a, b)".
18 * if (streq(somestring, ""))
19 * printf("String is empty!\n");
21 #define streq(a,b) (strcmp((a),(b)) == 0)
24 * strstarts - Does this string start with this prefix?
25 * @str: string to test
26 * @prefix: prefix to look for at start of str
29 * if (strstarts(somestring, "foo"))
30 * printf("String %s begins with 'foo'!\n", somestring);
32 #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
35 * strends - Does this string end with this postfix?
36 * @str: string to test
37 * @postfix: postfix to look for at end of str
40 * if (strends(somestring, "foo"))
41 * printf("String %s end with 'foo'!\n", somestring);
43 static inline bool strends(const char *str, const char *postfix)
45 if (strlen(str) < strlen(postfix))
48 return streq(str + strlen(str) - strlen(postfix), postfix);
52 * stringify - Turn expression into a string literal
53 * @expr: any C expression
56 * #define PRINT_COND_IF_FALSE(cond) \
57 * ((cond) || printf("%s is false!", stringify(cond)))
59 #define stringify(expr) stringify_1(expr)
60 /* Double-indirection required to stringify expansions */
61 #define stringify_1(expr) #expr
64 * strcount - Count number of (non-overlapping) occurrences of a substring.
65 * @haystack: a C string
66 * @needle: a substring
70 * i = strcount("aaa aaa", "a"); // i = 6;
71 * i = strcount("aaa aaa", "ab"); // i = 0;
72 * i = strcount("aaa aaa", "aa"); // i = 2;
74 size_t strcount(const char *haystack, const char *needle);
77 * STR_MAX_CHARS - Maximum possible size of numeric string for this type.
78 * @type_or_expr: a pointer or integer type or expression.
80 * This provides enough space for a nul-terminated string which represents the
81 * largest possible value for the type or expression.
83 * Note: The implementation adds extra space so hex values or negative
84 * values will fit (eg. sprintf(... "%p"). )
87 * char str[STR_MAX_CHARS(i)];
89 * sprintf(str, "%i", i);
91 #define STR_MAX_CHARS(type_or_expr) \
92 ((sizeof(type_or_expr) * CHAR_BIT + 8) / 9 * 3 + 2 \
93 + STR_MAX_CHARS_TCHECK_(type_or_expr))
96 /* Only a simple type can have 0 assigned, so test that. */
97 #define STR_MAX_CHARS_TCHECK_(type_or_expr) \
98 ({ typeof(type_or_expr) x = 0; (void)x; 0; })
100 #define STR_MAX_CHARS_TCHECK_(type_or_expr) 0
104 * cisalnum - isalnum() which takes a char (and doesn't accept EOF)
107 * Surprisingly, the standard ctype.h isalnum() takes an int, which
108 * must have the value of EOF (-1) or an unsigned char. This variant
109 * takes a real char, and doesn't accept EOF.
111 static inline bool cisalnum(char c)
113 return isalnum((unsigned char)c);
115 static inline bool cisalpha(char c)
117 return isalpha((unsigned char)c);
119 static inline bool cisascii(char c)
121 return isascii((unsigned char)c);
124 static inline bool cisblank(char c)
126 return isblank((unsigned char)c);
129 static inline bool ciscntrl(char c)
131 return iscntrl((unsigned char)c);
133 static inline bool cisdigit(char c)
135 return isdigit((unsigned char)c);
137 static inline bool cisgraph(char c)
139 return isgraph((unsigned char)c);
141 static inline bool cislower(char c)
143 return islower((unsigned char)c);
145 static inline bool cisprint(char c)
147 return isprint((unsigned char)c);
149 static inline bool cispunct(char c)
151 return ispunct((unsigned char)c);
153 static inline bool cisspace(char c)
155 return isspace((unsigned char)c);
157 static inline bool cisupper(char c)
159 return isupper((unsigned char)c);
161 static inline bool cisxdigit(char c)
163 return isxdigit((unsigned char)c);
166 #include <ccan/str/str_debug.h>
168 /* These checks force things out of line, hence they are under DEBUG. */
169 #ifdef CCAN_STR_DEBUG
170 #include <ccan/build_assert/build_assert.h>
172 /* These are commonly misused: they take -1 or an *unsigned* char value. */
187 /* You can use a char if char is unsigned. */
188 #if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
189 #define str_check_arg_(i) \
190 ((i) + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(i), \
194 #define str_check_arg_(i) (i)
197 #define isalnum(i) str_isalnum(str_check_arg_(i))
198 #define isalpha(i) str_isalpha(str_check_arg_(i))
199 #define isascii(i) str_isascii(str_check_arg_(i))
201 #define isblank(i) str_isblank(str_check_arg_(i))
203 #define iscntrl(i) str_iscntrl(str_check_arg_(i))
204 #define isdigit(i) str_isdigit(str_check_arg_(i))
205 #define isgraph(i) str_isgraph(str_check_arg_(i))
206 #define islower(i) str_islower(str_check_arg_(i))
207 #define isprint(i) str_isprint(str_check_arg_(i))
208 #define ispunct(i) str_ispunct(str_check_arg_(i))
209 #define isspace(i) str_isspace(str_check_arg_(i))
210 #define isupper(i) str_isupper(str_check_arg_(i))
211 #define isxdigit(i) str_isxdigit(str_check_arg_(i))
214 /* With GNU magic, we can make const-respecting standard string functions. */
219 /* + 0 is needed to decay array into pointer. */
220 #define strstr(haystack, needle) \
221 ((typeof((haystack) + 0))str_strstr((haystack), (needle)))
222 #define strchr(haystack, c) \
223 ((typeof((haystack) + 0))str_strchr((haystack), (c)))
224 #define strrchr(haystack, c) \
225 ((typeof((haystack) + 0))str_strrchr((haystack), (c)))
227 #endif /* CCAN_STR_DEBUG */
229 #endif /* CCAN_STR_H */