1 /* string-util.c - Extra or enhanced routines for null terminated strings.
3 * Copyright (c) 2012 Jani Nikula
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Jani Nikula <jani@nikula.org>
22 #include "string-util.h"
30 strtok_len (char *s, const char *delim, size_t *len)
32 /* skip initial delims */
33 s += strspn (s, delim);
36 *len = strcspn (s, delim);
38 return *len ? s : NULL;
42 strsplit_len (const char *s, char delim, size_t *len)
44 bool escaping = false;
47 /* Skip initial unescaped delimiters */
48 while (*s && *s == delim)
51 while (s[count] && (escaping || s[count] != delim)) {
52 escaping = (s[count] == '\\');
64 strtok_len_c (const char *s, const char *delim, size_t *len)
66 /* strtok_len is already const-safe, but we can't express both
67 * versions in the C type system. */
68 return strtok_len ((char *) s, delim, len);
72 sanitize_string (const void *ctx, const char *str)
79 out = talloc_strdup (ctx, str);
83 for (loop = out; *loop; loop++) {
84 if (*loop == '\t' || *loop == '\n')
86 else if ((unsigned char) (*loop) < 32)
94 is_unquoted_terminator (unsigned char c)
96 return c == 0 || c <= ' ' || c == ')';
100 make_boolean_term (void *ctx, const char *prefix, const char *term,
101 char **buf, size_t *len)
106 int need_quoting = 0;
108 /* Do we need quoting? To be paranoid, we quote anything
109 * containing a quote or '(', even though these only matter at the
110 * beginning, and anything containing non-ASCII text. */
113 for (in = term; *in && ! need_quoting; in++)
114 if (is_unquoted_terminator (*in) || *in == '"' || *in == '('
115 || (unsigned char) *in > 127)
119 for (in = term; *in; in++)
120 needed += (*in == '"') ? 2 : 1;
122 needed = strlen (term) + 1;
124 /* Reserve space for the prefix */
126 needed += strlen (prefix) + 1;
128 if ((*buf == NULL) || (needed > *len)) {
130 *buf = talloc_realloc (ctx, *buf, char, *len);
140 /* Copy in the prefix */
142 strcpy (out, prefix);
143 out += strlen (prefix);
147 if (! need_quoting) {
152 /* Quote term by enclosing it in double quotes and doubling any
153 * internal double quotes. */
168 skip_space (const char *str)
170 while (*str && isspace ((unsigned char) *str))
176 parse_boolean_term (void *ctx, const char *str,
177 char **prefix_out, char **term_out)
181 *prefix_out = *term_out = NULL;
184 str = skip_space (str);
185 const char *pos = strchr (str, ':');
187 if (! pos || pos == str)
189 *prefix_out = talloc_strndup (ctx, str, pos - str);
196 /* Implement de-quoting compatible with make_boolean_term. */
198 char *out = talloc_array (ctx, char, strlen (pos));
205 /* Skip the opening quote, find the closing quote, and
206 * un-double doubled internal quotes. */
207 for (++pos; *pos; ) {
211 /* Found the closing quote. */
213 pos = skip_space (pos);
219 /* Did the term terminate without a closing quote or is there
220 * trailing text after the closing quote? */
221 if (! closed || *pos)
225 const char *start = pos;
226 /* Check for text after the boolean term. */
227 while (! is_unquoted_terminator (*pos))
229 if (*skip_space (pos)) {
233 /* No trailing text; dup the string so the caller can free
235 *term_out = talloc_strndup (ctx, start, pos - start);
244 talloc_free (*prefix_out);
245 talloc_free (*term_out);
251 strcmp_null (const char *s1, const char *s2)
254 return strcmp (s1, s2);
255 else if (! s1 && ! s2)
258 return 1; /* s1 (non-NULL) is greater than s2 (NULL) */
260 return -1; /* s1 (NULL) is less than s2 (non-NULL) */
264 strcase_equal (const void *a, const void *b)
266 return strcasecmp (a, b) == 0;
270 strcase_hash (const void *ptr)
274 /* This is the djb2 hash. */
275 unsigned int hash = 5381;
278 hash = ((hash << 5) + hash) + tolower (*s);
286 strip_trailing (char *str, char ch)
290 for (i = strlen (str) - 1; i >= 0; i--) {