From 1ac0119f7db890080a3057a73704672c004227be Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Sun, 29 Jul 2012 00:29:04 +2000 Subject: [PATCH] [PATCH v2 03/13] sprinter: Add a string_len method --- 43/691561c196991a86db02084dc5d5215ab392b0 | 193 ++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 43/691561c196991a86db02084dc5d5215ab392b0 diff --git a/43/691561c196991a86db02084dc5d5215ab392b0 b/43/691561c196991a86db02084dc5d5215ab392b0 new file mode 100644 index 000000000..6c0fc73ea --- /dev/null +++ b/43/691561c196991a86db02084dc5d5215ab392b0 @@ -0,0 +1,193 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 6CF9C431FBC + for ; Fri, 27 Jul 2012 21:29:31 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 4hqMLyE7PfPr for ; + Fri, 27 Jul 2012 21:29:30 -0700 (PDT) +Received: from dmz-mailsec-scanner-1.mit.edu (DMZ-MAILSEC-SCANNER-1.MIT.EDU + [18.9.25.12]) + by olra.theworths.org (Postfix) with ESMTP id 20DFB431FAF + for ; Fri, 27 Jul 2012 21:29:26 -0700 (PDT) +X-AuditID: 1209190c-b7f806d000006b87-65-50136aa4f71a +Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) + by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP + id 42.00.27527.4AA63105; Sat, 28 Jul 2012 00:29:24 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q6S4TNXB017240; + Sat, 28 Jul 2012 00:29:23 -0400 +Received: from drake.dyndns.org + (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [209.6.116.242]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6S4TKie027354 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sat, 28 Jul 2012 00:29:21 -0400 (EDT) +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1SuyeW-0002Wp-DK; Sat, 28 Jul 2012 00:29:20 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v2 03/13] sprinter: Add a string_len method +Date: Sat, 28 Jul 2012 00:29:04 -0400 +Message-Id: <1343449754-9010-4-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10 +In-Reply-To: <1343449754-9010-1-git-send-email-amdragon@mit.edu> +References: <1343449754-9010-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFjrPIsWRmVeSWpSXmKPExsUixCmqrLskSzjAYF63lsXehnZGi9VzeSyu + 35zJbPFm5TxWBxaPnbPusnss3rSfzePw14UsHs9W3WIOYInisklJzcksSy3St0vgypjV1M9Y + sEmhYsvU2ywNjBMluxg5OSQETCQW3n3JDmGLSVy4t54NxBYS2Mco8XindhcjF5C9gVHi8/MZ + bBDOIyaJ/0+WsUA4cxkldk17yQrSwiagIbFt/3JGEFtEQFpi593ZQHEODmaBPInHayVAwsIC + 1hLTF78D28AioCpxduZVZhCbV8Be4tnNFjaIK+Qlnt7vA7M5BRwkNr/8wAxxkb3E7skbWCcw + 8i9gZFjFKJuSW6Wbm5iZU5yarFucnJiXl1qka6iXm1mil5pSuokRHG6SPDsY3xxUOsQowMGo + xMOrfF0oQIg1say4MvcQoyQHk5Iob1S4cIAQX1J+SmVGYnFGfFFpTmrxIUYJDmYlEd6dgkA5 + 3pTEyqrUonyYlDQHi5I47+WUm/5CAumJJanZqakFqUUwWRkODiUJ3u2ZQI2CRanpqRVpmTkl + CGkmDk6Q4TxAw9eA1PAWFyTmFmemQ+RPMSpKifNOAkkIgCQySvPgemHp4BWjONArwrzXQKp4 + gKkErvsV0GAmoMEW0UIgg0sSEVJSDYziKws05+140m3X4K/YFGC7LThu3aJHz15GT+6dGvQ+ + +uCC+ri89w5TN65/VHxeVHGe87fJxxJ7JrMY2yjNjjj8+6LsjafuGl2Tl/jeiWtr75eM8Xy9 + ItfxMMc3kYU/Y5emXq6xWH688+eGlNry4M1C8U/FdzGcKdyezDBhnVTw5ojl2eVTNnxUYinO + SDTUYi4qTgQA20xwAeICAAA= +Cc: tomi.ollila@iki.fi +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 28 Jul 2012 04:29:31 -0000 + +This method allows callers to output strings with specific lengths. +It's useful both for strings with embedded NULs (which JSON can +represent, though parser support is apparently spotty), and +non-terminated strings. +--- + sprinter-json.c | 16 ++++++++++++++-- + sprinter-text.c | 11 +++++++++-- + sprinter.h | 9 ++++++--- + 3 files changed, 29 insertions(+), 7 deletions(-) + +diff --git a/sprinter-json.c b/sprinter-json.c +index 4649655..c9b6835 100644 +--- a/sprinter-json.c ++++ b/sprinter-json.c +@@ -88,8 +88,13 @@ json_end (struct sprinter *sp) + fputc ('\n', spj->stream); + } + ++/* This implementation supports embedded NULs as allowed by the JSON ++ * specification and Unicode. Support for *parsing* embedded NULs ++ * varies, but is generally not a problem outside of C-based parsers ++ * (Python's json module and Emacs' json.el take embedded NULs in ++ * stride). */ + static void +-json_string (struct sprinter *sp, const char *val) ++json_string_len (struct sprinter *sp, const char *val, size_t len) + { + static const char *const escapes[] = { + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b", +@@ -98,7 +103,7 @@ json_string (struct sprinter *sp, const char *val) + struct sprinter_json *spj = json_begin_value (sp); + + fputc ('"', spj->stream); +- for (; *val; ++val) { ++ for (; len; ++val, --len) { + unsigned char ch = *val; + if (ch < ARRAY_SIZE (escapes) && escapes[ch]) + fputs (escapes[ch], spj->stream); +@@ -111,6 +116,12 @@ json_string (struct sprinter *sp, const char *val) + } + + static void ++json_string (struct sprinter *sp, const char *val) ++{ ++ json_string_len (sp, val, strlen (val)); ++} ++ ++static void + json_integer (struct sprinter *sp, int val) + { + struct sprinter_json *spj = json_begin_value (sp); +@@ -166,6 +177,7 @@ sprinter_json_create (const void *ctx, FILE *stream) + .begin_list = json_begin_list, + .end = json_end, + .string = json_string, ++ .string_len = json_string_len, + .integer = json_integer, + .boolean = json_boolean, + .null = json_null, +diff --git a/sprinter-text.c b/sprinter-text.c +index b208840..dfa54b5 100644 +--- a/sprinter-text.c ++++ b/sprinter-text.c +@@ -25,14 +25,20 @@ struct sprinter_text { + }; + + static void +-text_string (struct sprinter *sp, const char *val) ++text_string_len (struct sprinter *sp, const char *val, size_t len) + { + struct sprinter_text *sptxt = (struct sprinter_text *) sp; + + if (sptxt->current_prefix != NULL) + fprintf (sptxt->stream, "%s:", sptxt->current_prefix); + +- fputs(val, sptxt->stream); ++ fwrite (val, len, 1, sptxt->stream); ++} ++ ++static void ++text_string (struct sprinter *sp, const char *val) ++{ ++ text_string_len (sp, val, strlen (val)); + } + + static void +@@ -105,6 +111,7 @@ sprinter_text_create (const void *ctx, FILE *stream) + .begin_list = text_begin_list, + .end = text_end, + .string = text_string, ++ .string_len = text_string_len, + .integer = text_integer, + .boolean = text_boolean, + .null = text_null, +diff --git a/sprinter.h b/sprinter.h +index 6680d41..5f43175 100644 +--- a/sprinter.h ++++ b/sprinter.h +@@ -23,11 +23,14 @@ typedef struct sprinter { + */ + void (*end) (struct sprinter *); + +- /* Print one string/integer/boolean/null element (possibly inside a +- * list or map, followed or preceded by separators). +- * For string, the char * must be UTF-8 encoded. ++ /* Print one string/integer/boolean/null element (possibly inside ++ * a list or map, followed or preceded by separators). For string ++ * and string_len, the char * must be UTF-8 encoded. string_len ++ * allows non-terminated strings and strings with embedded NULs ++ * (though the handling of the latter is format-dependent). + */ + void (*string) (struct sprinter *, const char *); ++ void (*string_len) (struct sprinter *, const char *, size_t); + void (*integer) (struct sprinter *, int); + void (*boolean) (struct sprinter *, notmuch_bool_t); + void (*null) (struct sprinter *); +-- +1.7.10 + -- 2.26.2