From 6978da3131db9ef592a9068c09d9466aa6db6276 Mon Sep 17 00:00:00 2001 From: Feng Guo Date: Wed, 10 Sep 2025 21:41:37 +0800 Subject: [PATCH 1/3] Optimization of the case_insensitive_strcmp function --- cJSON.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cJSON.c b/cJSON.c index ca824f0e..ff25193b 100644 --- a/cJSON.c +++ b/cJSON.c @@ -132,6 +132,8 @@ CJSON_PUBLIC(const char*) cJSON_Version(void) /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) { + int l = 0; + int r = 0; if ((string1 == NULL) || (string2 == NULL)) { return 1; @@ -142,15 +144,16 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned return 0; } - for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + while (true) { - if (*string1 == '\0') + l = *(string1++); + r = *(string2++); + if (l != r || l == 0) { - return 0; + return l - r; } } - - return tolower(*string1) - tolower(*string2); + return 0; } typedef struct internal_hooks From c943d5b1d11c3a6dabc428df83889419330d5f8e Mon Sep 17 00:00:00 2001 From: Feng Guo Date: Wed, 10 Sep 2025 22:38:29 +0800 Subject: [PATCH 2/3] Optimization of the case_insensitive_strcmp function --- cJSON.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cJSON.c b/cJSON.c index bd77aaa7..2b827ec1 100644 --- a/cJSON.c +++ b/cJSON.c @@ -117,7 +117,7 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) } /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ -#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 19) +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif @@ -146,8 +146,8 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned while (true) { - l = *(string1++); - r = *(string2++); + l = tolower(*string1++); + r = tolower(*string2++); if (l != r || l == 0) { return l - r; From 5a89b46b6c01148e06e847fc2e988a97428ae586 Mon Sep 17 00:00:00 2001 From: Feng Guo Date: Wed, 10 Sep 2025 22:57:07 +0800 Subject: [PATCH 3/3] Optimization of the case_insensitive_strcmp function --- cJSON.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 2b827ec1..875f5201 100644 --- a/cJSON.c +++ b/cJSON.c @@ -117,7 +117,7 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) } /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ -#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 19) #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif