]> 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;
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
187     if (! pos || pos == str)
188         goto FAIL;
189     *prefix_out = talloc_strndup (ctx, str, pos - str);
190     if (! *prefix_out) {
191         err = ENOMEM;
192         goto FAIL;
193     }
194     ++pos;
195
196     /* Implement de-quoting compatible with make_boolean_term. */
197     if (*pos == '"') {
198         char *out = talloc_array (ctx, char, strlen (pos));
199         int closed = 0;
200         if (! out) {
201             err = ENOMEM;
202             goto FAIL;
203         }
204         *term_out = out;
205         /* Skip the opening quote, find the closing quote, and
206          * un-double doubled internal quotes. */
207         for (++pos; *pos; ) {
208             if (*pos == '"') {
209                 ++pos;
210                 if (*pos != '"') {
211                     /* Found the closing quote. */
212                     closed = 1;
213                     pos = skip_space (pos);
214                     break;
215                 }
216             }
217             *out++ = *pos++;
218         }
219         /* Did the term terminate without a closing quote or is there
220          * trailing text after the closing quote? */
221         if (! closed || *pos)
222             goto FAIL;
223         *out = '\0';
224     } else {
225         const char *start = pos;
226         /* Check for text after the boolean term. */
227         while (! is_unquoted_terminator (*pos))
228             ++pos;
229         if (*skip_space (pos)) {
230             err = EINVAL;
231             goto FAIL;
232         }
233         /* No trailing text; dup the string so the caller can free
234          * it. */
235         *term_out = talloc_strndup (ctx, start, pos - start);
236         if (! *term_out) {
237             err = ENOMEM;
238             goto FAIL;
239         }
240     }
241     return 0;
242
243   FAIL:
244     talloc_free (*prefix_out);
245     talloc_free (*term_out);
246     errno = err;
247     return -1;
248 }
249
250 int
251 strcmp_null (const char *s1, const char *s2)
252 {
253     if (s1 && s2)
254         return strcmp (s1, s2);
255     else if (! s1 && ! s2)
256         return 0;
257     else if (s1)
258         return 1;       /* s1 (non-NULL) is greater than s2 (NULL) */
259     else
260         return -1;      /* s1 (NULL) is less than s2 (non-NULL) */
261 }
262
263 int
264 strcase_equal (const void *a, const void *b)
265 {
266     return strcasecmp (a, b) == 0;
267 }
268
269 unsigned int
270 strcase_hash (const void *ptr)
271 {
272     const char *s = ptr;
273
274     /* This is the djb2 hash. */
275     unsigned int hash = 5381;
276
277     while (s && *s) {
278         hash = ((hash << 5) + hash) + tolower (*s);
279         s++;
280     }
281
282     return hash;
283 }
284
285 void
286 strip_trailing (char *str, char ch)
287 {
288     int i;
289
290     for (i = strlen (str) - 1; i >= 0; i--) {
291         if (str[i] == ch)
292             str[i] = '\0';
293         else
294             break;
295     }
296 }