This obsoletes

  id:1356493723-11085-1-git-send-email-amdragon at mit.edu

This version improves the documentation comment for make_boolean_term
and hopefully simplifies parse_boolean_term (though in a somewhat
different way than David suggested).

The diff relative to v2 follows

diff --git a/util/string-util.c b/util/string-util.c
index db01b4b..83b4953 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -112,11 +112,12 @@ parse_boolean_term (void *ctx, const char *str,

     /* Implement de-quoting compatible with make_boolean_term. */
     if (*pos == '"') {
-       char *out = talloc_strdup (ctx, pos + 1);
+       char *out = talloc_array (ctx, char, strlen (pos));
        int closed = 0;
-       /* Find the closing quote and un-double doubled internal
-        * quotes. */
-       for (pos = *term_out = out; *pos; ) {
+       *term_out = out;
+       /* Skip the opening quote, find the closing quote, and
+        * un-double doubled internal quotes. */
+       for (++pos; *pos; ) {
            if (*pos == '"') {
                ++pos;
                if (*pos != '"') {
@@ -133,12 +134,15 @@ parse_boolean_term (void *ctx, const char *str,
            goto FAIL;
        *out = '\0';
     } else {
-       *term_out = talloc_strdup (ctx, pos);
+       const char *start = pos;
        /* Check for text after the boolean term. */
        while (*pos > ' ' && *pos != ')')
            ++pos;
        if (*pos)
            goto FAIL;
+       /* No trailing text; dup the string so the caller can free
+        * it. */
+       *term_out = talloc_strdup (ctx, start);
     }
     return 0;

diff --git a/util/string-util.h b/util/string-util.h
index aff2d65..43d49d0 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -20,7 +20,12 @@
 char *strtok_len (char *s, const char *delim, size_t *len);

 /* Construct a boolean term query with the specified prefix (e.g.,
- * "id") and search term, quoting term as necessary.
+ * "id") and search term, quoting term as necessary.  Specifically, if
+ * term contains any non-printable ASCII characters, non-ASCII
+ * characters, close parenthesis or double quotes, it will be enclosed
+ * in double quotes and any internal double quotes will be doubled
+ * (e.g. a"b -> "a""b").  The result will be a valid notmuch query and
+ * can be parsed by parse_boolean_term.
  *
  * Output is into buf; it may be talloc_realloced.
  * Return: 0 on success, non-zero on memory allocation failure.

Reply via email to