Re: [Patch v7 04/14] notmuch-tag: factor out double quoting routine
authorJani Nikula <jani@nikula.org>
Sat, 15 Dec 2012 22:20:42 +0000 (00:20 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:20 +0000 (09:52 -0800)
9b/2d7c5dd144006c537ee5b9a3420b6cdfbcab6d [new file with mode: 0644]

diff --git a/9b/2d7c5dd144006c537ee5b9a3420b6cdfbcab6d b/9b/2d7c5dd144006c537ee5b9a3420b6cdfbcab6d
new file mode 100644 (file)
index 0000000..b0056dd
--- /dev/null
@@ -0,0 +1,266 @@
+Return-Path: <jani@nikula.org>\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 402E3431FBC\r
+       for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:51 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 DpbKc0TzkIEn for <notmuch@notmuchmail.org>;\r
+       Sat, 15 Dec 2012 14:20:49 -0800 (PST)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+       [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 53C8D431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:49 -0800 (PST)\r
+Received: by mail-lb0-f181.google.com with SMTP id ge1so3662168lbb.26\r
+       for <notmuch@notmuchmail.org>; Sat, 15 Dec 2012 14:20:47 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:cc:subject:in-reply-to:references:user-agent:date\r
+       :message-id:mime-version:content-type:x-gm-message-state;\r
+       bh=PLnt+C2UQKPR8q12fFktFhDO5bw/oqGtj4oRH1Ud4V8=;\r
+       b=bfmkqzd/TvyoYNFWuzqQ2aroZfKHKU/mlTwqi/UINi2WUcsgrUMRYGhlvrF/bNT2fe\r
+       In+F++xbmaIQw8djPbVSCZN7ooFjoYT6T9iAD36tmexGD3B3ItmoLUvj4VQTwyb33vDY\r
+       t0naeOaveAhxaGAPJlUonLcwxVJiSohySTH5/h56i4toMymHVGG12UR8+m6FsRYPLAui\r
+       XLflMss2HdFyKpgvxLFW/nE+66KTrGADyBCNwwAPCsjDnLn1GLmgTiBnDUELTCx4q7Uz\r
+       Qjx0K4RboOeOe3l5gx7eoSSLg7QNGGRHx7JGNCvUPloLm4x3sJxiXb3mGGBWXj9yTU1p\r
+       N6Cw==\r
+Received: by 10.112.50.43 with SMTP id z11mr3984586lbn.36.1355610046340;\r
+       Sat, 15 Dec 2012 14:20:46 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id fb1sm3247399lbb.15.2012.12.15.14.20.43\r
+       (version=SSLv3 cipher=OTHER); Sat, 15 Dec 2012 14:20:44 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: david@tethera.net, notmuch@notmuchmail.org\r
+Subject: Re: [Patch v7 04/14] notmuch-tag: factor out double quoting routine\r
+In-Reply-To: <1355492062-7546-5-git-send-email-david@tethera.net>\r
+References: <1355492062-7546-1-git-send-email-david@tethera.net>\r
+       <1355492062-7546-5-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.14+138~g7041c56 (http://notmuchmail.org) Emacs/23.4.1\r
+       (i686-pc-linux-gnu)\r
+Date: Sun, 16 Dec 2012 00:20:42 +0200\r
+Message-ID: <87zk1fot39.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQlvGOEE9k1xDPF+Wk/Oxa/6vrQm4o74sMNA/RWPvneQWBmnF5SgQng57vSMJs3RXjlvIdOD\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: Sat, 15 Dec 2012 22:20:51 -0000\r
+\r
+On Fri, 14 Dec 2012, david@tethera.net wrote:\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      |   50 ++++++++++++++++----------------------------------\r
+>  util/string-util.c |   34 ++++++++++++++++++++++++++++++++++\r
+>  util/string-util.h |    8 ++++++++\r
+>  3 files changed, 58 insertions(+), 34 deletions(-)\r
+>\r
+> diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> index 0965ee7..13f2268 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,44 @@ _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
+> +\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
+> +    double_quote_str (ctx,\r
+> +                      tag_op_list_tag (list, i),\r
+> +                      &escaped, &escaped_len);\r
+\r
+Check return value?\r
+\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..ea7c25b 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,36 @@ 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 (needed > *len)\r
+> +    *buf = talloc_realloc (ctx, *buf, char, 2*needed);\r
+\r
+You fail to set *len to 2*needed, leading to doing realloc every time.\r
+\r
+Also, I think you should follow the getline pattern like you did in\r
+hex_encode: if *buf == NULL, the input value of *len is ignored.\r
+\r
+BR,\r
+Jani.\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..b593bc7 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 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
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r