Re: [PATCH 1/2] Add Google Inc. to AUTHORS as a contributor.
[notmuch-archives.git] / 43 / 691561c196991a86db02084dc5d5215ab392b0
1 Return-Path: <amdragon@mit.edu>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 6CF9C431FBC\r
6         for <notmuch@notmuchmail.org>; Fri, 27 Jul 2012 21:29:31 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 4hqMLyE7PfPr for <notmuch@notmuchmail.org>;\r
16         Fri, 27 Jul 2012 21:29:30 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-1.mit.edu (DMZ-MAILSEC-SCANNER-1.MIT.EDU\r
18         [18.9.25.12])\r
19         by olra.theworths.org (Postfix) with ESMTP id 20DFB431FAF\r
20         for <notmuch@notmuchmail.org>; Fri, 27 Jul 2012 21:29:26 -0700 (PDT)\r
21 X-AuditID: 1209190c-b7f806d000006b87-65-50136aa4f71a\r
22 Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
23         by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 42.00.27527.4AA63105; Sat, 28 Jul 2012 00:29:24 -0400 (EDT)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q6S4TNXB017240; \r
27         Sat, 28 Jul 2012 00:29:23 -0400\r
28 Received: from drake.dyndns.org\r
29         (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
30         [209.6.116.242]) (authenticated bits=0)\r
31         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
32         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6S4TKie027354\r
33         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
34         Sat, 28 Jul 2012 00:29:21 -0400 (EDT)\r
35 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
36         (envelope-from <amdragon@mit.edu>)\r
37         id 1SuyeW-0002Wp-DK; Sat, 28 Jul 2012 00:29:20 -0400\r
38 From: Austin Clements <amdragon@MIT.EDU>\r
39 To: notmuch@notmuchmail.org\r
40 Subject: [PATCH v2 03/13] sprinter: Add a string_len method\r
41 Date: Sat, 28 Jul 2012 00:29:04 -0400\r
42 Message-Id: <1343449754-9010-4-git-send-email-amdragon@mit.edu>\r
43 X-Mailer: git-send-email 1.7.10\r
44 In-Reply-To: <1343449754-9010-1-git-send-email-amdragon@mit.edu>\r
45 References: <1343449754-9010-1-git-send-email-amdragon@mit.edu>\r
46 X-Brightmail-Tracker:\r
47  H4sIAAAAAAAAA+NgFjrPIsWRmVeSWpSXmKPExsUixCmqrLskSzjAYF63lsXehnZGi9VzeSyu\r
48         35zJbPFm5TxWBxaPnbPusnss3rSfzePw14UsHs9W3WIOYInisklJzcksSy3St0vgypjV1M9Y\r
49         sEmhYsvU2ywNjBMluxg5OSQETCQW3n3JDmGLSVy4t54NxBYS2Mco8XindhcjF5C9gVHi8/MZ\r
50         bBDOIyaJ/0+WsUA4cxkldk17yQrSwiagIbFt/3JGEFtEQFpi593ZQHEODmaBPInHayVAwsIC\r
51         1hLTF78D28AioCpxduZVZhCbV8Be4tnNFjaIK+Qlnt7vA7M5BRwkNr/8wAxxkb3E7skbWCcw\r
52         8i9gZFjFKJuSW6Wbm5iZU5yarFucnJiXl1qka6iXm1mil5pSuokRHG6SPDsY3xxUOsQowMGo\r
53         xMOrfF0oQIg1say4MvcQoyQHk5Iob1S4cIAQX1J+SmVGYnFGfFFpTmrxIUYJDmYlEd6dgkA5\r
54         3pTEyqrUonyYlDQHi5I47+WUm/5CAumJJanZqakFqUUwWRkODiUJ3u2ZQI2CRanpqRVpmTkl\r
55         CGkmDk6Q4TxAw9eA1PAWFyTmFmemQ+RPMSpKifNOAkkIgCQySvPgemHp4BWjONArwrzXQKp4\r
56         gKkErvsV0GAmoMEW0UIgg0sSEVJSDYziKws05+140m3X4K/YFGC7LThu3aJHz15GT+6dGvQ+\r
57         +uCC+ri89w5TN65/VHxeVHGe87fJxxJ7JrMY2yjNjjj8+6LsjafuGl2Tl/jeiWtr75eM8Xy9\r
58         ItfxMMc3kYU/Y5emXq6xWH688+eGlNry4M1C8U/FdzGcKdyezDBhnVTw5ojl2eVTNnxUYinO\r
59         SDTUYi4qTgQA20xwAeICAAA=\r
60 Cc: tomi.ollila@iki.fi\r
61 X-BeenThere: notmuch@notmuchmail.org\r
62 X-Mailman-Version: 2.1.13\r
63 Precedence: list\r
64 List-Id: "Use and development of the notmuch mail system."\r
65         <notmuch.notmuchmail.org>\r
66 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
69 List-Post: <mailto:notmuch@notmuchmail.org>\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
71 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
72         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
73 X-List-Received-Date: Sat, 28 Jul 2012 04:29:31 -0000\r
74 \r
75 This method allows callers to output strings with specific lengths.\r
76 It's useful both for strings with embedded NULs (which JSON can\r
77 represent, though parser support is apparently spotty), and\r
78 non-terminated strings.\r
79 ---\r
80  sprinter-json.c |   16 ++++++++++++++--\r
81  sprinter-text.c |   11 +++++++++--\r
82  sprinter.h      |    9 ++++++---\r
83  3 files changed, 29 insertions(+), 7 deletions(-)\r
84 \r
85 diff --git a/sprinter-json.c b/sprinter-json.c\r
86 index 4649655..c9b6835 100644\r
87 --- a/sprinter-json.c\r
88 +++ b/sprinter-json.c\r
89 @@ -88,8 +88,13 @@ json_end (struct sprinter *sp)\r
90         fputc ('\n', spj->stream);\r
91  }\r
92  \r
93 +/* This implementation supports embedded NULs as allowed by the JSON\r
94 + * specification and Unicode.  Support for *parsing* embedded NULs\r
95 + * varies, but is generally not a problem outside of C-based parsers\r
96 + * (Python's json module and Emacs' json.el take embedded NULs in\r
97 + * stride). */\r
98  static void\r
99 -json_string (struct sprinter *sp, const char *val)\r
100 +json_string_len (struct sprinter *sp, const char *val, size_t len)\r
101  {\r
102      static const char *const escapes[] = {\r
103         ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
104 @@ -98,7 +103,7 @@ json_string (struct sprinter *sp, const char *val)\r
105      struct sprinter_json *spj = json_begin_value (sp);\r
106  \r
107      fputc ('"', spj->stream);\r
108 -    for (; *val; ++val) {\r
109 +    for (; len; ++val, --len) {\r
110         unsigned char ch = *val;\r
111         if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
112             fputs (escapes[ch], spj->stream);\r
113 @@ -111,6 +116,12 @@ json_string (struct sprinter *sp, const char *val)\r
114  }\r
115  \r
116  static void\r
117 +json_string (struct sprinter *sp, const char *val)\r
118 +{\r
119 +    json_string_len (sp, val, strlen (val));\r
120 +}\r
121 +\r
122 +static void\r
123  json_integer (struct sprinter *sp, int val)\r
124  {\r
125      struct sprinter_json *spj = json_begin_value (sp);\r
126 @@ -166,6 +177,7 @@ sprinter_json_create (const void *ctx, FILE *stream)\r
127             .begin_list = json_begin_list,\r
128             .end = json_end,\r
129             .string = json_string,\r
130 +           .string_len = json_string_len,\r
131             .integer = json_integer,\r
132             .boolean = json_boolean,\r
133             .null = json_null,\r
134 diff --git a/sprinter-text.c b/sprinter-text.c\r
135 index b208840..dfa54b5 100644\r
136 --- a/sprinter-text.c\r
137 +++ b/sprinter-text.c\r
138 @@ -25,14 +25,20 @@ struct sprinter_text {\r
139  };\r
140  \r
141  static void\r
142 -text_string (struct sprinter *sp, const char *val)\r
143 +text_string_len (struct sprinter *sp, const char *val, size_t len)\r
144  {\r
145      struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
146  \r
147      if (sptxt->current_prefix != NULL)\r
148         fprintf (sptxt->stream, "%s:", sptxt->current_prefix);\r
149  \r
150 -    fputs(val, sptxt->stream);\r
151 +    fwrite (val, len, 1, sptxt->stream);\r
152 +}\r
153 +\r
154 +static void\r
155 +text_string (struct sprinter *sp, const char *val)\r
156 +{\r
157 +    text_string_len (sp, val, strlen (val));\r
158  }\r
159  \r
160  static void\r
161 @@ -105,6 +111,7 @@ sprinter_text_create (const void *ctx, FILE *stream)\r
162             .begin_list = text_begin_list,\r
163             .end = text_end,\r
164             .string = text_string,\r
165 +           .string_len = text_string_len,\r
166             .integer = text_integer,\r
167             .boolean = text_boolean,\r
168             .null = text_null,\r
169 diff --git a/sprinter.h b/sprinter.h\r
170 index 6680d41..5f43175 100644\r
171 --- a/sprinter.h\r
172 +++ b/sprinter.h\r
173 @@ -23,11 +23,14 @@ typedef struct sprinter {\r
174       */\r
175      void (*end) (struct sprinter *);\r
176  \r
177 -    /* Print one string/integer/boolean/null element (possibly inside a\r
178 -     * list or map, followed or preceded by separators).\r
179 -     * For string, the char * must be UTF-8 encoded.\r
180 +    /* Print one string/integer/boolean/null element (possibly inside\r
181 +     * a list or map, followed or preceded by separators).  For string\r
182 +     * and string_len, the char * must be UTF-8 encoded.  string_len\r
183 +     * allows non-terminated strings and strings with embedded NULs\r
184 +     * (though the handling of the latter is format-dependent).\r
185       */\r
186      void (*string) (struct sprinter *, const char *);\r
187 +    void (*string_len) (struct sprinter *, const char *, size_t);\r
188      void (*integer) (struct sprinter *, int);\r
189      void (*boolean) (struct sprinter *, notmuch_bool_t);\r
190      void (*null) (struct sprinter *);\r
191 -- \r
192 1.7.10\r
193 \r