]> git.notmuchmail.org Git - notmuch/blob - util/string-util.h
debian: add changelog stanza for 0.18.2-1
[notmuch] / util / string-util.h
1 #ifndef _STRING_UTIL_H
2 #define _STRING_UTIL_H
3
4 #include <string.h>
5
6 /* like strtok(3), but without state, and doesn't modify s.  Return
7  * value is indicated by pointer and length, not null terminator.
8  *
9  * Usage pattern:
10  *
11  * const char *tok = input;
12  * const char *delim = " \t";
13  * size_t tok_len = 0;
14  *
15  * while ((tok = strtok_len (tok + tok_len, delim, &tok_len)) != NULL) {
16  *     // do stuff with string tok of length tok_len
17  * }
18  */
19
20 char *strtok_len (char *s, const char *delim, size_t *len);
21
22 /* Return a talloced string with str sanitized.
23  *
24  * Whitespace characters (tabs and newlines) are replaced with spaces,
25  * non-printable characters with question marks.
26  */
27 char *sanitize_string (const void *ctx, const char *str);
28
29 /* Construct a boolean term query with the specified prefix (e.g.,
30  * "id") and search term, quoting term as necessary.  Specifically, if
31  * term contains any non-printable ASCII characters, non-ASCII
32  * characters, close parenthesis or double quotes, it will be enclosed
33  * in double quotes and any internal double quotes will be doubled
34  * (e.g. a"b -> "a""b").  The result will be a valid notmuch query and
35  * can be parsed by parse_boolean_term.
36  *
37  * Output is into buf; it may be talloc_realloced.
38  * Return: 0 on success, -1 on error.  errno will be set to ENOMEM if
39  * there is an allocation failure.
40  */
41 int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,
42                        char **buf, size_t *len);
43
44 /* Parse a boolean term query consisting of a prefix, a colon, and a
45  * term that may be quoted as described for make_boolean_term.  If the
46  * term is not quoted, then it ends at the first whitespace or close
47  * parenthesis.  str may containing leading or trailing whitespace,
48  * but anything else is considered a parse error.  This is compatible
49  * with anything produced by make_boolean_term, and supports a subset
50  * of the quoting styles supported by Xapian (and hence notmuch).
51  * *prefix_out and *term_out will be talloc'd with context ctx.
52  *
53  * Return: 0 on success, -1 on error.  errno will be set to EINVAL if
54  * there is a parse error or ENOMEM if there is an allocation failure.
55  */
56 int
57 parse_boolean_term (void *ctx, const char *str,
58                     char **prefix_out, char **term_out);
59
60 #endif