add json string compare functions
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 11 Jan 2022 18:36:28 +0000 (19:36 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 11 Jan 2022 18:36:28 +0000 (19:36 +0100)
application/json.c
application/json.h

index 4dd672c..5099d9b 100644 (file)
@@ -885,3 +885,20 @@ void json_value_free(JSONValue *value) {
     }
     free(value);
 }
+
+int json_strcmp(JSONValue *jsstr, const char *str) {
+    return json_strncmp(jsstr, str, strlen(str));
+}
+
+int json_strncmp(JSONValue *jsstr, const char *str, size_t slen) {
+    if(jsstr->type != JSON_STRING) {
+        return -1;
+    }
+    size_t jsstrlen = jsstr->value.string.length;
+    
+    if(jsstrlen != slen) {
+        return jsstrlen > slen ? 1 : -1;
+    }
+    
+    return memcmp(jsstr, jsstr->value.string.string, slen);
+}
index 659c7f6..0731cca 100644 (file)
@@ -206,6 +206,9 @@ JSONValue* json_array_get(JSONArray *array, size_t i);
 
 void json_value_free(JSONValue *value);
 
+int json_strcmp(JSONValue *jsstr, const char *str);
+int json_strncmp(JSONValue *jsstr, const char *str, size_t slen);
+
 #ifdef __cplusplus
 }
 #endif