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)
|
||||
{
|
||||
const char *ptr = token->str;
|
||||
QString *str;
|
||||
GString *str;
|
||||
char quote;
|
||||
const char *beg;
|
||||
int cp, trailing;
|
||||
@ -140,7 +140,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
||||
|
||||
assert(*ptr == '"' || *ptr == '\'');
|
||||
quote = *ptr++;
|
||||
str = qstring_new();
|
||||
str = g_string_new(NULL);
|
||||
|
||||
while (*ptr != quote) {
|
||||
assert(*ptr);
|
||||
@ -149,31 +149,31 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
||||
beg = ptr++;
|
||||
switch (*ptr++) {
|
||||
case '"':
|
||||
qstring_append_chr(str, '"');
|
||||
g_string_append_c(str, '"');
|
||||
break;
|
||||
case '\'':
|
||||
qstring_append_chr(str, '\'');
|
||||
g_string_append_c(str, '\'');
|
||||
break;
|
||||
case '\\':
|
||||
qstring_append_chr(str, '\\');
|
||||
g_string_append_c(str, '\\');
|
||||
break;
|
||||
case '/':
|
||||
qstring_append_chr(str, '/');
|
||||
g_string_append_c(str, '/');
|
||||
break;
|
||||
case 'b':
|
||||
qstring_append_chr(str, '\b');
|
||||
g_string_append_c(str, '\b');
|
||||
break;
|
||||
case 'f':
|
||||
qstring_append_chr(str, '\f');
|
||||
g_string_append_c(str, '\f');
|
||||
break;
|
||||
case 'n':
|
||||
qstring_append_chr(str, '\n');
|
||||
g_string_append_c(str, '\n');
|
||||
break;
|
||||
case 'r':
|
||||
qstring_append_chr(str, '\r');
|
||||
g_string_append_c(str, '\r');
|
||||
break;
|
||||
case 't':
|
||||
qstring_append_chr(str, '\t');
|
||||
g_string_append_c(str, '\t');
|
||||
break;
|
||||
case 'u':
|
||||
cp = cvt4hex(ptr);
|
||||
@ -200,7 +200,7 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
||||
(int)(ptr - beg), beg);
|
||||
goto out;
|
||||
}
|
||||
qstring_append(str, utf8_buf);
|
||||
g_string_append(str, utf8_buf);
|
||||
break;
|
||||
default:
|
||||
parse_error(ctxt, token, "invalid escape sequence in string");
|
||||
@ -225,14 +225,14 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
||||
ptr = end;
|
||||
len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
|
||||
assert(len >= 0);
|
||||
qstring_append(str, utf8_buf);
|
||||
g_string_append(str, utf8_buf);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
return qstring_from_gstring(str);
|
||||
|
||||
out:
|
||||
qobject_unref(str);
|
||||
g_string_free(str, true);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user