]> git.ozlabs.org Git - ccan/blob - ccan/str/str.h
54afca0f21c2afc671a7d0f5ad356e4fe1604b0f
[ccan] / ccan / str / str.h
1 #ifndef CCAN_STR_H
2 #define CCAN_STR_H
3 #include "config.h"
4 #include <string.h>
5 #include <stdbool.h>
6 #include <ctype.h>
7
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 /**
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 /**
33  * strends - Does this string end with this postfix?
34  * @str: string to test
35  * @postfix: postfix to look for at end of str
36  *
37  * Example:
38  *      if (strends(somestring, "foo"))
39  *              printf("String %s end with 'foo'!\n", somestring);
40  */
41 static inline bool strends(const char *str, const char *postfix)
42 {
43         if (strlen(str) < strlen(postfix))
44                 return false;
45
46         return streq(str + strlen(str) - strlen(postfix), postfix);
47 }
48
49 /**
50  * stringify - Turn expression into a string literal
51  * @expr: any C expression
52  *
53  * Example:
54  *      #define PRINT_COND_IF_FALSE(cond) \
55  *              ((cond) || printf("%s is false!", stringify(cond)))
56  */
57 #define stringify(expr)         stringify_1(expr)
58 /* Double-indirection required to stringify expansions */
59 #define stringify_1(expr)       #expr
60
61 /**
62  * strcount - Count number of (non-overlapping) occurrences of a substring.
63  * @haystack: a C string
64  * @needle: a substring
65  *
66  * Example:
67  *      int i;
68  *      i = strcount("aaa aaa", "a");  // i = 6;
69  *      i = strcount("aaa aaa", "ab"); // i = 0;
70  *      i = strcount("aaa aaa", "aa"); // i = 2;
71  */
72 size_t strcount(const char *haystack, const char *needle);
73
74 #include <ccan/str/str_debug.h>
75
76 /* These checks force things out of line, hence they are under DEBUG. */
77 #ifdef CCAN_STR_DEBUG
78 #include <ccan/build_assert/build_assert.h>
79
80 /* These are commonly misused: they take -1 or an *unsigned* char value. */
81 #undef isalnum
82 #undef isalpha
83 #undef isascii
84 #undef isblank
85 #undef iscntrl
86 #undef isdigit
87 #undef isgraph
88 #undef islower
89 #undef isprint
90 #undef ispunct
91 #undef isspace
92 #undef isupper
93 #undef isxdigit
94
95 /* You can use a char if char is unsigned. */
96 #if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
97 #define str_check_arg_(i)                                               \
98         ((i) + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(i), \
99                                                                   char) \
100                                     || (char)255 > 0))
101 #else
102 #define str_check_arg_(i) (i)
103 #endif
104
105 #define isalnum(i) str_isalnum(str_check_arg_(i))
106 #define isalpha(i) str_isalpha(str_check_arg_(i))
107 #define isascii(i) str_isascii(str_check_arg_(i))
108 #define isblank(i) str_isblank(str_check_arg_(i))
109 #define iscntrl(i) str_iscntrl(str_check_arg_(i))
110 #define isdigit(i) str_isdigit(str_check_arg_(i))
111 #define isgraph(i) str_isgraph(str_check_arg_(i))
112 #define islower(i) str_islower(str_check_arg_(i))
113 #define isprint(i) str_isprint(str_check_arg_(i))
114 #define ispunct(i) str_ispunct(str_check_arg_(i))
115 #define isspace(i) str_isspace(str_check_arg_(i))
116 #define isupper(i) str_isupper(str_check_arg_(i))
117 #define isxdigit(i) str_isxdigit(str_check_arg_(i))
118
119 #if HAVE_TYPEOF
120 /* With GNU magic, we can make const-respecting standard string functions. */
121 #undef strstr
122 #undef strchr
123 #undef strrchr
124
125 /* + 0 is needed to decay array into pointer. */
126 #define strstr(haystack, needle)                                        \
127         ((typeof((haystack) + 0))str_strstr((haystack), (needle)))
128 #define strchr(haystack, c)                                     \
129         ((typeof((haystack) + 0))str_strchr((haystack), (c)))
130 #define strrchr(haystack, c)                                    \
131         ((typeof((haystack) + 0))str_strrchr((haystack), (c)))
132 #endif
133 #endif /* CCAN_STR_DEBUG */
134
135 #endif /* CCAN_STR_H */