mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/4] lib: jsmn: add case-insensitive comparison
Date: Thu, 21 Sep 2023 12:24:25 +0200	[thread overview]
Message-ID: <20230921102426.1109289-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230921102426.1109289-1-a.fatoum@pengutronix.de>

Users may want to do a case-insensitive comparison of tokens. Add simple
helpers for that.

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

diff --git a/include/jsmn.h b/include/jsmn.h
index 62197c2593b4..64b3b535ab6e 100644
--- a/include/jsmn.h
+++ b/include/jsmn.h
@@ -101,6 +101,9 @@ JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *to
 /** Returns `true` if `token` is to `str`. */
 JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token);
 
+/** Returns `true` if `token` is equal to `str`, ignoring case. */
+JSMN_API bool jsmn_strcase_eq(const char *str, const char *json, const jsmntok_t *token);
+
 /** Returns the token after the value at `tokens[0]`. */
 JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens);
 
diff --git a/lib/jsmn.c b/lib/jsmn.c
index 3d2ada7b7fdc..9e624f7518ad 100644
--- a/lib/jsmn.c
+++ b/lib/jsmn.c
@@ -418,6 +418,18 @@ JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *to
 	return token->type == JSMN_STRING && jsmn_eq(str, json, token);
 }
 
+static bool jsmn_case_eq(const char *val, const char *json, const jsmntok_t *token)
+{
+	size_t token_size = jsmn_token_size(token);
+	return strlen(val) == token_size
+		&& strncasecmp(json + token->start, val, token_size) == 0;
+}
+
+JSMN_API bool jsmn_strcase_eq(const char *str, const char *json, const jsmntok_t *token)
+{
+	return token->type == JSMN_STRING && jsmn_case_eq(str, json, token);
+}
+
 JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens)
 {
 	int max_index = tokens[0].end;
-- 
2.39.2




  parent reply	other threads:[~2023-09-21 10:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Ahmad Fatoum [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230921102426.1109289-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox