--- /dev/null
+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 95FF4431FBD\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Dec 2012 05:08:48 -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 PqYuMMu7VhmA for <notmuch@notmuchmail.org>;\r
+ Fri, 21 Dec 2012 05:08:47 -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 57533431FD8\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Dec 2012 05:08:39 -0800 (PST)\r
+Received: from fctnnbsc30w-156034082078.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+ ([156.34.82.78] 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 1Tm2L6-0005da-Ai; Fri, 21 Dec 2012 09:08:36 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Tm2L0-0005yS-S5; Fri, 21 Dec 2012 09:08:30 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v8 04/18] notmuch-tag: factor out double quoting routine\r
+Date: Fri, 21 Dec 2012 09:08:13 -0400\r
+Message-Id: <1356095307-22895-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1356095307-22895-1-git-send-email-david@tethera.net>\r
+References: <1356095307-22895-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, 21 Dec 2012 13:08:49 -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 | 52 ++++++++++++++++++----------------------------------\r
+ util/string-util.c | 37 +++++++++++++++++++++++++++++++++++++\r
+ util/string-util.h | 8 ++++++++\r
+ 3 files changed, 63 insertions(+), 34 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 0965ee7..a480215 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,46 @@ _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
++ /* 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
++ /* XXX in case of OOM, query_string will be deallocated when\r
++ * ctx is, which might be at shutdown */\r
++ if (double_quote_str (ctx,\r
++ tag_op_list_tag (list, i),\r
++ &escaped, &escaped_len))\r
++ return NULL;\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..b9039f4 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,39 @@ 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 ((*buf == NULL) || (needed > *len)) {\r
++ *len = 2 * needed;\r
++ *buf = talloc_realloc (ctx, *buf, char, *len);\r
++ }\r
++\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..4fc7942 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 memory allocation 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