From 61cf31c92d4ee4956f5c443f83e4774d70605f1b Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 11 Jan 2022 19:36:28 +0100 Subject: [PATCH] add json string compare functions --- application/json.c | 17 +++++++++++++++++ application/json.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/application/json.c b/application/json.c index 4dd672c..5099d9b 100644 --- a/application/json.c +++ b/application/json.c @@ -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); +} diff --git a/application/json.h b/application/json.h index 659c7f6..0731cca 100644 --- a/application/json.h +++ b/application/json.h @@ -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 -- 1.8.3.1