diff options
author | Markus Armbruster <armbru@redhat.com> | 2018-08-23 18:39:54 +0200 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2018-08-24 20:26:37 +0200 |
commit | de6decfe8e2939464fc27ac2ab4ef87689329fec (patch) | |
tree | ff03b19c32b4200b91789fc6bc291cc96f0604c6 | |
parent | b2da4a4d7537567b44db60b7b79cd14f64e48f2f (diff) |
json: Simplify parse_string()
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-28-armbru@redhat.com>
-rw-r--r-- | qobject/json-parser.c | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/qobject/json-parser.c b/qobject/json-parser.c index 7437827c24..9cb363f7e1 100644 --- a/qobject/json-parser.c +++ b/qobject/json-parser.c @@ -101,8 +101,7 @@ static int hex2decimal(char ch) } else if (ch >= 'A' && ch <= 'F') { return 10 + (ch - 'A'); } - - return -1; + abort(); } /** @@ -144,7 +143,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) const char *ptr = token->str; QString *str; char quote; - int cp; + int cp, i; char *end; ssize_t len; char utf8_buf[5]; @@ -159,51 +158,48 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) ptr++; switch (*ptr++) { case '"': - qstring_append(str, "\""); + qstring_append_chr(str, '"'); break; case '\'': - qstring_append(str, "'"); + qstring_append_chr(str, '\''); break; case '\\': - qstring_append(str, "\\"); + qstring_append_chr(str, '\\'); break; case '/': - qstring_append(str, "/"); + qstring_append_chr(str, '/'); break; case 'b': - qstring_append(str, "\b"); + qstring_append_chr(str, '\b'); break; case 'f': - qstring_append(str, "\f"); + qstring_append_chr(str, '\f'); break; case 'n': - qstring_append(str, "\n"); + qstring_append_chr(str, '\n'); break; case 'r': - qstring_append(str, "\r"); + qstring_append_chr(str, '\r'); break; case 't': - qstring_append(str, "\t"); + qstring_append_chr(str, '\t'); break; - case 'u': { - uint16_t unicode_char = 0; - char utf8_char[4]; - int i = 0; - + case 'u': + cp = 0; for (i = 0; i < 4; i++) { - if (qemu_isxdigit(*ptr)) { - unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4); - } else { + if (!qemu_isxdigit(*ptr)) { parse_error(ctxt, token, "invalid hex escape sequence in string"); goto out; } + cp <<= 4; + cp |= hex2decimal(*ptr); ptr++; } - wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char)); - qstring_append(str, utf8_char); - } break; + wchar_to_utf8(cp, utf8_buf, sizeof(utf8_buf)); + qstring_append(str, utf8_buf); + break; default: parse_error(ctxt, token, "invalid escape sequence in string"); goto out; |