提交 de6decfe 编写于 作者: M Markus Armbruster

json: Simplify parse_string()

Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-28-armbru@redhat.com>
上级 b2da4a4d
...@@ -101,8 +101,7 @@ static int hex2decimal(char ch) ...@@ -101,8 +101,7 @@ static int hex2decimal(char ch)
} else if (ch >= 'A' && ch <= 'F') { } else if (ch >= 'A' && ch <= 'F') {
return 10 + (ch - 'A'); return 10 + (ch - 'A');
} }
abort();
return -1;
} }
/** /**
...@@ -144,7 +143,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) ...@@ -144,7 +143,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
const char *ptr = token->str; const char *ptr = token->str;
QString *str; QString *str;
char quote; char quote;
int cp; int cp, i;
char *end; char *end;
ssize_t len; ssize_t len;
char utf8_buf[5]; char utf8_buf[5];
...@@ -159,51 +158,48 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) ...@@ -159,51 +158,48 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
ptr++; ptr++;
switch (*ptr++) { switch (*ptr++) {
case '"': case '"':
qstring_append(str, "\""); qstring_append_chr(str, '"');
break; break;
case '\'': case '\'':
qstring_append(str, "'"); qstring_append_chr(str, '\'');
break; break;
case '\\': case '\\':
qstring_append(str, "\\"); qstring_append_chr(str, '\\');
break; break;
case '/': case '/':
qstring_append(str, "/"); qstring_append_chr(str, '/');
break; break;
case 'b': case 'b':
qstring_append(str, "\b"); qstring_append_chr(str, '\b');
break; break;
case 'f': case 'f':
qstring_append(str, "\f"); qstring_append_chr(str, '\f');
break; break;
case 'n': case 'n':
qstring_append(str, "\n"); qstring_append_chr(str, '\n');
break; break;
case 'r': case 'r':
qstring_append(str, "\r"); qstring_append_chr(str, '\r');
break; break;
case 't': case 't':
qstring_append(str, "\t"); qstring_append_chr(str, '\t');
break; break;
case 'u': { case 'u':
uint16_t unicode_char = 0; cp = 0;
char utf8_char[4];
int i = 0;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if (qemu_isxdigit(*ptr)) { if (!qemu_isxdigit(*ptr)) {
unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
} else {
parse_error(ctxt, token, parse_error(ctxt, token,
"invalid hex escape sequence in string"); "invalid hex escape sequence in string");
goto out; goto out;
} }
cp <<= 4;
cp |= hex2decimal(*ptr);
ptr++; ptr++;
} }
wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char)); wchar_to_utf8(cp, utf8_buf, sizeof(utf8_buf));
qstring_append(str, utf8_char); qstring_append(str, utf8_buf);
} break; break;
default: default:
parse_error(ctxt, token, "invalid escape sequence in string"); parse_error(ctxt, token, "invalid escape sequence in string");
goto out; goto out;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册