]> git.notmuchmail.org Git - notmuch/commitdiff
sprinter: Add a string_len method
authorAustin Clements <amdragon@MIT.EDU>
Fri, 3 Aug 2012 01:14:49 +0000 (21:14 -0400)
committerDavid Bremner <bremner@debian.org>
Fri, 3 Aug 2012 23:21:29 +0000 (20:21 -0300)
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
sprinter-text.c
sprinter.h

index 4649655179b25b71512504806f1977c4747a7b9c..c9b683530b08cb97d0d4689efe7d0a3fff1b5e84 100644 (file)
@@ -88,8 +88,13 @@ json_end (struct sprinter *sp)
        fputc ('\n', spj->stream);
 }
 
        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
 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",
 {
     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);
     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);
        unsigned char ch = *val;
        if (ch < ARRAY_SIZE (escapes) && escapes[ch])
            fputs (escapes[ch], spj->stream);
@@ -110,6 +115,12 @@ json_string (struct sprinter *sp, const char *val)
     fputc ('"', spj->stream);
 }
 
     fputc ('"', spj->stream);
 }
 
+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)
 {
 static void
 json_integer (struct sprinter *sp, int val)
 {
@@ -166,6 +177,7 @@ sprinter_json_create (const void *ctx, FILE *stream)
            .begin_list = json_begin_list,
            .end = json_end,
            .string = json_string,
            .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,
            .integer = json_integer,
            .boolean = json_boolean,
            .null = json_null,
index b208840be72df7d0b2431737abb0f0c28b760eac..dfa54b5706981293ea5cf14a91ceb28137a081fb 100644 (file)
@@ -25,14 +25,20 @@ struct sprinter_text {
 };
 
 static void
 };
 
 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);
 
 {
     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
 }
 
 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,
            .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,
            .integer = text_integer,
            .boolean = text_boolean,
            .null = text_null,
index 6680d41958e209e0e30705cb153297809833810f..5f4317570a059814922402bbe89643e6c7db00b0 100644 (file)
@@ -23,11 +23,14 @@ typedef struct sprinter {
      */
     void (*end) (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) (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 *);
     void (*integer) (struct sprinter *, int);
     void (*boolean) (struct sprinter *, notmuch_bool_t);
     void (*null) (struct sprinter *);