[Patch v7 04/14] notmuch-tag: factor out double quoting routine
authordavid <david@tethera.net>
Fri, 14 Dec 2012 13:34:12 +0000 (09:34 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:09 +0000 (09:52 -0800)
6a/31fa4147c51e8452a11e7d6f47d4e217d507c1 [new file with mode: 0644]

diff --git a/6a/31fa4147c51e8452a11e7d6f47d4e217d507c1 b/6a/31fa4147c51e8452a11e7d6f47d4e217d507c1
new file mode 100644 (file)
index 0000000..3b9c71e
--- /dev/null
@@ -0,0 +1,234 @@
+Return-Path: <bremner@tethera.net>\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 86787431FDC\r
+       for <notmuch@notmuchmail.org>; Fri, 14 Dec 2012 05:34:54 -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 nKbxWROMvIpo for <notmuch@notmuchmail.org>;\r
+       Fri, 14 Dec 2012 05:34:52 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+       (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 51B36431FC9\r
+       for <notmuch@notmuchmail.org>; Fri, 14 Dec 2012 05:34:52 -0800 (PST)\r
+Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+       ([142.167.90.129] helo=zancas.localnet)\r
+       by tesseract.cs.unb.ca with esmtpsa\r
+       (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1TjVPf-0004yj-EU; Fri, 14 Dec 2012 09:34:51 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+       (envelope-from <bremner@tethera.net>)\r
+       id 1TjVPZ-000226-Sw; Fri, 14 Dec 2012 09:34:45 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v7 04/14] notmuch-tag: factor out double quoting routine\r
+Date: Fri, 14 Dec 2012 09:34:12 -0400\r
+Message-Id: <1355492062-7546-5-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1355492062-7546-1-git-send-email-david@tethera.net>\r
+References: <1355492062-7546-1-git-send-email-david@tethera.net>\r
+X-Spam_bar: -\r
+Cc: David Bremner <bremner@debian.org>\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: Fri, 14 Dec 2012 13:34:54 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+This could live in tag-util as well, but it is really nothing specific\r
+to tags (although the conventions are arguable specific to Xapian).\r
+\r
+The API is changed from "caller-allocates" to "readline-like". The scan for\r
+max tag length is pushed down into the double quoting routine.\r
+---\r
+ notmuch-tag.c      |   50 ++++++++++++++++----------------------------------\r
+ util/string-util.c |   34 ++++++++++++++++++++++++++++++++++\r
+ util/string-util.h |    8 ++++++++\r
+ 3 files changed, 58 insertions(+), 34 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 0965ee7..13f2268 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -20,6 +20,7 @@\r
\r
+ #include "notmuch-client.h"\r
+ #include "tag-util.h"\r
++#include "string-util.h"\r
\r
+ static volatile sig_atomic_t interrupted;\r
\r
+@@ -37,25 +38,6 @@ handle_sigint (unused (int sig))\r
+ }\r
\r
+ static char *\r
+-_escape_tag (char *buf, const char *tag)\r
+-{\r
+-    const char *in = tag;\r
+-    char *out = buf;\r
+-\r
+-    /* Boolean terms surrounded by double quotes can contain any\r
+-     * character.  Double quotes are quoted by doubling them. */\r
+-    *out++ = '"';\r
+-    while (*in) {\r
+-      if (*in == '"')\r
+-          *out++ = '"';\r
+-      *out++ = *in++;\r
+-    }\r
+-    *out++ = '"';\r
+-    *out = 0;\r
+-    return buf;\r
+-}\r
+-\r
+-static char *\r
+ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+                    const tag_op_list_t *list)\r
+ {\r
+@@ -67,44 +49,44 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+      * parenthesize and the exclusion part of the query must not use\r
+      * the '-' operator (though the NOT operator is fine). */\r
\r
+-    char *escaped, *query_string;\r
++    char *escaped = NULL;\r
++    size_t escaped_len = 0;\r
++    char *query_string;\r
+     const char *join = "";\r
+     size_t i;\r
+-    unsigned int max_tag_len = 0;\r
\r
+     /* Don't optimize if there are no tag changes. */\r
+     if (tag_op_list_size (list) == 0)\r
+       return talloc_strdup (ctx, orig_query_string);\r
\r
+-    /* Allocate a buffer for escaping tags.  This is large enough to\r
+-     * hold a fully escaped tag with every character doubled plus\r
+-     * enclosing quotes and a NUL. */\r
+-    for (i = 0; i < tag_op_list_size (list); i++)\r
+-      if (strlen (tag_op_list_tag (list, i)) > max_tag_len)\r
+-          max_tag_len = strlen (tag_op_list_tag (list, i));\r
+-\r
+-    escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
+-    if (! escaped)\r
+-      return NULL;\r
+-\r
+     /* Build the new query string */\r
+     if (strcmp (orig_query_string, "*") == 0)\r
+       query_string = talloc_strdup (ctx, "(");\r
+     else\r
+       query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
\r
++\r
++    /* Boolean terms surrounded by double quotes can contain any\r
++     * character.  Double quotes are quoted by doubling them. */\r
++\r
+     for (i = 0; i < tag_op_list_size (list) && query_string; i++) {\r
++      double_quote_str (ctx,\r
++                        tag_op_list_tag (list, i),\r
++                        &escaped, &escaped_len);\r
++\r
+       query_string = talloc_asprintf_append_buffer (\r
+           query_string, "%s%stag:%s", join,\r
+           tag_op_list_isremove (list, i) ? "" : "not ",\r
+-          _escape_tag (escaped, tag_op_list_tag (list, i)));\r
++          escaped);\r
+       join = " or ";\r
+     }\r
\r
+     if (query_string)\r
+       query_string = talloc_strdup_append_buffer (query_string, ")");\r
\r
+-    talloc_free (escaped);\r
++    if (escaped)\r
++      talloc_free (escaped);\r
++\r
+     return query_string;\r
+ }\r
\r
+diff --git a/util/string-util.c b/util/string-util.c\r
+index 44f8cd3..ea7c25b 100644\r
+--- a/util/string-util.c\r
++++ b/util/string-util.c\r
+@@ -20,6 +20,7 @@\r
\r
\r
+ #include "string-util.h"\r
++#include "talloc.h"\r
\r
+ char *\r
+ strtok_len (char *s, const char *delim, size_t *len)\r
+@@ -32,3 +33,36 @@ strtok_len (char *s, const char *delim, size_t *len)\r
\r
+     return *len ? s : NULL;\r
+ }\r
++\r
++\r
++int\r
++double_quote_str (void *ctx, const char *str,\r
++                char **buf, size_t *len)\r
++{\r
++    const char *in;\r
++    char *out;\r
++    size_t needed = 3;\r
++\r
++    for (in = str; *in; in++)\r
++      needed += (*in == '"') ? 2 : 1;\r
++\r
++    if (needed > *len)\r
++      *buf = talloc_realloc (ctx, *buf, char, 2*needed);\r
++\r
++    if (! *buf)\r
++      return 1;\r
++\r
++    out = *buf;\r
++\r
++    *out++ = '"';\r
++    in = str;\r
++    while (*in) {\r
++      if (*in == '"')\r
++          *out++ = '"';\r
++      *out++ = *in++;\r
++    }\r
++    *out++ = '"';\r
++    *out = 0;\r
++\r
++    return 0;\r
++}\r
+diff --git a/util/string-util.h b/util/string-util.h\r
+index ac7676c..b593bc7 100644\r
+--- a/util/string-util.h\r
++++ b/util/string-util.h\r
+@@ -19,4 +19,12 @@\r
\r
+ char *strtok_len (char *s, const char *delim, size_t *len);\r
\r
++/* Copy str to dest, surrounding with double quotes.\r
++ * Any internal double-quotes are doubled, i.e. a"b -> "a""b"\r
++ *\r
++ * Output is into buf; it may be talloc_realloced\r
++ * return 0 on success, non-zero on failure.\r
++ */\r
++int double_quote_str (void *talloc_ctx, const char *str,\r
++                    char **buf, size_t *len);\r
+ #endif\r
+-- \r
+1.7.10.4\r
+\r