mirror of
https://github.com/DaveGamble/cJSON
synced 2024-12-23 23:22:09 +00:00
parse_number: Use parse_buffer
This commit is contained in:
parent
fef1c45b92
commit
f69b109c9f
25
cJSON.c
25
cJSON.c
@ -202,7 +202,7 @@ typedef struct
|
|||||||
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
|
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
|
||||||
|
|
||||||
/* Parse the input text to generate a number, and populate the result into item. */
|
/* 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;
|
double number = 0;
|
||||||
unsigned char *after_end = NULL;
|
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();
|
unsigned char decimal_point = get_decimal_point();
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
||||||
if (input == NULL)
|
if ((input_buffer == NULL) || (input_buffer->content == NULL))
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy the number into a temporary buffer and replace '.' with the decimal point
|
/* copy the number into a temporary buffer and replace '.' with the decimal point
|
||||||
* of the current locale (for strtod) */
|
* of the current locale (for strtod)
|
||||||
for (i = 0; (i < (sizeof(number_c_string) - 1)) && (input[i] != '\0'); i++)
|
* 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 '0':
|
||||||
case '1':
|
case '1':
|
||||||
@ -235,7 +236,7 @@ static const unsigned char *parse_number(cJSON * const item, const unsigned char
|
|||||||
case '-':
|
case '-':
|
||||||
case 'e':
|
case 'e':
|
||||||
case 'E':
|
case 'E':
|
||||||
number_c_string[i] = input[i];
|
number_c_string[i] = buffer_at_offset(input_buffer)[i];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '.':
|
case '.':
|
||||||
@ -273,7 +274,8 @@ loop_end:
|
|||||||
|
|
||||||
item->type = cJSON_Number;
|
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 */
|
/* 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 */
|
/* 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'))))
|
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));
|
return parse_number(item, input_buffer);
|
||||||
if (content_pointer == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
input_buffer->offset = (size_t)(content_pointer - input_buffer->content);
|
|
||||||
return buffer_at_offset(input_buffer);
|
|
||||||
}
|
}
|
||||||
/* array */
|
/* array */
|
||||||
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
|
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
|
||||||
|
@ -45,7 +45,12 @@ static void assert_is_number(cJSON *number_item)
|
|||||||
|
|
||||||
static void assert_parse_number(const char *string, int integer, double real)
|
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);
|
assert_is_number(item);
|
||||||
TEST_ASSERT_EQUAL_INT(integer, item->valueint);
|
TEST_ASSERT_EQUAL_INT(integer, item->valueint);
|
||||||
TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);
|
TEST_ASSERT_EQUAL_DOUBLE(real, item->valuedouble);
|
||||||
|
Loading…
Reference in New Issue
Block a user