From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/5] util: Function to parse boolean term queries
Date: Tue, 25 Dec 2012 00:57:53 -0500 [thread overview]
Message-ID: <1356415076-5692-3-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1356415076-5692-1-git-send-email-amdragon@mit.edu>
This reproduces Xapian's parsing rules for boolean term queries. This
is provided as a generic string utility, but will be used shortly in
notmuch restore to parse and optimize for ID queries.
---
util/string-util.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
util/string-util.h | 11 +++++++++
2 files changed, 74 insertions(+)
diff --git a/util/string-util.c b/util/string-util.c
index 161a4dd..eaa6c99 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -94,3 +94,66 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,
return 0;
}
+
+static int
+consume_double_quote (const char **str)
+{
+ if (**str == '"') {
+ ++*str;
+ return 1;
+ } else if (strncmp(*str, "\xe2\x80\x9c", 3) == 0 || /* UTF8 0x201c */
+ strncmp(*str, "\xe2\x80\x9d", 3) == 0) { /* UTF8 0x201d */
+ *str += 3;
+ return 3;
+ } else {
+ return 0;
+ }
+}
+
+int
+parse_boolean_term (void *ctx, const char *str,
+ char **prefix_out, char **term_out)
+{
+ *prefix_out = *term_out = NULL;
+
+ /* Parse prefix */
+ const char *pos = strchr (str, ':');
+ if (! pos)
+ goto FAIL;
+ *prefix_out = talloc_strndup (ctx, str, pos - str);
+ ++pos;
+
+ /* Implement Xapian's boolean term de-quoting. This is a nearly
+ * direct translation of QueryParser::Internal::parse_query. */
+ pos = *term_out = talloc_strdup (ctx, pos);
+ if (consume_double_quote (&pos)) {
+ char *out = talloc_strdup (ctx, pos);
+ pos = *term_out = out;
+ while (1) {
+ if (! *pos) {
+ /* Premature end of string */
+ goto FAIL;
+ } else if (*pos == '"') {
+ if (*++pos != '"')
+ break;
+ } else if (consume_double_quote (&pos)) {
+ break;
+ }
+ *out++ = *pos++;
+ }
+ if (*pos)
+ goto FAIL;
+ *out = '\0';
+ } else {
+ while (*pos > ' ' && *pos != ')')
+ ++pos;
+ if (*pos)
+ goto FAIL;
+ }
+ return 0;
+
+ FAIL:
+ talloc_free (*prefix_out);
+ talloc_free (*term_out);
+ return 1;
+}
diff --git a/util/string-util.h b/util/string-util.h
index 7475e2c..e4e4c42 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -28,4 +28,15 @@ char *strtok_len (char *s, const char *delim, size_t *len);
int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,
char **buf, size_t *len);
+/* Parse a boolean term query, returning the prefix in *prefix_out and
+ * the term in *term_out. *prefix_out and *term_out will be talloc'd
+ * with context ctx.
+ *
+ * Return: 0 on success, non-zero on parse error (including trailing
+ * data in str).
+ */
+int
+parse_boolean_term (void *ctx, const char *str,
+ char **prefix_out, char **term_out);
+
#endif
--
1.7.10.4
next prev parent reply other threads:[~2012-12-25 5:58 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-25 5:57 [PATCH 0/5] Use Xapian query syntax for batch-tag dump/restore Austin Clements
2012-12-25 5:57 ` [PATCH 1/5] util: Factor out boolean term quoting routine Austin Clements
2012-12-25 12:25 ` David Bremner
2012-12-25 5:57 ` Austin Clements [this message]
2012-12-25 14:18 ` [PATCH 2/5] util: Function to parse boolean term queries David Bremner
2012-12-25 14:34 ` David Bremner
2012-12-26 1:23 ` Austin Clements
2012-12-25 5:57 ` [PATCH 3/5] dump: Disallow \n in message IDs Austin Clements
2012-12-25 14:21 ` David Bremner
2012-12-25 5:57 ` [PATCH 4/5] dump/restore: Use Xapian queries for batch-tag format Austin Clements
2012-12-25 5:57 ` [PATCH 5/5] man: Update notmuch-dump(1) for new " Austin Clements
2012-12-25 14:47 ` David Bremner
2012-12-25 15:18 ` David Bremner
2012-12-25 18:05 ` David Bremner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1356415076-5692-3-git-send-email-amdragon@mit.edu \
--to=amdragon@mit.edu \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).