--- /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 arlo.cworth.org (Postfix) with ESMTP id C608D6DE0231\r
+ for <notmuch@notmuchmail.org>; Sun, 22 May 2016 07:29:19 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.012\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
+ tests=[AWL=-0.001, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id XVd3_frxp_4y for <notmuch@notmuchmail.org>;\r
+ Sun, 22 May 2016 07:29:11 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 652246DE0222\r
+ for <notmuch@notmuchmail.org>; Sun, 22 May 2016 07:29:10 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1b4UNK-00051Q-Ct; Sun, 22 May 2016 10:29:02 -0400\r
+Received: (nullmailer pid 5517 invoked by uid 1000);\r
+ Sun, 22 May 2016 14:29:09 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC patch 1/2] lib: refactor _notmuch_message_has_term\r
+Date: Sun, 22 May 2016 11:28:58 -0300\r
+Message-Id: <1463927339-5441-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1463927339-5441-1-git-send-email-david@tethera.net>\r
+References: <1463927339-5441-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 22 May 2016 14:29:19 -0000\r
+\r
+With essentially the same code path, we can provide a key/value lookup\r
+facility, for future use.\r
+---\r
+ lib/message.cc | 68 ++++++++++++++++++++++++++++++++++++++++-----------\r
+ lib/notmuch-private.h | 6 +++++\r
+ 2 files changed, 60 insertions(+), 14 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 6839305..90c2bb2 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1263,36 +1263,76 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
+ return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
+ }\r
+ \r
++/*\r
++ * look for a term with given prefix. If value != NULL, return the\r
++ * suffix of the first such term. If suffix == NULL, consider any\r
++ * non-empty suffix a mismatch\r
++*/\r
++notmuch_private_status_t\r
++_notmuch_message_get_prefixed_term (notmuch_message_t *message,\r
++ const char *prefix,\r
++ const char **value)\r
++{\r
++ notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++\r
++ if (strlen (prefix) > NOTMUCH_TERM_MAX)\r
++ return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;\r
++\r
++ try {\r
++ /* Look for prefixed term */\r
++ Xapian::TermIterator i = message->doc.termlist_begin ();\r
++ i.skip_to (prefix);\r
++ if ((i == message->doc.termlist_end ()) ||\r
++ (strncmp (prefix, (*i).c_str (), strlen(prefix)) != 0)) {\r
++ status = NOTMUCH_PRIVATE_STATUS_NO_TERM_FOUND;\r
++ } else {\r
++ std::string suffix = (*i).substr (strlen (prefix));\r
++ if (value) {\r
++ *value = talloc_strdup (message, suffix.c_str ());\r
++ } else {\r
++ /* non-exact match considered failure */\r
++ if (suffix.length () > 0)\r
++ status = NOTMUCH_PRIVATE_STATUS_NO_TERM_FOUND;\r
++ }\r
++ }\r
++ } catch (Xapian::Error &error) {\r
++ status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
++ }\r
++\r
++ return status;\r
++}\r
++\r
+ notmuch_private_status_t\r
+ _notmuch_message_has_term (notmuch_message_t *message,\r
+ const char *prefix_name,\r
+ const char *value,\r
+ notmuch_bool_t *result)\r
+ {\r
++ notmuch_bool_t out = TRUE;\r
++ notmuch_private_status_t status;\r
+ char *term;\r
+- notmuch_bool_t out = FALSE;\r
+- notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
+ \r
+ if (value == NULL)\r
+ return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;\r
+ \r
++\r
+ term = talloc_asprintf (message, "%s%s",\r
+ _find_prefix (prefix_name), value);\r
+ \r
+- if (strlen (term) > NOTMUCH_TERM_MAX)\r
+- return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;\r
++ if (term == NULL) {\r
++ status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
+ \r
+- try {\r
+- /* Look for the exact term */\r
+- Xapian::TermIterator i = message->doc.termlist_begin ();\r
+- i.skip_to (term);\r
+- if (i != message->doc.termlist_end () &&\r
+- !strcmp ((*i).c_str (), term))\r
+- out = TRUE;\r
+- } catch (Xapian::Error &error) {\r
+- status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
++ status = _notmuch_message_get_prefixed_term (message, term, NULL);\r
++ if (status == NOTMUCH_PRIVATE_STATUS_NO_TERM_FOUND) {\r
++ status = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++ out = FALSE;\r
+ }\r
+- talloc_free (term);\r
++\r
++ DONE:\r
++ if (term)\r
++ talloc_free (term);\r
+ \r
+ *result = out;\r
+ return status;\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 9280797..f7eba4a 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -141,6 +141,7 @@ typedef enum _notmuch_private_status {\r
+ /* Then add our own private values. */\r
+ NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,\r
+ NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,\r
++ NOTMUCH_PRIVATE_STATUS_NO_TERM_FOUND,\r
+ \r
+ NOTMUCH_PRIVATE_STATUS_LAST_STATUS\r
+ } notmuch_private_status_t;\r
+@@ -333,6 +334,11 @@ _notmuch_message_initialize_ghost (notmuch_message_t *message,\r
+ void\r
+ _notmuch_message_close (notmuch_message_t *message);\r
+ \r
++notmuch_private_status_t\r
++_notmuch_message_get_prefixed_term (notmuch_message_t *message,\r
++ const char *prefix,\r
++ const char **suffix);\r
++\r
+ /* Get a copy of the data in this message document.\r
+ *\r
+ * Caller should talloc_free the result when done.\r
+-- \r
+2.8.1\r
+\r