From 5ac968c28b7570e667578e360631619e2fc48aeb Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 25 Oct 2022 01:06:15 +0200 Subject: [PATCH] Add MJSON_DEBUG for plen2 --- README.md | 1 + src/mjson.c | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9dbee3e..6481415 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ int main(void) { - `-D MJSON_ENABLE_MERGE=0` disable `mjson_merge()`, default: enabled - `-D MJSON_ENABLE_NEXT=0` disable `mjson_next()`, default: enabled - `-D MJSON_REALLOC=my_realloc` redefine realloc() used by `mjson_print_dynamic_buf()`, default: realloc +- `-D MJSON_DEBUG=true` enable `PLEN2` debug mode # Parsing API diff --git a/src/mjson.c b/src/mjson.c index 55ba507..3dacee0 100644 --- a/src/mjson.c +++ b/src/mjson.c @@ -191,11 +191,31 @@ static int plen1(const char *s) { return n; } +/* + * This function calculates the position of the `.` + * when the json key have a `.` in the key name + * + * Input: the json key + * Output: the position after the `.` + */ static int plen2(const char *s) { - int i = 0, n = 0; - while (s[i] != '\0' && s[i] != '.' && s[i] != '[') - n++, i += s[i] == '\\' ? 2 : 1; - // printf("PLEN: s: [%s], [%.*s] => %d\n", s, i, s, n); + int i = 0; + +#if MJSON_DEBUG + int n = 0; +#endif + + while (s[i] != '\0' && s[i] != '.' && s[i] != '['){ + i += s[i] == '\\' ? 2 : 1; +#if MJSON_DEBUG + n++; +#endif + } + +#if MJSON_DEBUG + printf("PLEN2: s: [%s], [%.*s] => %d\n", s, i, s, n); +#endif + return i; }