From c9739c59fd900e388e305e0eb1113664e97cbb74 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 14 Mar 2017 11:19:27 +0100 Subject: [PATCH] Replace strchr with switch case This should improve performance and improve readability. --- cJSON.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cJSON.c b/cJSON.c index 3ccdac2..9acb77c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -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 */ for (input_pointer = input; *input_pointer; input_pointer++) { - if (strchr("\"\\\b\f\n\r\t", *input_pointer)) + switch (*input_pointer) { - /* one character escape sequence */ - escape_characters++; - } - else if (*input_pointer < 32) - { - /* UTF-16 escape sequence uXXXX */ - escape_characters += 5; + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + 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;