]> git.notmuchmail.org Git - notmuch/blob - util/string-util.c
emacs: Add new option notmuch-search-hide-excluded
[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, last_nonspace = 0;
46
47     /* Skip initial unescaped delimiters and whitespace */
48     while (*s && (*s == delim || isspace (*s)))
49         s++;
50
51     while (s[count] && (escaping || s[count] != delim)) {
52         if (! isspace (s[count]))
53             last_nonspace = count;
54         escaping = (s[count] == '\\');
55         count++;
56     }
57
58     if (count == 0)
59         return NULL;
60
61     *len = last_nonspace + 1;
62     return s;
63 }
64
65 const char *
66 strtok_len_c (const char *s, const char *delim, size_t *len)
67 {
68     /* strtok_len is already const-safe, but we can't express both
69      * versions in the C type system. */
70     return strtok_len ((char *) s, delim, len);
71 }
72
73 char *
74 sanitize_string (const void *ctx, const char *str)
75 {
76     char *out, *loop;
77
78     if (! str)
79         return NULL;
80
81     out = talloc_strdup (ctx, str);
82     if (! out)
83         return NULL;
84
85     for (loop = out; *loop; loop++) {
86         if (*loop == '\t' || *loop == '\n')
87             *loop = ' ';
88         else if ((unsigned char) (*loop) < 32)
89             *loop = '?';
90     }
91
92     return out;
93 }
94
95 static int
96 is_unquoted_terminator (unsigned char c)
97 {
98     return c == 0 || c <= ' ' || c == ')';
99 }
100
101 int
102 make_boolean_term (void *ctx, const char *prefix, const char *term,
103                    char **buf, size_t *len)
104 {
105     const char *in;
106     char *out;
107     size_t needed = 3;
108     int need_quoting = 0;
109
110     /* Do we need quoting?  To be paranoid, we quote anything
111      * containing a quote or '(', even though these only matter at the
112      * beginning, and anything containing non-ASCII text. */
113     if (! term[0])
114         need_quoting = 1;
115     for (in = term; *in && ! need_quoting; in++)
116         if (is_unquoted_terminator (*in) || *in == '"' || *in == '('
117             || (unsigned char) *in > 127)
118             need_quoting = 1;
119
120     if (need_quoting)
121         for (in = term; *in; in++)
122             needed += (*in == '"') ? 2 : 1;
123     else
124         needed = strlen (term) + 1;
125
126     /* Reserve space for the prefix */
127     if (prefix)
128         needed += strlen (prefix) + 1;
129
130     if ((*buf == NULL) || (needed > *len)) {
131         *len = 2 * needed;
132         *buf = talloc_realloc (ctx, *buf, char, *len);
133     }
134
135     if (! *buf) {
136         errno = ENOMEM;
137         return -1;
138     }
139
140     out = *buf;
141
142     /* Copy in the prefix */
143     if (prefix) {
144         strcpy (out, prefix);
145         out += strlen (prefix);
146         *out++ = ':';
147     }
148
149     if (! need_quoting) {
150         strcpy (out, term);
151         return 0;
152     }
153
154     /* Quote term by enclosing it in double quotes and doubling any
155      * internal double quotes. */
156     *out++ = '"';
157     in = term;
158     while (*in) {
159         if (*in == '"')
160             *out++ = '"';
161         *out++ = *in++;
162     }
163     *out++ = '"';
164     *out = '\0';
165
166     return 0;
167 }
168
169 const char *
170 skip_space (const char *str)
171 {
172     while (*str && isspace ((unsigned char) *str))
173         ++str;
174     return str;
175 }
176
177 int
178 parse_boolean_term (void *ctx, const char *str,
179                     char **prefix_out, char **term_out)
180 {
181     int err = EINVAL;
182
183     *prefix_out = *term_out = NULL;
184
185     /* Parse prefix */
186     str = skip_space (str);
187     const char *pos = strchr (str, ':');
188
189     if (! pos || pos == str)
190         goto FAIL;
191     *prefix_out = talloc_strndup (ctx, str, pos - str);
192     if (! *prefix_out) {
193         err = ENOMEM;
194         goto FAIL;
195     }
196     ++pos;
197
198     /* Implement de-quoting compatible with make_boolean_term. */
199     if (*pos == '"') {
200         char *out = talloc_array (ctx, char, strlen (pos));
201         int closed = 0;
202         if (! out) {
203             err = ENOMEM;
204             goto FAIL;
205         }
206         *term_out = out;
207         /* Skip the opening quote, find the closing quote, and
208          * un-double doubled internal quotes. */
209         for (++pos; *pos; ) {
210             if (*pos == '"') {
211                 ++pos;
212                 if (*pos != '"') {
213                     /* Found the closing quote. */
214                     closed = 1;
215                     pos = skip_space (pos);
216                     break;
217                 }
218             }
219             *out++ = *pos++;
220         }
221         /* Did the term terminate without a closing quote or is there
222          * trailing text after the closing quote? */
223         if (! closed || *pos)
224             goto FAIL;
225         *out = '\0';
226     } else {
227         const char *start = pos;
228         /* Check for text after the boolean term. */
229         while (! is_unquoted_terminator (*pos))
230             ++pos;
231         if (*skip_space (pos)) {
232             err = EINVAL;
233             goto FAIL;
234         }
235         /* No trailing text; dup the string so the caller can free
236          * it. */
237         *term_out = talloc_strndup (ctx, start, pos - start);
238         if (! *term_out) {
239             err = ENOMEM;
240             goto FAIL;
241         }
242     }
243     return 0;
244
245   FAIL:
246     talloc_free (*prefix_out);
247     talloc_free (*term_out);
248     errno = err;
249     return -1;
250 }
251
252 int
253 strcmp_null (const char *s1, const char *s2)
254 {
255     if (s1 && s2)
256         return strcmp (s1, s2);
257     else if (! s1 && ! s2)
258         return 0;
259     else if (s1)
260         return 1;       /* s1 (non-NULL) is greater than s2 (NULL) */
261     else
262         return -1;      /* s1 (NULL) is less than s2 (non-NULL) */
263 }
264
265 int
266 strcase_equal (const void *a, const void *b)
267 {
268     return strcasecmp (a, b) == 0;
269 }
270
271 unsigned int
272 strcase_hash (const void *ptr)
273 {
274     const char *s = ptr;
275
276     /* This is the djb2 hash. */
277     unsigned int hash = 5381;
278
279     while (s && *s) {
280         hash = ((hash << 5) + hash) + tolower (*s);
281         s++;
282     }
283
284     return hash;
285 }
286
287 void
288 strip_trailing (char *str, char ch)
289 {
290     int i;
291
292     for (i = strlen (str) - 1; i >= 0; i--) {
293         if (str[i] == ch)
294             str[i] = '\0';
295         else
296             break;
297     }
298 }