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

16
cJSON.c
View File

@ -818,16 +818,26 @@ 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)
{ {
case '\"':
case '\\':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
/* one character escape sequence */ /* one character escape sequence */
escape_characters++; escape_characters++;
} break;
else if (*input_pointer < 32) default:
if (*input_pointer < 32)
{ {
/* UTF-16 escape sequence uXXXX */ /* UTF-16 escape sequence uXXXX */
escape_characters += 5; escape_characters += 5;
} }
break;
}
} }
output_length = (size_t)(input_pointer - input) + escape_characters; output_length = (size_t)(input_pointer - input) + escape_characters;