Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed May 26, 2021
2 parents ca0b164 + c050c10 commit 5b1d37e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dev/VisualStudio/lwjson_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
Expand Down
32 changes: 16 additions & 16 deletions docs/user-manual/data-access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down
2 changes: 1 addition & 1 deletion docs/user-manual/how-it-works.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 29 additions & 1 deletion lwjson/src/include/lwjson/lwjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#ifndef LWJSON_HDR_H
#define LWJSON_HDR_H
Expand Down Expand Up @@ -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;
}

/**
* \}
*/
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/include/lwjson/lwjson_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#ifndef LWJSON_HDR_OPT_H
#define LWJSON_HDR_OPT_H
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/include/lwjson/lwjson_opts_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#ifndef LWJSON_HDR_OPTS_H
#define LWJSON_HDR_OPTS_H
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/lwjson/lwjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#include <string.h>
#include "lwjson/lwjson.h"
Expand Down
2 changes: 1 addition & 1 deletion lwjson/src/lwjson/lwjson_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This file is part of LwJSON - Lightweight JSON format parser.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.4.0
* Version: v1.5.0
*/
#include <string.h>
#include <stdio.h>
Expand Down
8 changes: 8 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 5b1d37e

Please sign in to comment.