7 * streq - Are two strings equal?
11 * This macro is arguably more readable than "!strcmp(a, b)".
15 * printf("String is empty!\n");
17 #define streq(a,b) (strcmp((a),(b)) == 0)
20 * strstarts - Does this string start with this prefix?
21 * @str: string to test
22 * @prefix: prefix to look for at start of str
25 * if (strstarts(str, "foo"))
26 * printf("String %s begins with 'foo'!\n", str);
28 #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
31 * strends - Does this string end with this postfix?
32 * @str: string to test
33 * @postfix: postfix to look for at end of str
36 * if (strends(str, "foo"))
37 * printf("String %s end with 'foo'!\n", str);
39 static inline bool strends(const char *str, const char *postfix)
41 if (strlen(str) < strlen(postfix))
44 return streq(str + strlen(str) - strlen(postfix), postfix);
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
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
60 * The final char * in the array will be NULL, so you can use this or
61 * @nump to find the array length.
64 * unsigned int count_long_lines(const char *text)
67 * unsigned int i, long_lines = 0;
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)
78 char **strsplit(const void *ctx, const char *string, const char *delims,
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
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.
92 * // Append the string "--EOL" to each line.
93 * char *append_to_all_lines(const char *string)
96 * unsigned int i, num, newnum;
98 * lines = strsplit(NULL, string, "\n", NULL);
99 * ret = strjoin(NULL, lines, "-- EOL\n");
100 * talloc_free(lines);
104 char *strjoin(const void *ctx, char *strings[], const char *delim);
106 void *grab_fd(const void *ctx, int fd);
108 void *grab_file(const void *ctx, const char *filename);
109 #endif /* CCAN_STRING_H */