summaryrefslogtreecommitdiff
path: root/core/variant
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-10-09 11:00:16 +0200
committerGitHub <noreply@github.com>2021-10-09 11:00:16 +0200
commite8c89b2b9158ce011ed5c1a913f55d38226c4a55 (patch)
tree3f06047372a4a9d143e396fd4b60290920d95a35 /core/variant
parentab5e7d1b864c711849681a182909434fb10a2137 (diff)
parent575d4e00b64b6dfdcb03b227cb049ab2ab7cbff0 (diff)
Merge pull request #53588 from mashumafi/stringify
Diffstat (limited to 'core/variant')
-rw-r--r--core/variant/variant.cpp106
1 files changed, 27 insertions, 79 deletions
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp
index 86d772fb07..3214fc125d 100644
--- a/core/variant/variant.cpp
+++ b/core/variant/variant.cpp
@@ -1624,6 +1624,19 @@ Variant::operator String() const {
return stringify(stack);
}
+template <class T>
+String stringify_vector(const T &vec, List<const void *> &stack) {
+ String str("[");
+ for (int i = 0; i < vec.size(); i++) {
+ if (i > 0) {
+ str += ", ";
+ }
+ str = str + Variant(vec[i]).stringify(stack);
+ }
+ str += "]";
+ return str;
+}
+
String Variant::stringify(List<const void *> &stack) const {
switch (type) {
case NIL:
@@ -1703,88 +1716,31 @@ String Variant::stringify(List<const void *> &stack) const {
return str;
} break;
case PACKED_VECTOR2_ARRAY: {
- Vector<Vector2> vec = operator Vector<Vector2>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + Variant(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<Vector2>(), stack);
} break;
case PACKED_VECTOR3_ARRAY: {
- Vector<Vector3> vec = operator Vector<Vector3>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + Variant(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<Vector3>(), stack);
+ } break;
+ case PACKED_COLOR_ARRAY: {
+ return stringify_vector(operator Vector<Color>(), stack);
} break;
case PACKED_STRING_ARRAY: {
- Vector<String> vec = operator Vector<String>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + vec[i];
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<String>(), stack);
+ } break;
+ case PACKED_BYTE_ARRAY: {
+ return stringify_vector(operator Vector<uint8_t>(), stack);
} break;
case PACKED_INT32_ARRAY: {
- Vector<int32_t> vec = operator Vector<int32_t>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + itos(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<int32_t>(), stack);
} break;
case PACKED_INT64_ARRAY: {
- Vector<int64_t> vec = operator Vector<int64_t>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + itos(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<int64_t>(), stack);
} break;
case PACKED_FLOAT32_ARRAY: {
- Vector<float> vec = operator Vector<float>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + rtos(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<float>(), stack);
} break;
case PACKED_FLOAT64_ARRAY: {
- Vector<double> vec = operator Vector<double>();
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + rtos(vec[i]);
- }
- str += "]";
- return str;
+ return stringify_vector(operator Vector<double>(), stack);
} break;
case ARRAY: {
Array arr = operator Array();
@@ -1793,16 +1749,8 @@ String Variant::stringify(List<const void *> &stack) const {
}
stack.push_back(arr.id());
- String str("[");
- for (int i = 0; i < arr.size(); i++) {
- if (i) {
- str += ", ";
- }
-
- str += arr[i].stringify(stack);
- }
+ String str = stringify_vector(arr, stack);
- str += "]";
stack.erase(arr.id());
return str;