Replace strchr with switch case

This should improve performance and improve readability.
This commit is contained in:
Max Bruckner 2017-03-14 11:19:27 +01:00
parent 778a0c146f
commit c9739c59fd

26
cJSON.c
View File

@ -818,15 +818,25 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe
/* set "flag" to 1 if something needs to be escaped */ /* set "flag" to 1 if something needs to be escaped */
for (input_pointer = input; *input_pointer; input_pointer++) for (input_pointer = input; *input_pointer; input_pointer++)
{ {
if (strchr("\"\\\b\f\n\r\t", *input_pointer)) switch (*input_pointer)
{ {
/* one character escape sequence */ case '\"':
escape_characters++; case '\\':
} case '\b':
else if (*input_pointer < 32) case '\f':
{ case '\n':
/* UTF-16 escape sequence uXXXX */ case '\r':
escape_characters += 5; case '\t':
/* one character escape sequence */
escape_characters++;
break;
default:
if (*input_pointer < 32)
{
/* UTF-16 escape sequence uXXXX */
escape_characters += 5;
}
break;
} }
} }
output_length = (size_t)(input_pointer - input) + escape_characters; output_length = (size_t)(input_pointer - input) + escape_characters;