From b16bcc3c194a869629dc09d66def7e1f0e7ec2b7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 30 Apr 2017 12:53:00 +0200 Subject: [PATCH] refactor cJSONUtils_Pstrcasecmp --- cJSON_Utils.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index b69cfa0..d07179f 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -70,34 +70,35 @@ static int cJSONUtils_strcasecmp(const unsigned char *string1, const unsigned ch return tolower(*string1) - tolower(*string2); } -/* JSON Pointer implementation: */ -static int cJSONUtils_Pstrcasecmp(const unsigned char *a, const unsigned char *e) +/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */ +static int cJSONUtils_Pstrcasecmp(const unsigned char *name, const unsigned char *pointer) { - if (!a || !e) + if ((name == NULL) || (pointer == NULL)) { - return (a == e) ? 0 : 1; /* both NULL? */ + return 1; } - for (; *a && *e && (*e != '/'); (void)a++, e++) /* compare until next '/' */ + + for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */ { - if (*e == '~') + if (*pointer == '~') { /* check for escaped '~' (~0) and '/' (~1) */ - if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/'))) + if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/'))) { - /* invalid escape sequence or wrong character in *a */ + /* invalid escape sequence or wrong character in *name */ return 1; } else { - e++; + pointer++; } } - else if (tolower(*a) != tolower(*e)) + else if (tolower(*name) != tolower(*pointer)) { return 1; } } - if (((*e != 0) && (*e != '/')) != (*a != 0)) + if (((*pointer != 0) && (*pointer != '/')) != (*name != 0)) { /* one string has ended, the other not */ return 1;