diff options
Diffstat (limited to 'core/variant/variant.cpp')
-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: { |