mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Optimise string parsing when there are no escape characters
This resulted in approximately 40% reduction in instructions used in parse_string() when parsing a 4MB JSON file, as measured in callgrind.
This commit is contained in:
parent
bb27ffa152
commit
e64b984ddc
16
cJSON.c
16
cJSON.c
@ -776,6 +776,8 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
|
||||
const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
|
||||
unsigned char *output_pointer = NULL;
|
||||
unsigned char *output = NULL;
|
||||
size_t skipped_bytes = 0;
|
||||
size_t allocation_length = 0;
|
||||
|
||||
/* not a string */
|
||||
if (buffer_at_offset(input_buffer)[0] != '\"')
|
||||
@ -785,8 +787,6 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
|
||||
|
||||
{
|
||||
/* calculate approximate size of the output (overestimate) */
|
||||
size_t allocation_length = 0;
|
||||
size_t skipped_bytes = 0;
|
||||
while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
|
||||
{
|
||||
/* is escape sequence */
|
||||
@ -816,8 +816,16 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
|
||||
}
|
||||
}
|
||||
|
||||
/* If there are no escaped characters, we can use memcmp */
|
||||
if(skipped_bytes == 0)
|
||||
{
|
||||
memcpy(output, input_pointer, allocation_length);
|
||||
output[allocation_length-1] = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
/* else loop through the string literal */
|
||||
output_pointer = output;
|
||||
/* loop through the string literal */
|
||||
while (input_pointer < input_end)
|
||||
{
|
||||
if (*input_pointer != '\\')
|
||||
@ -872,9 +880,9 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
|
||||
input_pointer += sequence_length;
|
||||
}
|
||||
}
|
||||
|
||||
/* zero terminate the output */
|
||||
*output_pointer = '\0';
|
||||
}
|
||||
|
||||
item->type = cJSON_String;
|
||||
item->valuestring = (char*)output;
|
||||
|
@ -73,6 +73,7 @@ static void assert_not_parse_string(const char * const string)
|
||||
static void parse_string_should_parse_strings(void)
|
||||
{
|
||||
assert_parse_string("\"\"", "");
|
||||
assert_parse_string("\"Simple string\"", "Simple string");
|
||||
assert_parse_string(
|
||||
"\" !\\\"#$%&'()*+,-./\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~\"",
|
||||
" !\"#$%&'()*+,-.//0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~");
|
||||
|
Loading…
Reference in New Issue
Block a user