mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-23 06:54:30 +01:00
common: add length-limited versions of json_streq and json_get_member.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
2b6cdfeb5a
commit
24b6132ee8
@@ -32,13 +32,17 @@ int json_tok_full_len(const jsmntok_t *t)
|
||||
return t->end - t->start;
|
||||
}
|
||||
|
||||
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
|
||||
bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
|
||||
const char *str, size_t len)
|
||||
{
|
||||
if (tok->type != JSMN_STRING)
|
||||
return false;
|
||||
if (tok->end - tok->start != strlen(str))
|
||||
return false;
|
||||
return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0;
|
||||
return memeq(buffer + tok->start, tok->end - tok->start, str, len);
|
||||
}
|
||||
|
||||
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
|
||||
{
|
||||
return json_tok_strneq(buffer, tok, str, strlen(str));
|
||||
}
|
||||
|
||||
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
|
||||
@@ -299,8 +303,8 @@ const jsmntok_t *json_next(const jsmntok_t *tok)
|
||||
return t;
|
||||
}
|
||||
|
||||
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label)
|
||||
const jsmntok_t *json_get_membern(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label, size_t len)
|
||||
{
|
||||
const jsmntok_t *t;
|
||||
size_t i;
|
||||
@@ -309,12 +313,18 @@ const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
||||
return NULL;
|
||||
|
||||
json_for_each_obj(i, t, tok)
|
||||
if (json_tok_streq(buffer, t, label))
|
||||
if (json_tok_strneq(buffer, t, label, len))
|
||||
return t + 1;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label)
|
||||
{
|
||||
return json_get_membern(buffer, tok, label, strlen(label));
|
||||
}
|
||||
|
||||
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
|
||||
{
|
||||
const jsmntok_t *t;
|
||||
|
||||
@@ -26,6 +26,10 @@ int json_tok_full_len(const jsmntok_t *t);
|
||||
/* Is this a string equal to str? */
|
||||
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
|
||||
|
||||
/* Is this a string equal to str of length len? */
|
||||
bool json_tok_strneq(const char *buffer, const jsmntok_t *tok,
|
||||
const char *str, size_t len);
|
||||
|
||||
/* Does this string token start with prefix? */
|
||||
bool json_tok_startswith(const char *buffer, const jsmntok_t *tok,
|
||||
const char *prefix);
|
||||
@@ -90,6 +94,10 @@ const jsmntok_t *json_next(const jsmntok_t *tok);
|
||||
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label);
|
||||
|
||||
/* Get top-level member, with explicit label length */
|
||||
const jsmntok_t *json_get_membern(const char *buffer, const jsmntok_t tok[],
|
||||
const char *label, size_t len);
|
||||
|
||||
/* Get index'th array member. */
|
||||
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user