From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Cc: tomi.ollila@iki.fi
Subject: [PATCH v4 2/5] util: Function to parse boolean term queries
Date: Mon, 31 Dec 2012 01:42:39 -0500 [thread overview]
Message-ID: <1356936162-2589-3-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1356936162-2589-1-git-send-email-amdragon@mit.edu>
This parses the subset of Xapian's boolean term quoting rules that are
used by make_boolean_term. 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 | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++
util/string-util.h | 15 ++++++++++++
2 files changed, 82 insertions(+)
diff --git a/util/string-util.c b/util/string-util.c
index e4bea21..52c7781 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -22,6 +22,8 @@
#include "string-util.h"
#include "talloc.h"
+#include <ctype.h>
+
char *
strtok_len (char *s, const char *delim, size_t *len)
{
@@ -96,3 +98,68 @@ make_boolean_term (void *ctx, const char *prefix, const char *term,
return 0;
}
+
+static const char*
+skip_space (const char *str)
+{
+ while (*str && isspace (*str))
+ ++str;
+ return str;
+}
+
+int
+parse_boolean_term (void *ctx, const char *str,
+ char **prefix_out, char **term_out)
+{
+ *prefix_out = *term_out = NULL;
+
+ /* Parse prefix */
+ str = skip_space (str);
+ const char *pos = strchr (str, ':');
+ if (! pos)
+ goto FAIL;
+ *prefix_out = talloc_strndup (ctx, str, pos - str);
+ ++pos;
+
+ /* Implement de-quoting compatible with make_boolean_term. */
+ if (*pos == '"') {
+ char *out = talloc_array (ctx, char, strlen (pos));
+ int closed = 0;
+ *term_out = out;
+ /* Skip the opening quote, find the closing quote, and
+ * un-double doubled internal quotes. */
+ for (++pos; *pos; ) {
+ if (*pos == '"') {
+ ++pos;
+ if (*pos != '"') {
+ /* Found the closing quote. */
+ closed = 1;
+ pos = skip_space (pos);
+ break;
+ }
+ }
+ *out++ = *pos++;
+ }
+ /* Did the term terminate without a closing quote or is there
+ * trailing text after the closing quote? */
+ if (!closed || *pos)
+ goto FAIL;
+ *out = '\0';
+ } else {
+ const char *start = pos;
+ /* Check for text after the boolean term. */
+ while (*pos > ' ' && *pos != ')')
+ ++pos;
+ if (*skip_space (pos))
+ goto FAIL;
+ /* No trailing text; dup the string so the caller can free
+ * it. */
+ *term_out = talloc_strndup (ctx, start, pos - start);
+ }
+ 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 b8844a3..8b9fe50 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -33,4 +33,19 @@ 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 consisting of a prefix, a colon, and a
+ * term that may be quoted as described for make_boolean_term. If the
+ * term is not quoted, then it ends at the first whitespace or close
+ * parenthesis. str may containing leading or trailing whitespace,
+ * but anything else is considered a parse error. This is compatible
+ * with anything produced by make_boolean_term, and supports a subset
+ * of the quoting styles supported by Xapian (and hence notmuch).
+ * *prefix_out and *term_out will be talloc'd with context ctx.
+ *
+ * Return: 0 on success, non-zero on parse error.
+ */
+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-31 6:42 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-31 6:42 [PATCH v4 0/5] Use Xapian query syntax for batch-tag dump/restore Austin Clements
2012-12-31 6:42 ` [PATCH v4 1/5] util: Factor out boolean term quoting routine Austin Clements
2013-01-03 16:48 ` Jani Nikula
2013-01-04 7:26 ` Austin Clements
2012-12-31 6:42 ` Austin Clements [this message]
2012-12-31 12:01 ` [PATCH v4 2/5] util: Function to parse boolean term queries Mark Walters
2013-01-03 17:09 ` Jani Nikula
2012-12-31 6:42 ` [PATCH v4 3/5] dump: Disallow \n in message IDs Austin Clements
2013-01-03 17:19 ` Jani Nikula
2012-12-31 6:42 ` [PATCH v4 4/5] dump/restore: Use Xapian queries for batch-tag format Austin Clements
2012-12-31 12:41 ` Mark Walters
2012-12-31 16:52 ` David Bremner
2012-12-31 6:42 ` [PATCH v4 5/5] man: Update notmuch-dump(1) and notmuch-restore(1) Austin Clements
2012-12-31 22:14 ` [PATCH v4 0/5] Use Xapian query syntax for batch-tag dump/restore Tomi Ollila
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=1356936162-2589-3-git-send-email-amdragon@mit.edu \
--to=amdragon@mit.edu \
--cc=notmuch@notmuchmail.org \
--cc=tomi.ollila@iki.fi \
/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).