From de6decfe8e2939464fc27ac2ab4ef87689329fec Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 23 Aug 2018 18:39:54 +0200 Subject: [PATCH] json: Simplify parse_string() Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <20180823164025.12553-28-armbru@redhat.com> --- qobject/json-parser.c | 42 +++++++++++++++++++----------------------- 1 file 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;