From 284a8017b76ce5c4fd82188439e288debf463046 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 17 Oct 2016 01:23:37 +0700 Subject: [PATCH] reformatting: cJSONUtils_Compare --- cJSON_Utils.c | 77 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index c717285..f1a2bea 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -242,31 +242,62 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path) return ret; } -static int cJSONUtils_Compare(cJSON *a,cJSON *b) +static int cJSONUtils_Compare(cJSON *a, cJSON *b) { - if (a->type!=b->type) return -1; /* mismatched type. */ - switch (a->type) - { - case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */ - case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; /* string mismatch. */ - case cJSON_Array: for (a=a->child,b=b->child;a && b;a=a->next,b=b->next) {int err=cJSONUtils_Compare(a,b);if (err) return err;} - return (a || b)?-4:0; /* array size mismatch. */ - case cJSON_Object: - cJSONUtils_SortObject(a); - cJSONUtils_SortObject(b); - a=a->child,b=b->child; - while (a && b) - { - int err; - if (cJSONUtils_strcasecmp(a->string,b->string)) return -6; /* missing member */ - err=cJSONUtils_Compare(a,b);if (err) return err; - a=a->next,b=b->next; - } - return (a || b)?-5:0; /* object length mismatch */ + if (a->type != b->type) + { + /* mismatched type. */ + return -1; + } + switch (a->type) + { + case cJSON_Number: + /* numeric mismatch. */ + return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0; + case cJSON_String: + /* string mismatch. */ + return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0; + case cJSON_Array: + for (a = a->child, b = b->child; a && b; a = a->next, b = b->next) + { + int err = cJSONUtils_Compare(a, b); + if (err) + { + return err; + } + } + /* array size mismatch? (one of both children is not NULL) */ + return (a || b) ? -4 : 0; + case cJSON_Object: + cJSONUtils_SortObject(a); + cJSONUtils_SortObject(b); + a = a->child; + b = b->child; + while (a && b) + { + int err; + /* compare object keys */ + if (cJSONUtils_strcasecmp(a->string, b->string)) + { + /* missing member */ + return -6; + } + err = cJSONUtils_Compare(a, b); + if (err) + { + return err; + } + a = a->next; + b = b->next; + } + /* object length mismatch (one of both children is not null) */ + return (a || b) ? -5 : 0; - default: break; - } - return 0; + default: + break; + } + /* null, true or false */ + return 0; } static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)