Skip to content

Commit

Permalink
Add string compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed May 26, 2021
1 parent 10c0195 commit 7fa6d16
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Develop

- Add string compare function

## 1.4.0

- Add support with input string with length specifier
Expand Down
14 changes: 14 additions & 0 deletions lwjson/src/include/lwjson/lwjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
* \}
*/
Expand Down
4 changes: 4 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 7fa6d16

Please sign in to comment.