diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-05-06 08:59:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-06 08:59:08 +0200 |
commit | f4ece7e7361ae5e23a80743c9184ac584ad2ce76 (patch) | |
tree | d498727c3c59d2023e46ae84d98b50ce7d13bff6 /core | |
parent | 4b0b670dd486d08f94700e459f38a565dd77a679 (diff) | |
parent | 58fcad20ef9a24a679d1d69579c5ba8652d756e4 (diff) |
Merge pull request #60609 from nathanfranke/string-quotes
Diffstat (limited to 'core')
-rw-r--r-- | core/variant/variant.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index e79da748e9..e69bd88413 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -1620,6 +1620,27 @@ Variant::operator String() const { 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> String stringify_vector(const T &vec, int recursion_count) { String str("["); @@ -1627,7 +1648,8 @@ String stringify_vector(const T &vec, int recursion_count) { if (i > 0) { str += ", "; } - str = str + Variant(vec[i]).stringify(recursion_count); + + str += stringify_variant_clean(vec[i], recursion_count); } str += "]"; return str; @@ -1691,8 +1713,8 @@ String Variant::stringify(int recursion_count) const { recursion_count++; for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { _VariantStrPair sp; - sp.key = E->get().stringify(recursion_count); - sp.value = d[E->get()].stringify(recursion_count); + sp.key = stringify_variant_clean(E->get(), recursion_count); + sp.value = stringify_variant_clean(d[E->get()], recursion_count); pairs.push_back(sp); } @@ -1741,8 +1763,7 @@ String Variant::stringify(int recursion_count) const { return "[...]"; } - String str = stringify_vector(arr, recursion_count); - return str; + return stringify_vector(arr, recursion_count); } break; case OBJECT: { |