mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
parse_array: simplify to one do-while loop
This commit is contained in:
parent
b6974ecbc9
commit
24dbf29360
79
cJSON.c
79
cJSON.c
@ -1027,68 +1027,69 @@ static unsigned char *print_value(const cJSON *item, size_t depth, cjbool fmt, p
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Build an array from input text. */
|
/* Build an array from input text. */
|
||||||
static const unsigned char *parse_array(cJSON *item, const unsigned char *value, const unsigned char **ep)
|
static const unsigned char *parse_array(cJSON *item, const unsigned char *value, const unsigned char **error_pointer)
|
||||||
{
|
{
|
||||||
cJSON *head = NULL; /* head of the linked list */
|
cJSON *head = NULL; /* head of the linked list */
|
||||||
cJSON *child = NULL;
|
cJSON *current_item = NULL;
|
||||||
|
|
||||||
if (*value != '[')
|
if (*value != '[')
|
||||||
{
|
{
|
||||||
/* not an array! */
|
/* not an array */
|
||||||
*ep = value;
|
*error_pointer = value;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
value = skip(value + 1);
|
value = skip(value + 1); /* skip whitespace */
|
||||||
if (*value == ']')
|
if (*value == ']')
|
||||||
{
|
{
|
||||||
/* empty array. */
|
/* empty array */
|
||||||
goto success;
|
goto success;
|
||||||
}
|
}
|
||||||
|
|
||||||
head = child = cJSON_New_Item();
|
/* step back to character in front of the first element */
|
||||||
if (!child)
|
value--;
|
||||||
{
|
|
||||||
/* memory fail */
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
/* skip any spacing, get the value. */
|
|
||||||
value = skip(parse_value(child, skip(value), ep));
|
|
||||||
if (!value)
|
|
||||||
{
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* loop through the comma separated array elements */
|
/* loop through the comma separated array elements */
|
||||||
while (*value == ',')
|
do
|
||||||
{
|
{
|
||||||
cJSON *new_item = NULL;
|
/* allocate next item */
|
||||||
if (!(new_item = cJSON_New_Item()))
|
cJSON *new_item = cJSON_New_Item();
|
||||||
|
if (new_item == NULL)
|
||||||
{
|
{
|
||||||
/* memory fail */
|
goto fail; /* allocation failure */
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
/* add new item to end of the linked list */
|
|
||||||
child->next = new_item;
|
|
||||||
new_item->prev = child;
|
|
||||||
child = new_item;
|
|
||||||
|
|
||||||
/* go to the next comma */
|
/* attach next item to list */
|
||||||
value = skip(parse_value(child, skip(value + 1), ep));
|
if (head == NULL)
|
||||||
if (!value)
|
|
||||||
{
|
{
|
||||||
/* memory fail */
|
/* start the linked list */
|
||||||
goto fail;
|
current_item = head = new_item;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* add to the end and advance */
|
||||||
|
current_item->next = new_item;
|
||||||
|
new_item->prev = current_item;
|
||||||
|
current_item = new_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* parse next value */
|
||||||
|
value = skip(value + 1); /* skip whitespace before value */
|
||||||
|
value = parse_value(current_item, value, error_pointer);
|
||||||
|
value = skip(value); /* skip whitespace after value */
|
||||||
|
if (value == NULL)
|
||||||
|
{
|
||||||
|
goto fail; /* failed to parse value */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
while (*value == ',');
|
||||||
|
|
||||||
if (*value == ']')
|
if (*value == ']')
|
||||||
{
|
{
|
||||||
/* end of array */
|
goto success; /* end of array */
|
||||||
goto success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* malformed. */
|
/* malformed array */
|
||||||
*ep = value;
|
*error_pointer = value;
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
success:
|
success:
|
||||||
@ -1098,9 +1099,9 @@ success:
|
|||||||
return value + 1;
|
return value + 1;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
if (child != NULL)
|
if (head != NULL)
|
||||||
{
|
{
|
||||||
cJSON_Delete(child);
|
cJSON_Delete(head);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
Loading…
Reference in New Issue
Block a user