unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Cc: tomi.ollila@iki.fi
Subject: [PATCH v3 03/13] sprinter: Add a string_len method
Date: Thu,  2 Aug 2012 21:14:49 -0400	[thread overview]
Message-ID: <1343956499-14543-4-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1343956499-14543-1-git-send-email-amdragon@mit.edu>

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

  parent reply	other threads:[~2012-08-03  1:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-03  1:14 [PATCH v3 00/13] Convert notmuch show to use structure printers Austin Clements
2012-08-03  1:14 ` [PATCH v3 01/13] test: Uniformly canonicalize actual and expected JSON Austin Clements
2012-08-03  1:14 ` [PATCH v3 02/13] test: Remove unnecessary JSON canonicalization Austin Clements
2012-08-03  1:14 ` Austin Clements [this message]
2012-08-03  1:14 ` [PATCH v3 04/13] show: Associate an sprinter with each format Austin Clements
2012-08-03  1:14 ` [PATCH v3 05/13] reply: Create a JSON sprinter Austin Clements
2012-08-03  1:14 ` [PATCH v3 06/13] show: Feed the sprinter down to part formatters Austin Clements
2012-08-03  1:14 ` [PATCH v3 07/13] show: Convert format_headers_json to use sprinter Austin Clements
2012-08-03  1:14 ` [PATCH v3 08/13] show: Convert format_part_sigstatus_json " Austin Clements
2012-08-03  1:14 ` [PATCH v3 09/13] show: Convert non-envelope format_part_json " Austin Clements
2012-08-03  1:14 ` [PATCH v3 10/13] show: Convert envelope " Austin Clements
2012-08-03  1:14 ` [PATCH v3 11/13] show: Convert show_message " Austin Clements
2012-08-03  1:14 ` [PATCH v3 12/13] show: Convert do_show " Austin Clements
2012-08-03  1:14 ` [PATCH v3 13/13] show: Remove now unused fields from notmuch_show_format Austin Clements
2012-08-03  9:35 ` [PATCH v3 00/13] Convert notmuch show to use structure printers Tomi Ollila
2012-08-03 23:48 ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1343956499-14543-4-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.org \
    --cc=tomi.ollila@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).