diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e70d8..4041a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Develop +## 1.5.0 + +- Add string compare feature +- Add string compare with custom length feature + ## 1.4.0 - Add support with input string with length specifier diff --git a/dev/VisualStudio/lwjson_opts.h b/dev/VisualStudio/lwjson_opts.h index e76da37..2a27683 100644 --- a/dev/VisualStudio/lwjson_opts.h +++ b/dev/VisualStudio/lwjson_opts.h @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWJSON_HDR_OPTS_H #define LWJSON_HDR_OPTS_H diff --git a/docs/user-manual/data-access.rst b/docs/user-manual/data-access.rst index a1659dd..e3997a6 100644 --- a/docs/user-manual/data-access.rst +++ b/docs/user-manual/data-access.rst @@ -43,22 +43,22 @@ Let's consider following JSON as input: .. code:: { - "name":"John", - "born": { - "city": "Munich", - "year": 1993 - } - "cars":[ - { - "brand":"Porsche", - "year":2018 - }, - { - "brand":"Range", - "year":2020, - "repainted":true - } - ] + "name":"John", + "born": { + "city": "Munich", + "year": 1993 + }, + "cars":[ + { + "brand":"Porsche", + "year":2018 + }, + { + "brand":"Range", + "year":2020, + "repainted":true + } + ] } There is one *John*, born in *Munich* in ``1993`` and has ``2`` cars, *Porsche* and *Range*. diff --git a/docs/user-manual/how-it-works.rst b/docs/user-manual/how-it-works.rst index 377edf8..caa7b45 100644 --- a/docs/user-manual/how-it-works.rst +++ b/docs/user-manual/how-it-works.rst @@ -19,7 +19,7 @@ Each token consists of: As an example, JSON text ``{"mykey":"myvalue"}`` will be parsed into ``2`` tokens: * First token is the opening bracket and has type *object* as it holds children tokens -* Second token has name ``"mykey"``, its type is *string* and value is ``myvalue`` +* Second token has name ``mykey``, its type is *string* and value is ``myvalue`` .. warning:: When JSON input string is parsed, create tokens use input string as a reference. diff --git a/lwjson/src/include/lwjson/lwjson.h b/lwjson/src/include/lwjson/lwjson.h index 201f503..22cd303 100644 --- a/lwjson/src/include/lwjson/lwjson.h +++ b/lwjson/src/include/lwjson/lwjson.h @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWJSON_HDR_H #define LWJSON_HDR_H @@ -192,6 +192,34 @@ lwjson_get_val_string(const lwjson_token_t* token, size_t* str_len) { */ #define lwjson_get_val_string_length(token) ((size_t)(((token) != NULL && (token)->type == LWJSON_TYPE_STRING) ? (token)->u.str.token_value_len : 0)) +/** + * \brief Compare string token with user input string for a case-sensitive match + * \param[in] token: Token with string type + * \param[out] str: String to compare + * \return `1` if equal, `0` otherwise + */ +static inline uint8_t +lwjson_string_compare(const lwjson_token_t* token, const char* str) { + if (token != NULL && token->type == LWJSON_TYPE_STRING) { + return strncmp(token->u.str.token_value, str, token->u.str.token_value_len) == 0; + } + return 0; +} + +/** + * \brief Compare string token with user input string for a case-sensitive match + * \param[in] token: Token with string type + * \param[out] str: String to compare + * \return `1` if equal, `0` otherwise + */ +static inline uint8_t +lwjson_string_compare_n(const lwjson_token_t* token, const char* str, size_t len) { + if (token != NULL && token->type == LWJSON_TYPE_STRING && len <= token->u.str.token_value_len) { + return strncmp(token->u.str.token_value, str, len) == 0; + } + return 0; +} + /** * \} */ diff --git a/lwjson/src/include/lwjson/lwjson_opt.h b/lwjson/src/include/lwjson/lwjson_opt.h index 8ae3d3c..f36288f 100644 --- a/lwjson/src/include/lwjson/lwjson_opt.h +++ b/lwjson/src/include/lwjson/lwjson_opt.h @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWJSON_HDR_OPT_H #define LWJSON_HDR_OPT_H diff --git a/lwjson/src/include/lwjson/lwjson_opts_template.h b/lwjson/src/include/lwjson/lwjson_opts_template.h index 51d444f..e4c0006 100644 --- a/lwjson/src/include/lwjson/lwjson_opts_template.h +++ b/lwjson/src/include/lwjson/lwjson_opts_template.h @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWJSON_HDR_OPTS_H #define LWJSON_HDR_OPTS_H diff --git a/lwjson/src/lwjson/lwjson.c b/lwjson/src/lwjson/lwjson.c index 62c2e0c..a1659c0 100644 --- a/lwjson/src/lwjson/lwjson.c +++ b/lwjson/src/lwjson/lwjson.c @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include #include "lwjson/lwjson.h" diff --git a/lwjson/src/lwjson/lwjson_debug.c b/lwjson/src/lwjson/lwjson_debug.c index c6b2a97..7b9ddf5 100644 --- a/lwjson/src/lwjson/lwjson_debug.c +++ b/lwjson/src/lwjson/lwjson_debug.c @@ -29,7 +29,7 @@ * This file is part of LwJSON - Lightweight JSON format parser. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include #include diff --git a/test/test.c b/test/test.c index 18a163a..c8eb562 100644 --- a/test/test.c +++ b/test/test.c @@ -379,6 +379,14 @@ test_find_function(void) { && lwjson_get_val_string_length(token) == 11 && strncmp(token->u.str.token_value, "\\t\\u1234abc", 11) == 0); + /* Check string compare */ + RUN_TEST((token = lwjson_find_ex(&lwjson, NULL, "my_obj.arr.#.#.my_key")) != NULL + && lwjson_string_compare(token, "my_text")); + RUN_TEST((token = lwjson_find_ex(&lwjson, NULL, "my_obj.arr.#.#.my_key")) != NULL + && lwjson_string_compare_n(token, "my_text", 3)); + RUN_TEST((token = lwjson_find_ex(&lwjson, NULL, "my_obj.arr.#.#.my_key")) != NULL + && !lwjson_string_compare_n(token, "my_stext", 4)); /* Must be a fail */ + #undef RUN_TEST /* Call this once JSON usage is finished */