json: Use GString instead of QString to accumulate strings
QString supports modifying its string, but it's quite limited: you can only append. The remaining callers use it for building an initial string, never for modifying it later. Change parse_string() to do build the initial string with GString. This is another step towards making QString immutable. Signed-off-by: Markus Armbruster <armbru@redhat.com> Message-Id: <20201211171152.146877-18-armbru@redhat.com>
This commit is contained in:
parent
3ddba9a9e9
commit
68af4cc121
@ -130,7 +130,7 @@ static int cvt4hex(const char *s)
|
|||||||
static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
||||||
{
|
{
|
||||||
const char *ptr = token->str;
|
const char *ptr = token->str;
|
||||||
QString *str;
|
GString *str;
|
||||||
char quote;
|
char quote;
|
||||||
const char *beg;
|
const char *beg;
|
||||||
int cp, trailing;
|
int cp, trailing;
|
||||||
@ -140,7 +140,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
|||||||
|
|
||||||
assert(*ptr == '"' || *ptr == '\'');
|
assert(*ptr == '"' || *ptr == '\'');
|
||||||
quote = *ptr++;
|
quote = *ptr++;
|
||||||
str = qstring_new();
|
str = g_string_new(NULL);
|
||||||
|
|
||||||
while (*ptr != quote) {
|
while (*ptr != quote) {
|
||||||
assert(*ptr);
|
assert(*ptr);
|
||||||
@ -149,31 +149,31 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
|||||||
beg = ptr++;
|
beg = ptr++;
|
||||||
switch (*ptr++) {
|
switch (*ptr++) {
|
||||||
case '"':
|
case '"':
|
||||||
qstring_append_chr(str, '"');
|
g_string_append_c(str, '"');
|
||||||
break;
|
break;
|
||||||
case '\'':
|
case '\'':
|
||||||
qstring_append_chr(str, '\'');
|
g_string_append_c(str, '\'');
|
||||||
break;
|
break;
|
||||||
case '\\':
|
case '\\':
|
||||||
qstring_append_chr(str, '\\');
|
g_string_append_c(str, '\\');
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
qstring_append_chr(str, '/');
|
g_string_append_c(str, '/');
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
qstring_append_chr(str, '\b');
|
g_string_append_c(str, '\b');
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
qstring_append_chr(str, '\f');
|
g_string_append_c(str, '\f');
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
qstring_append_chr(str, '\n');
|
g_string_append_c(str, '\n');
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
qstring_append_chr(str, '\r');
|
g_string_append_c(str, '\r');
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
qstring_append_chr(str, '\t');
|
g_string_append_c(str, '\t');
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
cp = cvt4hex(ptr);
|
cp = cvt4hex(ptr);
|
||||||
@ -200,7 +200,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
|||||||
(int)(ptr - beg), beg);
|
(int)(ptr - beg), beg);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
qstring_append(str, utf8_buf);
|
g_string_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");
|
||||||
@ -225,14 +225,14 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
|||||||
ptr = end;
|
ptr = end;
|
||||||
len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
|
len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
|
||||||
assert(len >= 0);
|
assert(len >= 0);
|
||||||
qstring_append(str, utf8_buf);
|
g_string_append(str, utf8_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return qstring_from_gstring(str);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
qobject_unref(str);
|
g_string_free(str, true);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user