summaryrefslogtreecommitdiff
path: root/core/variant
diff options
context:
space:
mode:
authormashumafi <mashumafi@gmail.com>2021-10-09 03:47:18 +0000
committermashumafi <mashumafi@gmail.com>2021-10-09 06:19:25 +0000
commit575d4e00b64b6dfdcb03b227cb049ab2ab7cbff0 (patch)
tree8a033888b9cbb4f14fe5bec2c85fcfeeb0d76c4e /core/variant
parent6f72d9d19fe11ccc38a69e0460813646ad28d0e5 (diff)
Enhance and cleanup stringify for Vector
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;