diff options
author | Markus Armbruster <armbru@redhat.com> | 2020-12-11 18:11:36 +0100 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2020-12-19 10:38:43 +0100 |
commit | f1cc129df8341ebb6176363d24b57035bb5dabe4 (patch) | |
tree | 2caf54bd9a2894a4c78223f8a9a4e57c209da023 /qobject/qstring.c | |
parent | 6589f4599151201a61d6b1be8450adb63ae81017 (diff) |
qobject: Use GString instead of QString to accumulate JSON
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.
Use of GString for building the initial string is actually more
convenient here. Change qobject_to_json() & friends to do that.
Once all such uses are replaced this way, QString can become immutable.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20201211171152.146877-5-armbru@redhat.com>
Diffstat (limited to 'qobject/qstring.c')
-rw-r--r-- | qobject/qstring.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/qobject/qstring.c b/qobject/qstring.c index b66a2c35f2..af7c18ca73 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -66,6 +66,25 @@ QString *qstring_from_str(const char *str) return qstring_from_substr(str, 0, strlen(str)); } +/** + * qstring_from_gstring(): Convert a GString to a QString + * + * Return strong reference. + */ + +QString *qstring_from_gstring(GString *gstr) +{ + QString *qstring; + + qstring = g_malloc(sizeof(*qstring)); + qobject_init(QOBJECT(qstring), QTYPE_QSTRING); + qstring->length = gstr->len; + qstring->capacity = gstr->allocated_len; + qstring->string = g_string_free(gstr, false); + return qstring; +} + + static void capacity_increase(QString *qstring, size_t len) { if (qstring->capacity < (qstring->length + len)) { |