Browse Source

parse_number: Use parse_buffer

pull/148/head
Max Bruckner 8 years ago
parent
commit
f69b109c9f
  1. 25
      cJSON.c
  2. 7
      tests/parse_number.c

25
cJSON.c

@ -202,7 +202,7 @@ typedef struct
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
/* Parse the input text to generate a number, and populate the result into item. */
static const unsigned char *parse_number(cJSON * const item, const unsigned char * const input)
static const unsigned char *parse_number(cJSON * const item, parse_buffer * const input_buffer)
{
double number = 0;
unsigned char *after_end = NULL;
@ -210,16 +210,17 @@ static const unsigned char *parse_number(cJSON * const item, const unsigned char
unsigned char decimal_point = get_decimal_point();
size_t i = 0;
if (input == NULL)
if ((input_buffer == NULL) || (input_buffer->content == NULL))
{
return NULL;
}
/* copy the number into a temporary buffer and replace '.' with the decimal point
* of the current locale (for strtod) */
for (i = 0; (i < (sizeof(number_c_string) - 1)) && (input[i] != '\0'); i++)
* of the current locale (for strtod)
* This also takes care of '\0' not necessarily being available for marking the end of the input */
for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
{
switch (input[i])
switch (buffer_at_offset(input_buffer)[i])
{
case '0':
case '1':
@ -235,7 +236,7 @@ static const unsigned char *parse_number(cJSON * const item, const unsigned char
case '-':
case 'e':
case 'E':
number_c_string[i] = input[i];
number_c_string[i] = buffer_at_offset(input_buffer)[i];
break;
case '.':
@ -273,7 +274,8 @@ loop_end:
item->type = cJSON_Number;
return input + (after_end - number_c_string);
input_buffer->offset += (size_t)(after_end - number_c_string);
return buffer_at_offset(input_buffer);
}
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
@ -1151,14 +1153,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
/* number */
if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
{
content_pointer = parse_number(item, buffer_at_offset(input_buffer));
if (content_pointer == NULL)
{
return NULL;
}
input_buffer->offset = (size_t)(content_pointer - input_buffer->content);
return buffer_at_offset(input_buffer);
return parse_number(item, input_buffer);
}
/* array */
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))

7
tests/parse_number.c

@ -45,7 +45,12 @@ static void assert_is_number(cJSON *number_item)
static void assert_parse_number(const char *string, int integer, double real)
{
TEST_ASSERT_NOT_NULL(parse_number(item, (const unsigned char*)string));
parse_buffer buffer;
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.offset = 0;
TEST_ASSERT_NOT_NULL(parse_number(item, &buffer));
assert_is_number(item);
TEST_ASSERT_EQUAL_INT(integer, item->valueint);
TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);

Loading…
Cancel
Save