minor cleanup

Tue, 10 Dec 2024 21:40:39 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Dec 2024 21:40:39 +0100
changeset 1009
7650e722437e
parent 1008
3b69f025f083
child 1010
2d252c1c1d5d

minor cleanup

relates to #431

src/cx/json.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
--- a/src/cx/json.h	Tue Dec 10 00:52:15 2024 +0100
+++ b/src/cx/json.h	Tue Dec 10 21:40:39 2024 +0100
@@ -294,73 +294,73 @@
 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
 
 cx_attr_nonnull
-static inline bool cxJsonIsObject(CxJsonValue *value) {
+static inline bool cxJsonIsObject(const CxJsonValue *value) {
     return value->type == CX_JSON_OBJECT;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsArray(CxJsonValue *value) {
+static inline bool cxJsonIsArray(const CxJsonValue *value) {
     return value->type == CX_JSON_ARRAY;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsString(CxJsonValue *value) {
+static inline bool cxJsonIsString(const CxJsonValue *value) {
     return value->type == CX_JSON_STRING;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsNumber(CxJsonValue *value) {
+static inline bool cxJsonIsNumber(const CxJsonValue *value) {
     return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsInteger(CxJsonValue *value) {
+static inline bool cxJsonIsInteger(const CxJsonValue *value) {
     return value->type == CX_JSON_INTEGER;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsLiteral(CxJsonValue *value) {
+static inline bool cxJsonIsLiteral(const CxJsonValue *value) {
     return value->type == CX_JSON_LITERAL;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsBool(CxJsonValue *value) {
+static inline bool cxJsonIsBool(const CxJsonValue *value) {
     return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsTrue(CxJsonValue *value) {
+static inline bool cxJsonIsTrue(const CxJsonValue *value) {
     return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsFalse(CxJsonValue *value) {
+static inline bool cxJsonIsFalse(const CxJsonValue *value) {
     return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
 }
 
 cx_attr_nonnull
-static inline bool cxJsonIsNull(CxJsonValue *value) {
+static inline bool cxJsonIsNull(const CxJsonValue *value) {
     return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
 }
 
 cx_attr_nonnull
 cx_attr_returns_nonnull
-static inline char *cxJsonAsString(CxJsonValue *value) {
+static inline char *cxJsonAsString(const CxJsonValue *value) {
     return value->value.string.ptr;
 }
 
 cx_attr_nonnull
-static inline cxstring cxJsonAsCxString(CxJsonValue *value) {
+static inline cxstring cxJsonAsCxString(const CxJsonValue *value) {
     return cx_strcast(value->value.string);
 }
 
 cx_attr_nonnull
-static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) {
+static inline cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) {
     return value->value.string;
 }
 
 cx_attr_nonnull
-static inline double cxJsonAsDouble(CxJsonValue *value) {
+static inline double cxJsonAsDouble(const CxJsonValue *value) {
     if (value->type == CX_JSON_INTEGER) {
         return (double) value->value.integer;
     } else {
@@ -369,7 +369,7 @@
 }
 
 cx_attr_nonnull
-static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
+static inline int64_t cxJsonAsInteger(const CxJsonValue *value) {
     if (value->type == CX_JSON_INTEGER) {
         return value->value.integer;
     } else {
@@ -378,18 +378,18 @@
 }
 
 cx_attr_nonnull
-static inline bool cxJsonAsBool(CxJsonValue *value) {
+static inline bool cxJsonAsBool(const CxJsonValue *value) {
     return value->value.literal == CX_JSON_TRUE;
 }
 
 cx_attr_nonnull
-static inline size_t cxJsonArrSize(CxJsonValue *value) {
+static inline size_t cxJsonArrSize(const CxJsonValue *value) {
     return value->value.array.array_size;
 }
 
 cx_attr_nonnull
 cx_attr_returns_nonnull
-CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index);
+CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index);
 
 // TODO: add cxJsonArrIter()
 
@@ -397,7 +397,7 @@
 cx_attr_nonnull
 cx_attr_returns_nonnull
 cx_attr_cstr_arg(2)
-CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char* name);
+CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char* name);
 
 #ifdef __cplusplus
 }
--- a/src/json.c	Tue Dec 10 00:52:15 2024 +0100
+++ b/src/json.c	Tue Dec 10 21:40:39 2024 +0100
@@ -48,20 +48,6 @@
     }
 }
 
-
-static int token_isliteral(const char *content, size_t length) {
-    if (length == 4) {
-        if (!memcmp(content, "true", 4)) {
-            return 1;
-        } else if (!memcmp(content, "null", 4)) {
-            return 1;
-        }
-    } else if (length == 5 && !memcmp(content, "false", 5)) {
-        return 1;
-    }
-    return 0;
-}
-
 static int num_isexp(const char *content, size_t length, size_t pos) {
     if (pos >= length) {
         return 0;
@@ -123,7 +109,9 @@
     if (isstring) {
         ttype = CX_JSON_TOKEN_STRING;
     } else {
-        if (token_isliteral(str.ptr, str.length)) {
+        cxstring s = cx_strcast(str);
+        if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false"))
+            || !cx_strcmp(s, CX_STR("null"))) {
             ttype = CX_JSON_TOKEN_LITERAL;
         } else {
             ttype = token_numbertype(str.ptr, str.length);
@@ -383,7 +371,7 @@
     return v;
 }
 
-static int json_obj_add_entry(CxJson *json, char *name) {
+static int json_obj_add_entry(const CxJson *json, char *name) {
     CxJsonObjValue kv = {name, NULL};
     assert(json->vbuf_size > 0);
     CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
@@ -676,18 +664,17 @@
     cxFree(value->allocator, value);
 }
 
-CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index) {
+CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) {
     if (index >= value->value.array.array_size) {
         return &cx_json_value_nothing;
     }
     return value->value.array.array[index];
 }
 
-CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char *name) {
-    CxJsonObject *obj = &(value->value.object);
+CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
+    const CxJsonObject *obj = &(value->value.object);
     // TODO: think about sorting the object so that we can use binary search here
     for (size_t i = 0; i < obj->values_size; i++) {
-        // TODO: we might want to store names as cxmutstr
         if (0 == strcmp(name, obj->values[i].name)) {
             return obj->values[i].value;
         }

mercurial