瀏覽代碼

Simplify toString

Kajetan Johannes Hammerle 3 月之前
父節點
當前提交
b58fac69ad

+ 3 - 3
include/core/BitArray.h

@@ -3,12 +3,11 @@
 
 #include "core/Types.h"
 
-struct BitArrayT {
+typedef struct {
     u64 length : 56;
     u64 bits : 8;
     u64* data;
-};
-typedef struct BitArrayT BitArray;
+} BitArray;
 static_assert(sizeof(BitArray) == 16, "invalid bit array size");
 
 void initBitArray(BitArray* a, size_t length, size_t bits);
@@ -18,5 +17,6 @@ void setAllBits(BitArray* a, u64 value);
 u64 getBits(const BitArray* a, size_t index);
 i64 selectBits(const BitArray* a, size_t index);
 void setBitLength(BitArray* a, size_t newLength, size_t newBits);
+size_t toStringBitArray(const BitArray* a, char* buffer, size_t n);
 
 #endif

+ 3 - 3
include/core/Box.h

@@ -3,11 +3,10 @@
 
 #include "core/Vector.h"
 
-struct BoxT {
+typedef struct {
     Vector3 min;
     Vector3 max;
-};
-typedef struct BoxT Box;
+} Box;
 
 #define BOX ((Box){0})
 void setBox(Box* box, const Vector3* size);
@@ -15,5 +14,6 @@ void offsetBox(Box* box, const Vector3* offset);
 bool collidesWithBox(const Box* box, const Box* other);
 void expandBox(Box* box, const Vector3* offset);
 void growBox(Box* box, const Vector3* growth);
+size_t toStringBox(const Box* box, char* buffer, size_t n);
 
 #endif

+ 4 - 4
include/core/HashMap.h

@@ -214,21 +214,21 @@ size_t roundUp2(size_t n);
                               ToStringKey##N keyString,                        \
                               ToStringValue##N valueString) {                  \
         size_t w = 0;                                                          \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "["));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "["));                  \
         bool notFirst = false;                                                 \
         HashMapIterator##N i;                                                  \
         initHashMapIterator##N(&i, m);                                         \
         while(hasNextHashMapNode##N(&i)) {                                     \
             HashMapNode##N* node = nextHashMapNode##N(&i);                     \
             if(notFirst) {                                                     \
-                stringAdd(&w, &buffer, &n, coreToString(buffer, n, ", "));     \
+                stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));         \
             }                                                                  \
             notFirst = true;                                                   \
             stringAdd(&w, &buffer, &n, keyString(node->key, buffer, n));       \
-            stringAdd(&w, &buffer, &n, coreToString(buffer, n, " = "));        \
+            stringAdd(&w, &buffer, &n, toString(buffer, n, " = "));            \
             stringAdd(&w, &buffer, &n, valueString(node->value, buffer, n));   \
         }                                                                      \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "]"));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));                  \
         return w;                                                              \
     }
 

+ 3 - 3
include/core/List.h

@@ -114,17 +114,17 @@
     size_t toStringList##N(const List##N* l, char* buffer, size_t n,           \
                            ToString##N c) {                                    \
         size_t w = 0;                                                          \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "["));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "["));                  \
         T* end = getListEnd##N(l);                                             \
         T* p = getListStart##N(l);                                             \
         while(p != end) {                                                      \
             stringAdd(&w, &buffer, &n, c(p, buffer, n));                       \
             p++;                                                               \
             if(p != end) {                                                     \
-                stringAdd(&w, &buffer, &n, coreToString(buffer, n, ", "));     \
+                stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));         \
             }                                                                  \
         }                                                                      \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "]"));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));                  \
         return w;                                                              \
     }
 

+ 3 - 3
include/core/Matrix.h

@@ -4,10 +4,9 @@
 #include "core/Quaternion.h"
 #include "core/Vector.h"
 
-struct MatrixT {
+typedef struct {
     Vector4 data[4];
-};
-typedef struct MatrixT Matrix;
+} Matrix;
 
 #define ZERO_MATRIX ((Matrix){0})
 #define UNIT_MATRIX                                                            \
@@ -28,5 +27,6 @@ Matrix* rotateMatrixX(Matrix* m, float degrees);
 Matrix* rotateMatrixY(Matrix* m, float degrees);
 Matrix* rotateMatrixZ(Matrix* m, float degrees);
 Matrix* rotateMatrix(Matrix* m, const Quaternion* q);
+size_t toStringMatrix(const Matrix* m, char* buffer, size_t n);
 
 #endif

+ 3 - 3
include/core/Plane.h

@@ -3,13 +3,13 @@
 
 #include "core/Vector.h"
 
-struct PlaneT {
+typedef struct {
     Vector3 abc;
     float d;
-};
-typedef struct PlaneT Plane;
+} Plane;
 
 void initPlane(Plane* p, const Vector3* a, const Vector3* b, const Vector3* c);
 float signedDistance(const Plane* p, const Vector3* v);
+size_t toStringPlane(const Plane* p, char* buffer, size_t n);
 
 #endif

+ 3 - 3
include/core/Queue.h

@@ -66,19 +66,19 @@
     size_t toStringQueue##N(const Queue##N* r, char* buffer, size_t n,         \
                             ToString##N c) {                                   \
         size_t w = 0;                                                          \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "["));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "["));                  \
         size_t end = r->length;                                                \
         if(end > 0) {                                                          \
             end--;                                                             \
             for(size_t i = 0; i < end; i++) {                                  \
                 stringAdd(&w, &buffer, &n,                                     \
                           c(getQueueIndex##N(r, i), buffer, n));               \
-                stringAdd(&w, &buffer, &n, coreToString(buffer, n, ", "));     \
+                stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));         \
             }                                                                  \
             stringAdd(&w, &buffer, &n,                                         \
                       c(getQueueIndex##N(r, end), buffer, n));                 \
         }                                                                      \
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "]"));              \
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));                  \
         return w;                                                              \
     }
 

+ 1 - 24
include/core/ToString.h

@@ -5,32 +5,9 @@
 #include "core/Types.h"
 
 check_format(3, 4) size_t
-    coreToString(char* buffer, size_t n, const char* format, ...);
+    toString(char* buffer, size_t n, const char* format, ...);
 size_t toStringSize(const size_t* p, char* buffer, size_t n);
 size_t toStringInt(const int* p, char* buffer, size_t n);
 void stringAdd(size_t* w, char** buffer, size_t* n, size_t shift);
 
-#define STRUCT_TO_STRING(type)                                                 \
-    struct type##T;                                                            \
-    size_t toString##type(const struct type##T* a, char* buffer, size_t n);
-
-STRUCT_TO_STRING(BitArray)
-STRUCT_TO_STRING(Box)
-STRUCT_TO_STRING(Matrix)
-STRUCT_TO_STRING(Plane)
-
-// clang-format off
-#define PAIR(a, b) a: b
-#define STRUCT_PAIR(name)                                                      \
-    PAIR(const struct name##T*, toString##name), PAIR(struct name##T*, toString##name)
-// clang-format on
-
-#define toString(t, ...)                                                       \
-    _Generic((t),                                                              \
-        char*: coreToString,                                                   \
-        STRUCT_PAIR(BitArray),                                                 \
-        STRUCT_PAIR(Box),                                                      \
-        STRUCT_PAIR(Matrix),                                                   \
-        STRUCT_PAIR(Plane))(t, __VA_ARGS__)
-
 #endif

+ 19 - 0
src/BitArray.c

@@ -1,9 +1,11 @@
 #include "core/BitArray.h"
 
 #include <assert.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
 
+#include "core/ToString.h"
 #include "core/Utility.h"
 
 static u64 roundUpDivide(u64 a, u64 b) {
@@ -123,3 +125,20 @@ void setBitLength(BitArray* a, size_t newLength, size_t newBits) {
     a->length = newLength & 0xFFFFFFFFFFFFFF;
     a->bits = newBits & 0xFF;
 }
+
+size_t toStringBitArray(const BitArray* a, char* buffer, size_t n) {
+    size_t w = 0;
+    stringAdd(&w, &buffer, &n, toString(buffer, n, "["));
+    size_t length = a->length;
+    if(length > 0) {
+        length--;
+        for(size_t i = 0; i < length; i++) {
+            u64 v = getBits(a, i);
+            stringAdd(&w, &buffer, &n, toString(buffer, n, "%" PRIu64 ", ", v));
+        }
+        u64 v = getBits(a, length);
+        stringAdd(&w, &buffer, &n, toString(buffer, n, "%" PRIu64, v));
+    }
+    stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));
+    return w;
+}

+ 8 - 0
src/Box.c

@@ -1,6 +1,7 @@
 #include "core/Box.h"
 
 #include "core/Generic.h"
+#include "core/ToString.h"
 
 void setBox(Box* box, const Vector3* size) {
     for(size_t i = 0; i < 3; i++) {
@@ -54,3 +55,10 @@ void growBox(Box* box, const Vector3* growth) {
     box->min = nMin;
     box->max = nMax;
 }
+
+size_t toStringBox(const Box* box, char* buffer, size_t n) {
+    return toString(buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
+                    (double)box->min.data[0], (double)box->min.data[1],
+                    (double)box->min.data[2], (double)box->max.data[0],
+                    (double)box->max.data[1], (double)box->max.data[2]);
+}

+ 14 - 0
src/Matrix.c

@@ -122,3 +122,17 @@ Matrix* rotateMatrix(Matrix* m, const Quaternion* q) {
     m->data[2] = V(a.data[2], b.data[2], c.data[2], d.data[2]);
     return m;
 }
+
+size_t toStringMatrix(const Matrix* m, char* buffer, size_t n) {
+    size_t w = 0;
+    stringAdd(&w, &buffer, &n, toString(buffer, n, "["));
+    stringAdd(&w, &buffer, &n, toStringV4(m->data + 0, buffer, n));
+    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
+    stringAdd(&w, &buffer, &n, toStringV4(m->data + 1, buffer, n));
+    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
+    stringAdd(&w, &buffer, &n, toStringV4(m->data + 2, buffer, n));
+    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
+    stringAdd(&w, &buffer, &n, toStringV4(m->data + 3, buffer, n));
+    stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));
+    return w;
+}

+ 7 - 0
src/Plane.c

@@ -1,6 +1,7 @@
 #include "core/Plane.h"
 
 #include "core/Generic.h"
+#include "core/ToString.h"
 
 void initPlane(Plane* p, const Vector3* a, const Vector3* b, const Vector3* c) {
     cross(&p->abc, sub(b, a), sub(c, a));
@@ -11,3 +12,9 @@ void initPlane(Plane* p, const Vector3* a, const Vector3* b, const Vector3* c) {
 float signedDistance(const Plane* p, const Vector3* v) {
     return dot(&p->abc, v) + p->d;
 }
+
+size_t toStringPlane(const Plane* p, char* buffer, size_t n) {
+    return toString(buffer, n, "(%.3f x + %.3f y + %.3f z + %.3f)",
+                    (double)p->abc.data[0], (double)p->abc.data[1],
+                    (double)p->abc.data[2], (double)p->d);
+}

+ 1 - 56
src/ToString.c

@@ -1,18 +1,9 @@
 #include "core/ToString.h"
 
-#include <inttypes.h>
 #include <stdarg.h>
 #include <stdio.h>
 
-#include "core/BitArray.h"
-#include "core/Box.h"
-#include "core/HashMap.h"
-#include "core/Matrix.h"
-#include "core/Plane.h"
-#include "core/Queue.h"
-#include "core/Utility.h"
-
-size_t coreToString(char* buffer, size_t n, const char* format, ...) {
+size_t toString(char* buffer, size_t n, const char* format, ...) {
     va_list args;
     va_start(args, format);
     int w = vsnprintf(buffer, n, format, args);
@@ -37,49 +28,3 @@ void stringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
         *n = 0;
     }
 }
-
-size_t toStringBitArray(const BitArray* a, char* buffer, size_t n) {
-    size_t w = 0;
-    stringAdd(&w, &buffer, &n, coreToString(buffer, n, "["));
-    size_t length = a->length;
-    if(length > 0) {
-        length--;
-        for(size_t i = 0; i < length; i++) {
-            u64 v = getBits(a, i);
-            stringAdd(&w, &buffer, &n,
-                      coreToString(buffer, n, "%" PRIu64 ", ", v));
-        }
-        u64 v = getBits(a, length);
-        stringAdd(&w, &buffer, &n, coreToString(buffer, n, "%" PRIu64, v));
-    }
-    stringAdd(&w, &buffer, &n, coreToString(buffer, n, "]"));
-    return w;
-}
-
-size_t toStringBox(const Box* box, char* buffer, size_t n) {
-    return coreToString(buffer, n,
-                        "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
-                        (double)box->min.data[0], (double)box->min.data[1],
-                        (double)box->min.data[2], (double)box->max.data[0],
-                        (double)box->max.data[1], (double)box->max.data[2]);
-}
-
-size_t toStringMatrix(const Matrix* m, char* buffer, size_t n) {
-    size_t w = 0;
-    stringAdd(&w, &buffer, &n, toString(buffer, n, "["));
-    stringAdd(&w, &buffer, &n, toStringV4(m->data + 0, buffer, n));
-    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
-    stringAdd(&w, &buffer, &n, toStringV4(m->data + 1, buffer, n));
-    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
-    stringAdd(&w, &buffer, &n, toStringV4(m->data + 2, buffer, n));
-    stringAdd(&w, &buffer, &n, toString(buffer, n, ", "));
-    stringAdd(&w, &buffer, &n, toStringV4(m->data + 3, buffer, n));
-    stringAdd(&w, &buffer, &n, toString(buffer, n, "]"));
-    return w;
-}
-
-size_t toStringPlane(const Plane* p, char* buffer, size_t n) {
-    return coreToString(buffer, n, "(%.3f x + %.3f y + %.3f z + %.3f)",
-                        (double)p->abc.data[0], (double)p->abc.data[1],
-                        (double)p->abc.data[2], (double)p->d);
-}

+ 3 - 3
test/modules/BitArrayTests.c

@@ -83,7 +83,7 @@ static void testToString1() {
     setBits(&bits, 2, 3);
     setBits(&bits, 3, 4);
     char buffer[128];
-    size_t n = toString(&bits, buffer, sizeof(buffer));
+    size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
     TEST_SIZE(12, n);
     TEST_STRING("[1, 2, 3, 4]", buffer);
     destroyBitArray(&bits);
@@ -94,7 +94,7 @@ static void testToString2() {
     initBitArray(&bits, 1, 3);
     setBits(&bits, 0, 1);
     char buffer[128];
-    size_t n = toString(&bits, buffer, sizeof(buffer));
+    size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
     TEST_SIZE(3, n);
     TEST_STRING("[1]", buffer);
     destroyBitArray(&bits);
@@ -104,7 +104,7 @@ static void testToString3() {
     BitArray bits;
     initBitArray(&bits, 0, 0);
     char buffer[128];
-    size_t n = toString(&bits, buffer, sizeof(buffer));
+    size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
     TEST_SIZE(2, n);
     TEST_STRING("[]", buffer);
     destroyBitArray(&bits);

+ 8 - 8
test/modules/BoxTests.c

@@ -6,10 +6,10 @@ static void testInit() {
     Box box = BOX;
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     char buffer[128];
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([0.000, 0.000, 0.000], [1.000, 2.000, 3.000])", buffer);
     setBox(&box, &V(-1.0f, -2.0f, -3.0f));
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([-1.000, -2.000, -3.000], [0.000, 0.000, 0.000])", buffer);
 }
 
@@ -18,7 +18,7 @@ static void testOffset() {
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     offsetBox(&box, &V(7.0f, -4.0f, 6.0f));
     char buffer[128];
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([7.000, -4.000, 6.000], [8.000, -2.000, 9.000])", buffer);
 }
 
@@ -45,12 +45,12 @@ static void testExpand() {
     expandBox(&box, &V(7.0f, -4.0f, 6.0f));
 
     char buffer[128];
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([0.000, -4.000, 0.000], [8.000, 2.000, 9.000])", buffer);
 
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     expandBox(&box, &V(-7.0f, 4.0f, -6.0f));
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([-7.000, 0.000, -6.000], [1.000, 6.000, 3.000])", buffer);
 }
 
@@ -59,17 +59,17 @@ static void testGrow() {
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     growBox(&box, &V(4.0f, 2.0f, 6.0f));
     char buffer[128];
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([-2.000, -1.000, -3.000], [3.000, 3.000, 6.000])", buffer);
 
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     growBox(&box, &V(-4.0f, -2.0f, -6.0f));
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([0.500, 1.000, 1.500], [0.500, 1.000, 1.500])", buffer);
 
     setBox(&box, &V(1.0f, 2.0f, 3.0f));
     growBox(&box, &V(-0.1f, -4.0f, -1.0f));
-    toString(&box, buffer, sizeof(buffer));
+    toStringBox(&box, buffer, sizeof(buffer));
     TEST_STRING("Box([0.050, 1.000, 0.500], [0.950, 1.000, 2.500])", buffer);
 }
 

+ 1 - 1
test/modules/FrustumTests.c

@@ -54,7 +54,7 @@ static void testUpdateProjection() {
     initFrustum(&f, 60.0f, 0.1f, 1000.0f);
     const Matrix* m = updateProjection(&f, &IV(400, 300));
     char buffer[128];
-    toString(m, buffer, sizeof(buffer));
+    toStringMatrix(m, buffer, sizeof(buffer));
     TEST_STRING("[[1.299, 0.000, 0.000, 0.000], "
                 "[0.000, 1.732, 0.000, 0.000], "
                 "[0.000, 0.000, -1.000, -0.200], "

+ 1 - 1
test/modules/HashMapTests.c

@@ -233,7 +233,7 @@ static void testHash() {
 }
 
 static size_t toStringA(const A* a, char* buffer, size_t n) {
-    return coreToString(buffer, n, "{%zu, %zu}", a->a, a->b);
+    return toString(buffer, n, "{%zu, %zu}", a->a, a->b);
 }
 
 static void testSearchStruct() {

+ 1 - 1
test/modules/PlaneTests.c

@@ -8,7 +8,7 @@ static void testToString() {
     Plane p;
     initPlane(&p, &V(3, 6, 8), &V(7, 6, 2), &V(4, 4, 4));
     char buffer[128];
-    toString(&p, buffer, sizeof(buffer));
+    toStringPlane(&p, buffer, sizeof(buffer));
     TEST_STRING("(-0.684 x + 0.570 y + -0.456 z + 2.279)", buffer);
 }
 

+ 1 - 1
test/modules/ViewTests.c

@@ -32,7 +32,7 @@ static void testUpdateMatrix() {
     Matrix* m = updateMatrix(&v, &V(1.0f, 2.0f, 3.0f));
 
     char buffer[128];
-    toString(m, buffer, sizeof(buffer));
+    toStringMatrix(m, buffer, sizeof(buffer));
 
     TEST_STRING("[[0.000, 0.000, 0.000, -0.000], "
                 "[0.000, 0.000, 0.000, -0.000], "