[PATCH v2 2/5] util: Function to parse boolean term queries

2012-12-26 Thread David Bremner
Austin Clements  writes:

> + char *out = talloc_strdup (ctx, pos + 1);
> + int closed = 0;
> + /* Find the closing quote and un-double doubled internal
> +  * quotes. */
> + for (pos = *term_out = out; *pos; ) {

Since you strdup anyway, wouldn't it be easier to understand if pos read
from the input string and out wrote to term_out? Something like
(untested) 

index db01b4b..e4157d0 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -112,11 +112,12 @@ parse_boolean_term (void *ctx, const char *str,

 /* Implement de-quoting compatible with make_boolean_term. */
 if (*pos == '"') {
-   char *out = talloc_strdup (ctx, pos + 1);
+   char *out;
int closed = 0;
+   *term_out= talloc_strdup (ctx, pos + 1);
/* Find the closing quote and un-double doubled internal
 * quotes. */
-   for (pos = *term_out = out; *pos; ) {
+   for (out = *term_out; *pos; ) {
if (*pos == '"') {
++pos;


Perhaps the two talloc_strdups can even be unified, but I wouldn't worry
too much about that.


[PATCH v2 2/5] util: Function to parse boolean term queries

2012-12-25 Thread Austin Clements
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 |   51 +++
 util/string-util.h |   11 +++
 2 files changed, 62 insertions(+)

diff --git a/util/string-util.c b/util/string-util.c
index e4bea21..db01b4b 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -96,3 +96,54 @@ make_boolean_term (void *ctx, const char *prefix, const char 
*term,

 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 de-quoting compatible with make_boolean_term. */
+if (*pos == '"') {
+   char *out = talloc_strdup (ctx, pos + 1);
+   int closed = 0;
+   /* Find the closing quote and un-double doubled internal
+* quotes. */
+   for (pos = *term_out = out; *pos; ) {
+   if (*pos == '"') {
+   ++pos;
+   if (*pos != '"') {
+   /* Found the closing quote. */
+   closed = 1;
+   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 {
+   *term_out = talloc_strdup (ctx, pos);
+   /* Check for text after the boolean term. */
+   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..aff2d65 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 produced by make_boolean_term, 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



[PATCH v2 2/5] util: Function to parse boolean term queries

2012-12-25 Thread Austin Clements
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 |   51 +++
 util/string-util.h |   11 +++
 2 files changed, 62 insertions(+)

diff --git a/util/string-util.c b/util/string-util.c
index e4bea21..db01b4b 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -96,3 +96,54 @@ make_boolean_term (void *ctx, const char *prefix, const char 
*term,
 
 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 de-quoting compatible with make_boolean_term. */
+if (*pos == '') {
+   char *out = talloc_strdup (ctx, pos + 1);
+   int closed = 0;
+   /* Find the closing quote and un-double doubled internal
+* quotes. */
+   for (pos = *term_out = out; *pos; ) {
+   if (*pos == '') {
+   ++pos;
+   if (*pos != '') {
+   /* Found the closing quote. */
+   closed = 1;
+   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 {
+   *term_out = talloc_strdup (ctx, pos);
+   /* Check for text after the boolean term. */
+   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..aff2d65 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 produced by make_boolean_term, 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

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch