Skip to content

Commit

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

## Develop

- Add string compare function
- Add string compare feature
- Add string compare with custom length feature

## 1.4.0

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 @@ -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;
}

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

Expand Down

0 comments on commit cc59de6

Please sign in to comment.