This is version 2 of [0]. It adds some comments explaining handling of NUL bytes in strings and moves a few variable assignments closer to their uses.
[0] id:1343183693-17134-1-git-send-email-amdragon at mit.edu The diff from version 1 follows. (Note that date was already assigned where it was used in version 1, in addition to being assigned at the beginning of format_message_json; hence the diff diff only shows it being removed). diff --git a/notmuch-show.c b/notmuch-show.c index f5bc098..3556293 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -105,6 +105,8 @@ _get_one_line_summary (const void *ctx, notmuch_message_t *message) from, relative_date, tags); } +/* Emit a sequence of key/value pairs for the metadata of message. + * The caller should begin a map before calling this. */ static void format_message_json (sprinter_t *sp, notmuch_message_t *message) { @@ -113,9 +115,6 @@ format_message_json (sprinter_t *sp, notmuch_message_t *message) time_t date; const char *relative_date; - date = notmuch_message_get_date (message); - relative_date = notmuch_time_relative_date (local, date); - sp->map_key (sp, "id"); sp->string (sp, notmuch_message_get_message_id (message)); @@ -133,6 +132,7 @@ format_message_json (sprinter_t *sp, notmuch_message_t *message) sp->integer (sp, date); sp->map_key (sp, "date_relative"); + relative_date = notmuch_time_relative_date (local, date); sp->string (sp, relative_date); sp->map_key (sp, "tags"); diff --git a/sprinter-json.c b/sprinter-json.c index 2587ca6..c9b6835 100644 --- a/sprinter-json.c +++ b/sprinter-json.c @@ -88,6 +88,11 @@ 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_len (struct sprinter *sp, const char *val, size_t len) { diff --git a/sprinter.h b/sprinter.h index 826a852..5f43175 100644 --- a/sprinter.h +++ b/sprinter.h @@ -23,9 +23,11 @@ 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);