Re: [PATCH v3 2/5] util: Function to parse boolean term queries
authorTomi Ollila <tomi.ollila@iki.fi>
Sat, 29 Dec 2012 21:58:21 +0000 (23:58 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:52 +0000 (09:52 -0800)
f3/0a3edc117500ca1c3b7efda55a08da7f7f3b1c [new file with mode: 0644]

diff --git a/f3/0a3edc117500ca1c3b7efda55a08da7f7f3b1c b/f3/0a3edc117500ca1c3b7efda55a08da7f7f3b1c
new file mode 100644 (file)
index 0000000..4b7e950
--- /dev/null
@@ -0,0 +1,173 @@
+Return-Path: <tomi.ollila@iki.fi>\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 D2178431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 29 Dec 2012 13:58:32 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       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 KXkyali8N-EF for <notmuch@notmuchmail.org>;\r
+       Sat, 29 Dec 2012 13:58:31 -0800 (PST)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+       by olra.theworths.org (Postfix) with ESMTP id AEDA6431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 29 Dec 2012 13:58:30 -0800 (PST)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+       by guru.guru-group.fi (Postfix) with ESMTP id 114601000CA;\r
+       Sat, 29 Dec 2012 23:58:22 +0200 (EET)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v3 2/5] util: Function to parse boolean term queries\r
+In-Reply-To: <1356719189-2837-3-git-send-email-amdragon@mit.edu>\r
+References: <1356719189-2837-1-git-send-email-amdragon@mit.edu>\r
+       <1356719189-2837-3-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.14+216~gd58661f (http://notmuchmail.org) Emacs/24.2.1\r
+       (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+       $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+       !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Sat, 29 Dec 2012 23:58:21 +0200\r
+Message-ID: <m2han4sepu.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\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: Sat, 29 Dec 2012 21:58:33 -0000\r
+\r
+On Fri, Dec 28 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+\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 |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  util/string-util.h |   11 +++++++++++\r
+>  2 files changed, 66 insertions(+)\r
+>\r
+> diff --git a/util/string-util.c b/util/string-util.c\r
+> index e4bea21..83b4953 100644\r
+> --- a/util/string-util.c\r
+> +++ b/util/string-util.c\r
+> @@ -96,3 +96,58 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
+>  \r
+>      return 0;\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
+> +    const char *pos = strchr (str, ':');\r
+> +    if (! pos)\r
+> +    goto FAIL;\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
+> +                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
+> +        ++pos;\r
+> +    if (*pos)\r
+> +        goto FAIL;\r
+\r
+Mark pointed out a good case about trailing whitespace -- It would be nice\r
+if the core were lenient for such cases. I personally remember once wasting\r
+hours of work by just failing to notice trailing whitespace in one system\r
+so this subject is sensitive to me...\r
+\r
+Another thing I saw earlyer today: make_boolean_term() checks\r
+\r
+   if (*in <= ' ' || *in == ')' || *in == '"' || (unsigned char)*in > 127)\r
+\r
+but here the check is only\r
+\r
+   while (*pos > ' ' && *pos != ')')\r
+\r
+I wonder whether it matters...\r
+\r
+Everyting else looks good to me.\r
+\r
+\r
+Tomi\r
+\r
+> +    /* No trailing text; dup the string so the caller can free\r
+> +     * it. */\r
+> +    *term_out = talloc_strdup (ctx, 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..43d49d0 100644\r
+> --- a/util/string-util.h\r
+> +++ b/util/string-util.h\r
+> @@ -33,4 +33,15 @@ 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 produced by make_boolean_term, returning\r
+> + * the prefix in *prefix_out and the term in *term_out.  *prefix_out\r
+> + * and *term_out will be talloc'd with context ctx.\r
+> + *\r
+> + * Return: 0 on success, non-zero on parse error (including trailing\r
+> + * data in str).\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
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r