print_array: simplify code

This commit is contained in:
Max Bruckner 2017-02-20 15:12:10 +01:00
parent 8c1ed3ab82
commit 6d5a7c8c40
1 changed files with 8 additions and 27 deletions

35
cJSON.c
View File

@ -1156,51 +1156,31 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth,
{ {
unsigned char *output = NULL; unsigned char *output = NULL;
unsigned char *output_pointer = NULL; unsigned char *output_pointer = NULL;
size_t length = 5; size_t length = 0;
cJSON *current_element = item->child; cJSON *current_element = item->child;
size_t array_length = 0; size_t output_offset = 0;
size_t i = 0;
cjbool fail = false;
if (output_buffer == NULL) if (output_buffer == NULL)
{ {
return NULL; return NULL;
} }
/* How many entries in the array? */
while (current_element)
{
array_length++;
current_element = current_element->next;
}
/* Explicitly handle empty arrays */
if (array_length == 0)
{
output = ensure(output_buffer, 3);
if (output != NULL)
{
strcpy((char*)output, "[]");
}
return output;
}
/* Compose the output array. */ /* Compose the output array. */
/* opening square bracket */ /* opening square bracket */
i = output_buffer->offset; output_offset = output_buffer->offset;
output_pointer = ensure(output_buffer, 1); output_pointer = ensure(output_buffer, 1);
if (output_pointer == NULL) if (output_pointer == NULL)
{ {
return NULL; return NULL;
} }
*output_pointer = '['; *output_pointer = '[';
output_buffer->offset++; output_buffer->offset++;
current_element = item->child; current_element = item->child;
while (current_element && !fail) while (current_element != NULL)
{ {
if (!print_value(current_element, depth + 1, format, output_buffer)) if (print_value(current_element, depth + 1, format, output_buffer) == NULL)
{ {
return NULL; return NULL;
} }
@ -1223,6 +1203,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth,
} }
current_element = current_element->next; current_element = current_element->next;
} }
output_pointer = ensure(output_buffer, 2); output_pointer = ensure(output_buffer, 2);
if (output_pointer == NULL) if (output_pointer == NULL)
{ {
@ -1230,7 +1211,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth,
} }
*output_pointer++ = ']'; *output_pointer++ = ']';
*output_pointer = '\0'; *output_pointer = '\0';
output = (output_buffer->buffer) + i; output = output_buffer->buffer + output_offset;
return output; return output;
} }