]> git.ozlabs.org Git - ccan/blob - ccan/str/str.h
compiler: use everywhere.
[ccan] / ccan / str / str.h
1 #ifndef CCAN_STR_H
2 #define CCAN_STR_H
3 #include <string.h>
4 #include <stdbool.h>
5
6 /**
7  * streq - Are two strings equal?
8  * @a: first string
9  * @b: first string
10  *
11  * This macro is arguably more readable than "!strcmp(a, b)".
12  *
13  * Example:
14  *      if (streq(str, ""))
15  *              printf("String is empty!\n");
16  */
17 #define streq(a,b) (strcmp((a),(b)) == 0)
18
19 /**
20  * strstarts - Does this string start with this prefix?
21  * @str: string to test
22  * @prefix: prefix to look for at start of str
23  *
24  * Example:
25  *      if (strstarts(str, "foo"))
26  *              printf("String %s begins with 'foo'!\n", str);
27  */
28 #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
29
30 /**
31  * strends - Does this string end with this postfix?
32  * @str: string to test
33  * @postfix: postfix to look for at end of str
34  *
35  * Example:
36  *      if (strends(str, "foo"))
37  *              printf("String %s end with 'foo'!\n", str);
38  */
39 static inline bool strends(const char *str, const char *postfix)
40 {
41         if (strlen(str) < strlen(postfix))
42                 return false;
43
44         return streq(str + strlen(str) - strlen(postfix), postfix);
45 }
46
47 /**
48  * stringify - Turn expression into a string literal
49  * @expr: any C expression
50  *
51  * Example:
52  *      #define PRINT_COND_IF_FALSE(cond) \
53  *              ((cond) || printf("%s is false!", stringify(cond)))
54  */
55 #define stringify(expr)         stringify_1(expr)
56 /* Double-indirection required to stringify expansions */
57 #define stringify_1(expr)       #expr
58 #endif /* CCAN_STR_H */