From 10c01955fa4167ae8afc3958803f68df0d0257e5 Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Fri, 14 May 2021 10:36:40 +0200 Subject: [PATCH 1/4] Fix docs problems --- docs/user-manual/data-access.rst | 32 +++++++++++++++---------------- docs/user-manual/how-it-works.rst | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) 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. From 7fa6d1687bdc3161de5fc0b423ecb13e1c141e31 Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Wed, 26 May 2021 10:47:20 +0200 Subject: [PATCH 2/4] Add string compare function --- CHANGELOG.md | 2 ++ lwjson/src/include/lwjson/lwjson.h | 14 ++++++++++++++ test/test.c | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e70d8..001ce3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Develop +- Add string compare function + ## 1.4.0 - Add support with input string with length specifier diff --git a/lwjson/src/include/lwjson/lwjson.h b/lwjson/src/include/lwjson/lwjson.h index 201f503..ae17da1 100644 --- a/lwjson/src/include/lwjson/lwjson.h +++ b/lwjson/src/include/lwjson/lwjson.h @@ -192,6 +192,20 @@ 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; +} + /** * \} */ diff --git a/test/test.c b/test/test.c index 18a163a..e01ce02 100644 --- a/test/test.c +++ b/test/test.c @@ -379,6 +379,10 @@ 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")); + #undef RUN_TEST /* Call this once JSON usage is finished */ From cc59de61bcf1653be73d86443e9b1f9787ec04e9 Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Wed, 26 May 2021 20:22:36 +0200 Subject: [PATCH 3/4] Add string compare with length --- CHANGELOG.md | 3 ++- lwjson/src/include/lwjson/lwjson.h | 14 ++++++++++++++ test/test.c | 6 +++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 001ce3f..46fea77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Develop -- Add string compare function +- Add string compare feature +- Add string compare with custom length feature ## 1.4.0 diff --git a/lwjson/src/include/lwjson/lwjson.h b/lwjson/src/include/lwjson/lwjson.h index ae17da1..9f9a5a7 100644 --- a/lwjson/src/include/lwjson/lwjson.h +++ b/lwjson/src/include/lwjson/lwjson.h @@ -206,6 +206,20 @@ lwjson_string_compare(const lwjson_token_t* token, const char* str) { 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/test/test.c b/test/test.c index e01ce02..c8eb562 100644 --- a/test/test.c +++ b/test/test.c @@ -381,7 +381,11 @@ test_find_function(void) { /* Check string compare */ RUN_TEST((token = lwjson_find_ex(&lwjson, NULL, "my_obj.arr.#.#.my_key")) != NULL - && lwjson_string_compare(token, "my_text")); + && 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 From c050c101d59619fb924c73361b7cd6c4a46e0eaa Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Wed, 26 May 2021 20:23:58 +0200 Subject: [PATCH 4/4] Set version to 1.5 --- CHANGELOG.md | 2 ++ dev/VisualStudio/lwjson_opts.h | 2 +- lwjson/src/include/lwjson/lwjson.h | 2 +- lwjson/src/include/lwjson/lwjson_opt.h | 2 +- lwjson/src/include/lwjson/lwjson_opts_template.h | 2 +- lwjson/src/lwjson/lwjson.c | 2 +- lwjson/src/lwjson/lwjson_debug.c | 2 +- 7 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46fea77..4041a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Develop +## 1.5.0 + - Add string compare feature - Add string compare with custom length feature 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/lwjson/src/include/lwjson/lwjson.h b/lwjson/src/include/lwjson/lwjson.h index 9f9a5a7..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 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