Forráskód Böngészése

quote strings inside arrays and dictionaries

Nathan Franke 3 éve
szülő
commit
58fcad20ef

+ 26 - 5
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: {

+ 1 - 1
modules/gdscript/tests/scripts/parser/features/advanced_expression_matching.out

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

+ 3 - 3
modules/gdscript/tests/scripts/parser/features/dictionary.out

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

+ 1 - 1
modules/gdscript/tests/scripts/parser/features/dictionary_lua_style.out

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

+ 1 - 1
modules/gdscript/tests/scripts/parser/features/dictionary_mixed_syntax.out

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

+ 2 - 2
modules/gdscript/tests/scripts/parser/features/nested_dictionary.out

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

+ 3 - 3
modules/gdscript/tests/scripts/runtime/features/stringify.out

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