]> git.ozlabs.org Git - ccan/blob - ccan/string/string.h
merge
[ccan] / ccan / string / string.h
1 #ifndef CCAN_STRING_H
2 #define CCAN_STRING_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  * strsplit - Split string into an array of substrings
49  * @ctx: the context to tallocate from (often NULL)
50  * @string: the string to split
51  * @delims: delimiters where lines should be split.
52  * @nump: optional pointer to place resulting number of lines
53  *
54  * This function splits a single string into multiple strings.  The
55  * original string is untouched: an array is allocated (using talloc)
56  * pointing to copies of each substring.  Multiple delimiters result
57  * in empty substrings.  By definition, no delimiters will appear in
58  * the substrings.
59  *
60  * The final char * in the array will be NULL, so you can use this or
61  * @nump to find the array length.
62  *
63  * Example:
64  *      unsigned int count_long_lines(const char *text)
65  *      {
66  *              char **lines;
67  *              unsigned int i, long_lines = 0;
68  *
69  *              // Can only fail on out-of-memory.
70  *              lines = strsplit(NULL, string, "\n", NULL);
71  *              for (i = 0; lines[i] != NULL; i++)
72  *                      if (strlen(lines[i]) > 80)
73  *                              long_lines++;
74  *              talloc_free(lines);
75  *              return long_lines;
76  *      }
77  */
78 char **strsplit(const void *ctx, const char *string, const char *delims,
79                  unsigned int *nump);
80
81 /**
82  * strjoin - Join an array of substrings into one long string
83  * @ctx: the context to tallocate from (often NULL)
84  * @strings: the NULL-terminated array of strings to join
85  * @delim: the delimiter to insert between the strings
86  *
87  * This function joins an array of strings into a single string.  The
88  * return value is allocated using talloc.  Each string in @strings is
89  * followed by a copy of @delim.
90  *
91  * Example:
92  *      // Append the string "--EOL" to each line.
93  *      char *append_to_all_lines(const char *string)
94  *      {
95  *              char **lines, *ret;
96  *              unsigned int i, num, newnum;
97  *
98  *              lines = strsplit(NULL, string, "\n", NULL);
99  *              ret = strjoin(NULL, lines, "-- EOL\n");
100  *              talloc_free(lines);
101  *              return ret;
102  *      }
103  */
104 char *strjoin(const void *ctx, char *strings[], const char *delim);
105
106 void *grab_fd(const void *ctx, int fd);
107
108 void *grab_file(const void *ctx, const char *filename);
109 #endif /* CCAN_STRING_H */