mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper
@ 2023-09-21 10:24 Ahmad Fatoum
  2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-09-21 10:24 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

To make code a bit more terse, add a simple helper to get a token's size
and start using it in some of the existing jsmn helpers.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/jsmn.h | 5 +++++
 lib/jsmn.c     | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/jsmn.h b/include/jsmn.h
index 8f6db8d534f4..2dd1d5ffc08b 100644
--- a/include/jsmn.h
+++ b/include/jsmn.h
@@ -84,6 +84,11 @@ JSMN_API void jsmn_init(jsmn_parser *parser);
 JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
 			jsmntok_t *tokens, const unsigned int num_tokens);
 
+static inline int jsmn_token_size(const jsmntok_t *token)
+{
+	return token->end - token->start;
+}
+
 /** Returns `true` if `token` is a string and equal to `str`. */
 JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *token);
 
diff --git a/lib/jsmn.c b/lib/jsmn.c
index 7bdcc90f2f4b..9eeed790d93d 100644
--- a/lib/jsmn.c
+++ b/lib/jsmn.c
@@ -372,7 +372,7 @@ JSMN_API void jsmn_init(jsmn_parser *parser) {
 
 JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token)
 {
-	size_t token_size = token->end - token->start;
+	size_t token_size = jsmn_token_size(token);
 	return strlen(val) == token_size
 		&& strncmp(json + token->start, val, token_size) == 0;
 }
@@ -446,7 +446,7 @@ JSMN_API char *jsmn_strcpy(const char *path[], const char *json,
 	if (!node || node->type != JSMN_STRING)
 		return NULL;
 
-	value_size = node->end - node->start;
+	value_size = jsmn_token_size(node);
 	value = malloc(value_size + 1);
 	if (value) {
 		strncpy(value, json + node->start, value_size);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-09-21 13:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum
2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum
2023-09-21 10:24 ` [PATCH 3/4] lib: jsmn: add case-insensitive comparison Ahmad Fatoum
2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum
2023-09-21 13:03   ` Sascha Hauer
2023-09-21 13:04     ` Ahmad Fatoum
2023-09-21 13:08       ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox