From b8e3673d0fc0f83fb6095e720411d0b0ed79b9ce Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 30 Apr 2017 12:54:00 +0200 Subject: [PATCH] refactor cJSONUtils_PointerEncodedstrlen --- cJSON_Utils.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index d07179f..0fd18ab 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -107,18 +107,20 @@ static int cJSONUtils_Pstrcasecmp(const unsigned char *name, const unsigned char return 0; } -static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *s) +/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */ +static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *string) { - size_t l = 0; - for (; *s; (void)s++, l++) + size_t length; + for (length = 0; *string != '\0'; (void)string++, length++) { - if ((*s == '~') || (*s == '/')) + /* character needs to be escaped? */ + if ((*string == '~') || (*string == '/')) { - l++; + length++; } } - return l; + return length; } static void cJSONUtils_PointerEncodedstrcpy(unsigned char *d, const unsigned char *s)