Merge pull request #60609 from nathanfranke/string-quotes

This commit is contained in:
Rémi Verschelde 2022-05-06 08:59:08 +02:00 committed by GitHub
commit f4ece7e736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 16 deletions

View File

@ -1620,6 +1620,27 @@ Variant::operator String() const {
return stringify(0); return stringify(0);
} }
String stringify_variant_clean(const Variant p_variant, int recursion_count) {
String s = p_variant.stringify(recursion_count);
// Wrap strings in quotes to avoid ambiguity.
switch (p_variant.get_type()) {
case Variant::STRING: {
s = s.c_escape().quote();
} break;
case Variant::STRING_NAME: {
s = "&" + s.c_escape().quote();
} break;
case Variant::NODE_PATH: {
s = "^" + s.c_escape().quote();
} break;
default: {
} break;
}
return s;
}
template <class T> template <class T>
String stringify_vector(const T &vec, int recursion_count) { String stringify_vector(const T &vec, int recursion_count) {
String str("["); String str("[");
@ -1627,7 +1648,8 @@ String stringify_vector(const T &vec, int recursion_count) {
if (i > 0) { if (i > 0) {
str += ", "; str += ", ";
} }
str = str + Variant(vec[i]).stringify(recursion_count);
str += stringify_variant_clean(vec[i], recursion_count);
} }
str += "]"; str += "]";
return str; return str;
@ -1691,8 +1713,8 @@ String Variant::stringify(int recursion_count) const {
recursion_count++; recursion_count++;
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
_VariantStrPair sp; _VariantStrPair sp;
sp.key = E->get().stringify(recursion_count); sp.key = stringify_variant_clean(E->get(), recursion_count);
sp.value = d[E->get()].stringify(recursion_count); sp.value = stringify_variant_clean(d[E->get()], recursion_count);
pairs.push_back(sp); pairs.push_back(sp);
} }
@ -1741,8 +1763,7 @@ String Variant::stringify(int recursion_count) const {
return "[...]"; return "[...]";
} }
String str = stringify_vector(arr, recursion_count); return stringify_vector(arr, recursion_count);
return str;
} break; } break;
case OBJECT: { case OBJECT: {

View File

@ -10,5 +10,5 @@ wildcard
[1,2,[1,{1:2,2:var z,..}]] [1,2,[1,{1:2,2:var z,..}]]
3 3
[1,2,[1,{1:2,2:var z,..}]] [1,2,[1,{1:2,2:var z,..}]]
[1, 3, 5, 123] [1, 3, 5, "123"]
wildcard wildcard

View File

@ -7,8 +7,8 @@ null
false false
empty array empty array
zero Vector2i zero Vector2i
{22:{4:[nesting, arrays]}} {22:{4:["nesting", "arrays"]}}
{4:[nesting, arrays]} {4:["nesting", "arrays"]}
[nesting, arrays] ["nesting", "arrays"]
nesting nesting
arrays arrays

View File

@ -1,2 +1,2 @@
GDTEST_OK GDTEST_OK
{a:1, b:2, with spaces:3, 2:4} {"a":1, "b":2, "with spaces":3, "2":4}

View File

@ -1,2 +1,2 @@
GDTEST_OK GDTEST_OK
{hello:{world:{is:beautiful}}} {"hello":{"world":{"is":"beautiful"}}}

View File

@ -1,5 +1,5 @@
GDTEST_OK GDTEST_OK
{8:{key:value}} {8:{"key":"value"}}
{key:value} {"key":"value"}
value value
value value

View File

@ -21,14 +21,14 @@ hello/world
RID(0) RID(0)
Node::get_name Node::get_name
Node::[signal]property_list_changed Node::[signal]property_list_changed
{hello:123} {"hello":123}
[hello, 123] ["hello", 123]
[255, 0, 1] [255, 0, 1]
[-1, 0, 1] [-1, 0, 1]
[-1, 0, 1] [-1, 0, 1]
[-1, 0, 1] [-1, 0, 1]
[-1, 0, 1] [-1, 0, 1]
[hello, world] ["hello", "world"]
[(1, 1), (0, 0)] [(1, 1), (0, 0)]
[(1, 1, 1), (0, 0, 0)] [(1, 1, 1), (0, 0, 0)]
[(1, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1)] [(1, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1)]