Re: [PATCH v4 2/5] util: Function to parse boolean term queries
authorJani Nikula <jani@nikula.org>
Thu, 3 Jan 2013 17:09:08 +0000 (18:09 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:54 +0000 (09:52 -0800)
dc/5911b26dd84600c03c8f128277eecbe661f9ed [new file with mode: 0644]

diff --git a/dc/5911b26dd84600c03c8f128277eecbe661f9ed b/dc/5911b26dd84600c03c8f128277eecbe661f9ed
new file mode 100644 (file)
index 0000000..685c6b6
--- /dev/null
@@ -0,0 +1,206 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id DADF0431FAF\r
+       for <notmuch@notmuchmail.org>; Thu,  3 Jan 2013 09:09:19 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id mZVqrZHqFyow for <notmuch@notmuchmail.org>;\r
+       Thu,  3 Jan 2013 09:09:19 -0800 (PST)\r
+Received: from mail-we0-f179.google.com (mail-we0-f179.google.com\r
+       [74.125.82.179]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 20B09431FAE\r
+       for <notmuch@notmuchmail.org>; Thu,  3 Jan 2013 09:09:19 -0800 (PST)\r
+Received: by mail-we0-f179.google.com with SMTP id r6so7149381wey.38\r
+       for <notmuch@notmuchmail.org>; Thu, 03 Jan 2013 09:09:17 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=x-received:from:to:cc:subject:in-reply-to:references:user-agent\r
+       :date:message-id:mime-version:content-type:x-gm-message-state;\r
+       bh=e4PnHTCba8bM8wgutAbPE19VYXrJzHa/EWjghj7pLy4=;\r
+       b=dxR5ygrgCQZZ3FhhYoQTPSH5mLH5xO+4sjidSEUvxm+q1b2OwTK7xItJAAXtsGSmsI\r
+       C90xFVk92c1mK56jX0iVC3WzZeuy/NusjK5HWokh/WspZJdVZvX6mZOM2PvkHghdG7Nx\r
+       AXN3jxJ21vxSWDFgKnNDJJzKDM+FZkuIKbF05VVbGBaRYby2Ho/A4bcjPx+puAubDOmF\r
+       5L1ZEihNEHeNllMz1gWVweqNLEBoxccbBCnh6dHEY+35yaPtbI08CzQiw2TzgnxKdL/P\r
+       3d0kuMhDrVpEzVjHBhI1DKA49qbOi6PXFMEzlHbrsAytL51FJVH8uz2KieOU1LbJkbg3\r
+       t/XQ==\r
+X-Received: by 10.180.88.138 with SMTP id bg10mr76204269wib.13.1357232956539; \r
+       Thu, 03 Jan 2013 09:09:16 -0800 (PST)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+       by mx.google.com with ESMTPS id s10sm86244668wiw.4.2013.01.03.09.09.14\r
+       (version=SSLv3 cipher=OTHER); Thu, 03 Jan 2013 09:09:15 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v4 2/5] util: Function to parse boolean term queries\r
+In-Reply-To: <1356936162-2589-3-git-send-email-amdragon@mit.edu>\r
+References: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
+       <1356936162-2589-3-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.14+235~gdaf492b (http://notmuchmail.org) Emacs/23.2.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Thu, 03 Jan 2013 18:09:08 +0100\r
+Message-ID: <87vcbegpmz.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQlLS1mRI4Jj6V15GKpL4ym1d3UPtpKWkZr4x/QMkVL6GVzhLhhw5wxQiNWa67J59wy+Xx5a\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 03 Jan 2013 17:09:20 -0000\r
+\r
+On Mon, 31 Dec 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> This parses the subset of Xapian's boolean term quoting rules that are\r
+> used by make_boolean_term.  This is provided as a generic string\r
+> utility, but will be used shortly in notmuch restore to parse and\r
+> optimize for ID queries.\r
+> ---\r
+>  util/string-util.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  util/string-util.h |   15 ++++++++++++\r
+>  2 files changed, 82 insertions(+)\r
+>\r
+> diff --git a/util/string-util.c b/util/string-util.c\r
+> index e4bea21..52c7781 100644\r
+> --- a/util/string-util.c\r
+> +++ b/util/string-util.c\r
+> @@ -22,6 +22,8 @@\r
+>  #include "string-util.h"\r
+>  #include "talloc.h"\r
+>  \r
+> +#include <ctype.h>\r
+> +\r
+>  char *\r
+>  strtok_len (char *s, const char *delim, size_t *len)\r
+>  {\r
+> @@ -96,3 +98,68 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+>  \r
+>      return 0;\r
+>  }\r
+> +\r
+> +static const char*\r
+> +skip_space (const char *str)\r
+> +{\r
+> +    while (*str && isspace (*str))\r
+\r
+Pedantic: isspace ((unsigned char) *str)\r
+\r
+> +    ++str;\r
+> +    return str;\r
+> +}\r
+> +\r
+> +int\r
+> +parse_boolean_term (void *ctx, const char *str,\r
+> +                char **prefix_out, char **term_out)\r
+> +{\r
+> +    *prefix_out = *term_out = NULL;\r
+> +\r
+> +    /* Parse prefix */\r
+> +    str = skip_space (str);\r
+> +    const char *pos = strchr (str, ':');\r
+> +    if (! pos)\r
+\r
+if (! pos || pos == str) ?\r
+\r
+> +    goto FAIL;\r
+\r
+Could just return 1 here.\r
+\r
+> +    *prefix_out = talloc_strndup (ctx, str, pos - str);\r
+> +    ++pos;\r
+> +\r
+> +    /* Implement de-quoting compatible with make_boolean_term. */\r
+> +    if (*pos == '"') {\r
+> +    char *out = talloc_array (ctx, char, strlen (pos));\r
+> +    int closed = 0;\r
+> +    *term_out = out;\r
+> +    /* Skip the opening quote, find the closing quote, and\r
+> +     * un-double doubled internal quotes. */\r
+> +    for (++pos; *pos; ) {\r
+> +        if (*pos == '"') {\r
+> +            ++pos;\r
+> +            if (*pos != '"') {\r
+> +                /* Found the closing quote. */\r
+> +                closed = 1;\r
+> +                pos = skip_space (pos);\r
+\r
+Is it necessary to accept trailing space?\r
+\r
+> +                break;\r
+> +            }\r
+> +        }\r
+> +        *out++ = *pos++;\r
+> +    }\r
+> +    /* Did the term terminate without a closing quote or is there\r
+> +     * trailing text after the closing quote? */\r
+> +    if (!closed || *pos)\r
+> +        goto FAIL;\r
+> +    *out = '\0';\r
+> +    } else {\r
+> +    const char *start = pos;\r
+> +    /* Check for text after the boolean term. */\r
+> +    while (*pos > ' ' && *pos != ')')\r
+\r
+The condition could have *pos there too for clarity, though not strictly\r
+necessary. Would be neat to have a ctype style helper that could be\r
+shared between this and make_boolean_term.\r
+\r
+> +        ++pos;\r
+> +    if (*skip_space (pos))\r
+\r
+Is it necessary to accept trailing space?\r
+\r
+> +        goto FAIL;\r
+> +    /* No trailing text; dup the string so the caller can free\r
+> +     * it. */\r
+> +    *term_out = talloc_strndup (ctx, start, pos - start);\r
+> +    }\r
+> +    return 0;\r
+> +\r
+> + FAIL:\r
+> +    talloc_free (*prefix_out);\r
+> +    talloc_free (*term_out);\r
+> +    return 1;\r
+> +}\r
+> diff --git a/util/string-util.h b/util/string-util.h\r
+> index b8844a3..8b9fe50 100644\r
+> --- a/util/string-util.h\r
+> +++ b/util/string-util.h\r
+> @@ -33,4 +33,19 @@ char *strtok_len (char *s, const char *delim, size_t *len);\r
+>  int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
+>                     char **buf, size_t *len);\r
+>  \r
+> +/* Parse a boolean term query consisting of a prefix, a colon, and a\r
+> + * term that may be quoted as described for make_boolean_term.  If the\r
+> + * term is not quoted, then it ends at the first whitespace or close\r
+> + * parenthesis.  str may containing leading or trailing whitespace,\r
+> + * but anything else is considered a parse error.  This is compatible\r
+> + * with anything produced by make_boolean_term, and supports a subset\r
+> + * of the quoting styles supported by Xapian (and hence notmuch).\r
+> + * *prefix_out and *term_out will be talloc'd with context ctx.\r
+> + *\r
+> + * Return: 0 on success, non-zero on parse error.\r
+> + */\r
+> +int\r
+> +parse_boolean_term (void *ctx, const char *str,\r
+> +                char **prefix_out, char **term_out);\r
+> +\r
+>  #endif\r
+> -- \r
+> 1.7.10.4\r