reformatting: cJSONUtils_Compare

This commit is contained in:
Max Bruckner 2016-10-17 01:23:37 +07:00
parent 1235c62235
commit 284a8017b7

View File

@ -242,31 +242,62 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
return ret; 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. */ if (a->type != b->type)
switch (a->type) {
{ /* mismatched type. */
case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */ return -1;
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;} switch (a->type)
return (a || b)?-4:0; /* array size mismatch. */ {
case cJSON_Object: case cJSON_Number:
cJSONUtils_SortObject(a); /* numeric mismatch. */
cJSONUtils_SortObject(b); return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
a=a->child,b=b->child; case cJSON_String:
while (a && b) /* string mismatch. */
{ return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
int err; case cJSON_Array:
if (cJSONUtils_strcasecmp(a->string,b->string)) return -6; /* missing member */ for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
err=cJSONUtils_Compare(a,b);if (err) return err; {
a=a->next,b=b->next; int err = cJSONUtils_Compare(a, b);
} if (err)
return (a || b)?-5:0; /* object length mismatch */ {
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; default:
} break;
return 0; }
/* null, true or false */
return 0;
} }
static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)