]> git.ozlabs.org Git - ccan/blob - ccan/str/str.h
str: add STR_MAX_CHARS().
[ccan] / ccan / str / str.h
1 /* Placed into the public domain. */
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 /**
11  * streq - Are two strings equal?
12  * @a: first string
13  * @b: first string
14  *
15  * This macro is arguably more readable than "!strcmp(a, b)".
16  *
17  * Example:
18  *      if (streq(somestring, ""))
19  *              printf("String is empty!\n");
20  */
21 #define streq(a,b) (strcmp((a),(b)) == 0)
22
23 /**
24  * strstarts - Does this string start with this prefix?
25  * @str: string to test
26  * @prefix: prefix to look for at start of str
27  *
28  * Example:
29  *      if (strstarts(somestring, "foo"))
30  *              printf("String %s begins with 'foo'!\n", somestring);
31  */
32 #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
33
34 /**
35  * strends - Does this string end with this postfix?
36  * @str: string to test
37  * @postfix: postfix to look for at end of str
38  *
39  * Example:
40  *      if (strends(somestring, "foo"))
41  *              printf("String %s end with 'foo'!\n", somestring);
42  */
43 static inline bool strends(const char *str, const char *postfix)
44 {
45         if (strlen(str) < strlen(postfix))
46                 return false;
47
48         return streq(str + strlen(str) - strlen(postfix), postfix);
49 }
50
51 /**
52  * stringify - Turn expression into a string literal
53  * @expr: any C expression
54  *
55  * Example:
56  *      #define PRINT_COND_IF_FALSE(cond) \
57  *              ((cond) || printf("%s is false!", stringify(cond)))
58  */
59 #define stringify(expr)         stringify_1(expr)
60 /* Double-indirection required to stringify expansions */
61 #define stringify_1(expr)       #expr
62
63 /**
64  * strcount - Count number of (non-overlapping) occurrences of a substring.
65  * @haystack: a C string
66  * @needle: a substring
67  *
68  * Example:
69  *      int i;
70  *      i = strcount("aaa aaa", "a");  // i = 6;
71  *      i = strcount("aaa aaa", "ab"); // i = 0;
72  *      i = strcount("aaa aaa", "aa"); // i = 2;
73  */
74 size_t strcount(const char *haystack, const char *needle);
75
76 /**
77  * STR_MAX_CHARS - Maximum possible size of numeric string for this type.
78  * @type_or_expr: a pointer or integer type or expression.
79  *
80  * This provides enough space for a nul-terminated string which represents the
81  * largest possible value for the type or expression.
82  *
83  * Note: The implementation adds extra space so hex values or negative
84  * values will fit (eg. sprintf(... "%p"). )
85  *
86  * Example:
87  *      char str[STR_MAX_CHARS(i)];
88  *
89  *      sprintf(str, "%i", i);
90  */
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))
94
95 #if HAVE_TYPEOF
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; })
99 #else
100 #define STR_MAX_CHARS_TCHECK_(type_or_expr) 0
101 #endif
102
103 /**
104  * cisalnum - isalnum() which takes a char (and doesn't accept EOF)
105  * @c: a character
106  *
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.
110  */
111 static inline bool cisalnum(char c)
112 {
113         return isalnum((unsigned char)c);
114 }
115 static inline bool cisalpha(char c)
116 {
117         return isalpha((unsigned char)c);
118 }
119 static inline bool cisascii(char c)
120 {
121         return isascii((unsigned char)c);
122 }
123 #if HAVE_ISBLANK
124 static inline bool cisblank(char c)
125 {
126         return isblank((unsigned char)c);
127 }
128 #endif
129 static inline bool ciscntrl(char c)
130 {
131         return iscntrl((unsigned char)c);
132 }
133 static inline bool cisdigit(char c)
134 {
135         return isdigit((unsigned char)c);
136 }
137 static inline bool cisgraph(char c)
138 {
139         return isgraph((unsigned char)c);
140 }
141 static inline bool cislower(char c)
142 {
143         return islower((unsigned char)c);
144 }
145 static inline bool cisprint(char c)
146 {
147         return isprint((unsigned char)c);
148 }
149 static inline bool cispunct(char c)
150 {
151         return ispunct((unsigned char)c);
152 }
153 static inline bool cisspace(char c)
154 {
155         return isspace((unsigned char)c);
156 }
157 static inline bool cisupper(char c)
158 {
159         return isupper((unsigned char)c);
160 }
161 static inline bool cisxdigit(char c)
162 {
163         return isxdigit((unsigned char)c);
164 }
165
166 #include <ccan/str/str_debug.h>
167
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>
171
172 /* These are commonly misused: they take -1 or an *unsigned* char value. */
173 #undef isalnum
174 #undef isalpha
175 #undef isascii
176 #undef isblank
177 #undef iscntrl
178 #undef isdigit
179 #undef isgraph
180 #undef islower
181 #undef isprint
182 #undef ispunct
183 #undef isspace
184 #undef isupper
185 #undef isxdigit
186
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), \
191                                                                   char) \
192                                     || (char)255 > 0))
193 #else
194 #define str_check_arg_(i) (i)
195 #endif
196
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))
200 #if HAVE_ISBLANK
201 #define isblank(i) str_isblank(str_check_arg_(i))
202 #endif
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))
212
213 #if HAVE_TYPEOF
214 /* With GNU magic, we can make const-respecting standard string functions. */
215 #undef strstr
216 #undef strchr
217 #undef strrchr
218
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)))
226 #endif
227 #endif /* CCAN_STR_DEBUG */
228
229 #endif /* CCAN_STR_H */