[PATCH 2/5] util: Function to parse boolean term queries
authorAustin Clements <amdragon@MIT.EDU>
Tue, 25 Dec 2012 05:57:53 +0000 (00:57 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:43 +0000 (09:52 -0800)
dd/600f0ea5afb6788c894173be3346446eeb39b7 [new file with mode: 0644]

diff --git a/dd/600f0ea5afb6788c894173be3346446eeb39b7 b/dd/600f0ea5afb6788c894173be3346446eeb39b7
new file mode 100644 (file)
index 0000000..472b135
--- /dev/null
@@ -0,0 +1,174 @@
+Return-Path: <amdragon@mit.edu>\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 9B39B431FBC\r
+       for <notmuch@notmuchmail.org>; Mon, 24 Dec 2012 21:58:18 -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 RjDg331+12Vn for <notmuch@notmuchmail.org>;\r
+       Mon, 24 Dec 2012 21:58:17 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
+       [18.9.25.14])\r
+       by olra.theworths.org (Postfix) with ESMTP id 47FAA431FBD\r
+       for <notmuch@notmuchmail.org>; Mon, 24 Dec 2012 21:58:14 -0800 (PST)\r
+X-AuditID: 1209190e-b7fa16d000001402-a4-50d94075fa9d\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id CA.1D.05122.57049D05; Tue, 25 Dec 2012 00:58:13 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qBP5wCIg008904; \r
+       Tue, 25 Dec 2012 00:58:12 -0500\r
+Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net\r
+       [76.21.105.205]) (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qBP5w7AB011705\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Tue, 25 Dec 2012 00:58:10 -0500 (EST)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1TnNWg-0001Xq-K2; Tue, 25 Dec 2012 00:58:06 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/5] util: Function to parse boolean term queries\r
+Date: Tue, 25 Dec 2012 00:57:53 -0500\r
+Message-Id: <1356415076-5692-3-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1356415076-5692-1-git-send-email-amdragon@mit.edu>\r
+References: <1356415076-5692-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrHIsWRmVeSWpSXmKPExsUixG6nrlvqcDPA4Mc/Hosbrd2MFk3TnS1W\r
+       z+WxuH5zJrMDi8fOWXfZPW7df83u8WzVLWaPLYfeMwewRHHZpKTmZJalFunbJXBlLNx3haXg\r
+       m2jFq1m3mBsYDwl0MXJySAiYSDy5dYMRwhaTuHBvPVsXIxeHkMA+Romff44yQTgbGCV+LN7J\r
+       AuFcZJI4NP0yC0iLkMBcRom2JgcQm01AQ2Lb/uVgo0QEpCV23p3NCmIzC+RJPHy0lR3EFhZw\r
+       lPh4cB5YDYuAqkRf11ygdRwcvAL2Estmm0FcoSjR/WwCG4jNKeAgMffPSqhV9hJf525mmcDI\r
+       v4CRYRWjbEpulW5uYmZOcWqybnFyYl5eapGusV5uZoleakrpJkZQsHFK8u1g/HpQ6RCjAAej\r
+       Eg/vxTk3AoRYE8uKK3MPMUpyMCmJ8r62uxkgxJeUn1KZkVicEV9UmpNafIhRgoNZSYTXhAco\r
+       x5uSWFmVWpQPk5LmYFES572SctNfSCA9sSQ1OzW1ILUIJivDwaEkwfsHZKhgUWp6akVaZk4J\r
+       QpqJgxNkOA/QcEl7kOHFBYm5xZnpEPlTjIpS4rymIAkBkERGaR5cLywZvGIUB3pFmFcIpIoH\r
+       mEjgul8BDWYCGhzLdwNkcEkiQkqqgZE/9caMpoRzE8W/Cl/Ij81dtGhL8LfdtVafLqr4zknr\r
+       0Nu1z6xIpE1Ln/VpqoQUv3zXnu7CWyuXv93je+pa6+UC64jcN5O7m3zCWSpzBSWeS6Uzfncu\r
+       v/VdWv7tlCcMTZtuWR41WSfDc3LZLYEZFyLmZCzrmZK7NV5r8VLrO5UZ5s6V9W3xm5VYijMS\r
+       DbWYi4oTAU74pRPhAgAA\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: Tue, 25 Dec 2012 05:58:19 -0000\r
+\r
+This reproduces Xapian's parsing rules for boolean term queries.  This\r
+is provided as a generic string utility, but will be used shortly in\r
+notmuch restore to parse and optimize for ID queries.\r
+---\r
+ util/string-util.c |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/string-util.h |   11 +++++++++\r
+ 2 files changed, 74 insertions(+)\r
+\r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index 161a4dd..eaa6c99 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -94,3 +94,66 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,\r
\r
+     return 0;\r
+ }\r
++\r
++static int\r
++consume_double_quote (const char **str)\r
++{\r
++    if (**str == '"') {\r
++      ++*str;\r
++      return 1;\r
++    } else if (strncmp(*str, "\xe2\x80\x9c", 3) == 0 || /* UTF8 0x201c */\r
++             strncmp(*str, "\xe2\x80\x9d", 3) == 0) { /* UTF8 0x201d */\r
++      *str += 3;\r
++      return 3;\r
++    } else {\r
++      return 0;\r
++    }\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 Xapian's boolean term de-quoting.  This is a nearly\r
++     * direct translation of QueryParser::Internal::parse_query. */\r
++    pos = *term_out = talloc_strdup (ctx, pos);\r
++    if (consume_double_quote (&pos)) {\r
++      char *out = talloc_strdup (ctx, pos);\r
++      pos = *term_out = out;\r
++      while (1) {\r
++          if (! *pos) {\r
++              /* Premature end of string */\r
++              goto FAIL;\r
++          } else if (*pos == '"') {\r
++              if (*++pos != '"')\r
++                  break;\r
++          } else if (consume_double_quote (&pos)) {\r
++              break;\r
++          }\r
++          *out++ = *pos++;\r
++      }\r
++      if (*pos)\r
++          goto FAIL;\r
++      *out = '\0';\r
++    } else {\r
++      while (*pos > ' ' && *pos != ')')\r
++          ++pos;\r
++      if (*pos)\r
++          goto FAIL;\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 7475e2c..e4e4c42 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -28,4 +28,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, returning the prefix in *prefix_out and\r
++ * the term in *term_out.  *prefix_out and *term_out will be talloc'd\r
++ * 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