--- /dev/null
+Return-Path: <amdragon@mit.edu>\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 BD101429E3A\r
+ for <notmuch@notmuchmail.org>; Thu, 2 Aug 2012 18:15:10 -0700 (PDT)\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 HuF7U1DehA9G for <notmuch@notmuchmail.org>;\r
+ Thu, 2 Aug 2012 18:15:09 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
+ [18.7.68.37])\r
+ by olra.theworths.org (Postfix) with ESMTP id 1B500431E64\r
+ for <notmuch@notmuchmail.org>; Thu, 2 Aug 2012 18:15:08 -0700 (PDT)\r
+X-AuditID: 12074425-b7f9b6d0000008c4-ba-501b261b242f\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+ by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id D8.BD.02244.B162B105; Thu, 2 Aug 2012 21:15:07 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q731F6dw012765; \r
+ Thu, 2 Aug 2012 21:15:06 -0400\r
+Received: from drake.dyndns.org\r
+ (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [209.6.116.242]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q731F4Hj002786\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Thu, 2 Aug 2012 21:15:05 -0400 (EDT)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1Sx6To-0003wE-3m; Thu, 02 Aug 2012 21:15:04 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 03/13] sprinter: Add a string_len method\r
+Date: Thu, 2 Aug 2012 21:14:49 -0400\r
+Message-Id: <1343956499-14543-4-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1343956499-14543-1-git-send-email-amdragon@mit.edu>\r
+References: <1343956499-14543-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrKIsWRmVeSWpSXmKPExsUixCmqrCutJh1g8GGPrMXquTwW12/OZLZ4\r
+ s3IeqwOzx85Zd9k9Dn9dyOLxbNUt5gDmKC6blNSczLLUIn27BK6MWU39jAWbFCq2TL3N0sA4\r
+ UbKLkZNDQsBE4vrdT2wQtpjEhXvrgWwuDiGBfYwSh570MIEkhATWM0o8mW8CkXjIJLHl2Guo\r
+ qrmMEp+nvgdrZxPQkNi2fzkjiC0iIC2x8+5sVhCbWSBOYsuU/2BxYQFriVc7L4BNZRFQlZiy\r
+ 6jtQDQcHr4CDxNP+IIgr5CWe3u8DG8kp4Cjx+u0rRogjHCSWt3WzTmDkX8DIsIpRNiW3Sjc3\r
+ MTOnODVZtzg5MS8vtUjXQi83s0QvNaV0EyMotNhdVHcwTjikdIhRgINRiYfXSFI6QIg1say4\r
+ MvcQoyQHk5IoL7MSUIgvKT+lMiOxOCO+qDQntfgQowQHs5IIb9USqQAh3pTEyqrUonyYlDQH\r
+ i5I4742Um/5CAumJJanZqakFqUUwWRkODiUJXllVoKGCRanpqRVpmTklCGkmDk6Q4TxAw+1B\r
+ aniLCxJzizPTIfKnGBWlxHnZQRICIImM0jy4Xljsv2IUB3pFmPeCClAVDzBtwHW/AhrMBDTY\r
+ zgzk6uKSRISUVANj7tqWCXVL/zCqaHrcUz/gLvzTMjqj68EmbcemWwWtUufc+h9p7ruTW3Zb\r
+ fmvRQ7OFF1p4bO/lN3qIpGR7Cy/Q6pM7tn3xi2VRsndLb+gI759YGvxPR/BHhJ2OsAyzTfkt\r
+ l8PnjX0sCz6tK5si6/E8cFVNdZ57Iu+2N/7s39j1I/7sP7Vk91UlluKMREMt5qLiRAA73IiM\r
+ 2AIAAA==\r
+Cc: tomi.ollila@iki.fi\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: Fri, 03 Aug 2012 01:15:11 -0000\r
+\r
+This method allows callers to output strings with specific lengths.\r
+It's useful both for strings with embedded NULs (which JSON can\r
+represent, though parser support is apparently spotty), and\r
+non-terminated strings.\r
+---\r
+ sprinter-json.c | 16 ++++++++++++++--\r
+ sprinter-text.c | 11 +++++++++--\r
+ sprinter.h | 9 ++++++---\r
+ 3 files changed, 29 insertions(+), 7 deletions(-)\r
+\r
+diff --git a/sprinter-json.c b/sprinter-json.c\r
+index 4649655..c9b6835 100644\r
+--- a/sprinter-json.c\r
++++ b/sprinter-json.c\r
+@@ -88,8 +88,13 @@ json_end (struct sprinter *sp)\r
+ fputc ('\n', spj->stream);\r
+ }\r
+ \r
++/* This implementation supports embedded NULs as allowed by the JSON\r
++ * specification and Unicode. Support for *parsing* embedded NULs\r
++ * varies, but is generally not a problem outside of C-based parsers\r
++ * (Python's json module and Emacs' json.el take embedded NULs in\r
++ * stride). */\r
+ static void\r
+-json_string (struct sprinter *sp, const char *val)\r
++json_string_len (struct sprinter *sp, const char *val, size_t len)\r
+ {\r
+ static const char *const escapes[] = {\r
+ ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
+@@ -98,7 +103,7 @@ json_string (struct sprinter *sp, const char *val)\r
+ struct sprinter_json *spj = json_begin_value (sp);\r
+ \r
+ fputc ('"', spj->stream);\r
+- for (; *val; ++val) {\r
++ for (; len; ++val, --len) {\r
+ unsigned char ch = *val;\r
+ if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
+ fputs (escapes[ch], spj->stream);\r
+@@ -111,6 +116,12 @@ json_string (struct sprinter *sp, const char *val)\r
+ }\r
+ \r
+ static void\r
++json_string (struct sprinter *sp, const char *val)\r
++{\r
++ json_string_len (sp, val, strlen (val));\r
++}\r
++\r
++static void\r
+ json_integer (struct sprinter *sp, int val)\r
+ {\r
+ struct sprinter_json *spj = json_begin_value (sp);\r
+@@ -166,6 +177,7 @@ sprinter_json_create (const void *ctx, FILE *stream)\r
+ .begin_list = json_begin_list,\r
+ .end = json_end,\r
+ .string = json_string,\r
++ .string_len = json_string_len,\r
+ .integer = json_integer,\r
+ .boolean = json_boolean,\r
+ .null = json_null,\r
+diff --git a/sprinter-text.c b/sprinter-text.c\r
+index b208840..dfa54b5 100644\r
+--- a/sprinter-text.c\r
++++ b/sprinter-text.c\r
+@@ -25,14 +25,20 @@ struct sprinter_text {\r
+ };\r
+ \r
+ static void\r
+-text_string (struct sprinter *sp, const char *val)\r
++text_string_len (struct sprinter *sp, const char *val, size_t len)\r
+ {\r
+ struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+ \r
+ if (sptxt->current_prefix != NULL)\r
+ fprintf (sptxt->stream, "%s:", sptxt->current_prefix);\r
+ \r
+- fputs(val, sptxt->stream);\r
++ fwrite (val, len, 1, sptxt->stream);\r
++}\r
++\r
++static void\r
++text_string (struct sprinter *sp, const char *val)\r
++{\r
++ text_string_len (sp, val, strlen (val));\r
+ }\r
+ \r
+ static void\r
+@@ -105,6 +111,7 @@ sprinter_text_create (const void *ctx, FILE *stream)\r
+ .begin_list = text_begin_list,\r
+ .end = text_end,\r
+ .string = text_string,\r
++ .string_len = text_string_len,\r
+ .integer = text_integer,\r
+ .boolean = text_boolean,\r
+ .null = text_null,\r
+diff --git a/sprinter.h b/sprinter.h\r
+index 6680d41..5f43175 100644\r
+--- a/sprinter.h\r
++++ b/sprinter.h\r
+@@ -23,11 +23,14 @@ typedef struct sprinter {\r
+ */\r
+ void (*end) (struct sprinter *);\r
+ \r
+- /* Print one string/integer/boolean/null element (possibly inside a\r
+- * list or map, followed or preceded by separators).\r
+- * For string, the char * must be UTF-8 encoded.\r
++ /* Print one string/integer/boolean/null element (possibly inside\r
++ * a list or map, followed or preceded by separators). For string\r
++ * and string_len, the char * must be UTF-8 encoded. string_len\r
++ * allows non-terminated strings and strings with embedded NULs\r
++ * (though the handling of the latter is format-dependent).\r
+ */\r
+ void (*string) (struct sprinter *, const char *);\r
++ void (*string_len) (struct sprinter *, const char *, size_t);\r
+ void (*integer) (struct sprinter *, int);\r
+ void (*boolean) (struct sprinter *, notmuch_bool_t);\r
+ void (*null) (struct sprinter *);\r
+-- \r
+1.7.10\r
+\r