]> git.notmuchmail.org Git - notmuch/blob - util/string-util.c
Merge tag '0.31.4'
[notmuch] / util / string-util.c
1 /* string-util.c -  Extra or enhanced routines for null terminated strings.
2  *
3  * Copyright (c) 2012 Jani Nikula
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Jani Nikula <jani@nikula.org>
19  */
20
21
22 #include "string-util.h"
23 #include "talloc.h"
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdbool.h>
28
29 char *
30 strtok_len (char *s, const char *delim, size_t *len)
31 {
32     /* skip initial delims */
33     s += strspn (s, delim);
34
35     /* length of token */
36     *len = strcspn (s, delim);
37
38     return *len ? s : NULL;
39 }
40
41 const char *
42 strsplit_len (const char *s, char delim, size_t *len)
43 {
44     bool escaping = false;
45     size_t count = 0;
46
47     /* Skip initial unescaped delimiters */
48     while (*s && *s == delim)
49         s++;
50
51     while (s[count] && (escaping || s[count] != delim)) {
52         escaping = (s[count] == '\\');
53         count++;
54     }
55
56     if (count==0)
57         return NULL;
58
59     *len = count;
60     return s;
61 }
62
63 const char *
64 strtok_len_c (const char *s, const char *delim, size_t *len)
65 {
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);
69 }
70
71 char *
72 sanitize_string (const void *ctx, const char *str)
73 {
74     char *out, *loop;
75
76     if (! str)
77         return NULL;
78
79     out = talloc_strdup (ctx, str);
80     if (! out)
81         return NULL;
82
83     for (loop = out; *loop; loop++) {
84         if (*loop == '\t' || *loop == '\n')
85             *loop = ' ';
86         else if ((unsigned char) (*loop) < 32)
87             *loop = '?';
88     }
89
90     return out;
91 }
92
93 static int
94 is_unquoted_terminator (unsigned char c)
95 {
96     return c == 0 || c <= ' ' || c == ')';
97 }
98
99 int
100 make_boolean_term (void *ctx, const char *prefix, const char *term,
101                    char **buf, size_t *len)
102 {
103     const char *in;
104     char *out;
105     size_t needed = 3;
106     int need_quoting = 0;
107
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. */
111     if (! term[0])
112         need_quoting = 1;
113     for (in = term; *in && ! need_quoting; in++)
114         if (is_unquoted_terminator (*in) || *in == '"' || *in == '('
115             || (unsigned char) *in > 127)
116             need_quoting = 1;
117
118     if (need_quoting)
119         for (in = term; *in; in++)
120             needed += (*in == '"') ? 2 : 1;
121     else
122         needed = strlen (term) + 1;
123
124     /* Reserve space for the prefix */
125     if (prefix)
126         needed += strlen (prefix) + 1;
127
128     if ((*buf == NULL) || (needed > *len)) {
129         *len = 2 * needed;
130         *buf = talloc_realloc (ctx, *buf, char, *len);
131     }
132
133     if (! *buf) {
134         errno = ENOMEM;
135         return -1;
136     }
137
138     out = *buf;
139
140     /* Copy in the prefix */
141     if (prefix) {
142         strcpy (out, prefix);
143         out += strlen (prefix);
144         *out++ = ':';
145     }
146
147     if (! need_quoting) {
148         strcpy (out, term);
149         return 0;
150     }
151
152     /* Quote term by enclosing it in double quotes and doubling any
153      * internal double quotes. */
154     *out++ = '"';
155     in = term;
156     while (*in) {
157         if (*in == '"')
158             *out++ = '"';
159         *out++ = *in++;
160     }
161     *out++ = '"';
162     *out = '\0';
163
164     return 0;
165 }
166
167 const char *
168 skip_space (const char *str)
169 {
170     while (*str && isspace ((unsigned char) *str))
171         ++str;
172     return str;
173 }
174
175 int
176 parse_boolean_term (void *ctx, const char *str,
177                     char **prefix_out, char **term_out)
178 {
179     int err = EINVAL;
180
181     *prefix_out = *term_out = NULL;
182
183     /* Parse prefix */
184     str = skip_space (str);
185     const char *pos = strchr (str, ':');
186     if (! pos || pos == str)
187         goto FAIL;
188     *prefix_out = talloc_strndup (ctx, str, pos - str);
189     if (! *prefix_out) {
190         err = ENOMEM;
191         goto FAIL;
192     }
193     ++pos;
194
195     /* Implement de-quoting compatible with make_boolean_term. */
196     if (*pos == '"') {
197         char *out = talloc_array (ctx, char, strlen (pos));
198         int closed = 0;
199         if (! out) {
200             err = ENOMEM;
201             goto FAIL;
202         }
203         *term_out = out;
204         /* Skip the opening quote, find the closing quote, and
205          * un-double doubled internal quotes. */
206         for (++pos; *pos; ) {
207             if (*pos == '"') {
208                 ++pos;
209                 if (*pos != '"') {
210                     /* Found the closing quote. */
211                     closed = 1;
212                     pos = skip_space (pos);
213                     break;
214                 }
215             }
216             *out++ = *pos++;
217         }
218         /* Did the term terminate without a closing quote or is there
219          * trailing text after the closing quote? */
220         if (! closed || *pos)
221             goto FAIL;
222         *out = '\0';
223     } else {
224         const char *start = pos;
225         /* Check for text after the boolean term. */
226         while (! is_unquoted_terminator (*pos))
227             ++pos;
228         if (*skip_space (pos)) {
229             err = EINVAL;
230             goto FAIL;
231         }
232         /* No trailing text; dup the string so the caller can free
233          * it. */
234         *term_out = talloc_strndup (ctx, start, pos - start);
235         if (! *term_out) {
236             err = ENOMEM;
237             goto FAIL;
238         }
239     }
240     return 0;
241
242   FAIL:
243     talloc_free (*prefix_out);
244     talloc_free (*term_out);
245     errno = err;
246     return -1;
247 }
248
249 int
250 strcmp_null (const char *s1, const char *s2)
251 {
252     if (s1 && s2)
253         return strcmp (s1, s2);
254     else if (! s1 && ! s2)
255         return 0;
256     else if (s1)
257         return 1;       /* s1 (non-NULL) is greater than s2 (NULL) */
258     else
259         return -1;      /* s1 (NULL) is less than s2 (non-NULL) */
260 }
261
262 int
263 strcase_equal (const void *a, const void *b)
264 {
265     return strcasecmp (a, b) == 0;
266 }
267
268 unsigned int
269 strcase_hash (const void *ptr)
270 {
271     const char *s = ptr;
272
273     /* This is the djb2 hash. */
274     unsigned int hash = 5381;
275
276     while (s && *s) {
277         hash = ((hash << 5) + hash) + tolower (*s);
278         s++;
279     }
280
281     return hash;
282 }
283
284 void
285 strip_trailing (char *str, char ch)
286 {
287     int i;
288
289     for (i = strlen (str) - 1; i >= 0; i--) {
290         if (str[i] == ch)
291             str[i] = '\0';
292         else
293             break;
294     }
295 }