From: Ramiro Polla Date: Sat, 8 Dec 2018 18:13:25 +0000 (+0100) Subject: json_tokener: optimize check for hex characters X-Git-Tag: json-c-0.16-20220414~40^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=45c601bfa488c35cdd953c6fbdb3ffa1a7c90abf;p=json-c json_tokener: optimize check for hex characters speedup for 32-bit: ~1% speedup for 64-bit: ~1% --- diff --git a/json_tokener.c b/json_tokener.c index d168e3f..b3b28b6 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -66,6 +66,13 @@ static int is_ws_char(char c) || c == '\r'; } +static int is_hex_char(char c) +{ + return (c >= '0' && c <= '9') + || (c >= 'A' && c <= 'F') + || (c >= 'a' && c <= 'f'); +} + /* Use C99 NAN by default; if not available, nan("") should work too. */ #ifndef NAN #define NAN nan("") @@ -609,7 +616,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok, /* Handle a 4-byte sequence, or two sequences if a surrogate pair */ while(1) { - if (c && strchr(json_hex_chars, c)) { + if (c && is_hex_char(c)) { tok->ucs_char += ((unsigned int)jt_hexdigit(c) << ((3-tok->st_pos++)*4)); if(tok->st_pos == 4) { unsigned char unescaped_utf[4]; @@ -641,7 +648,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok, got_hi_surrogate = tok->ucs_char; /* Not at end, and the next two chars should be "\u" */ if ((len == -1 || len > (tok->char_offset + 2)) && - // str[0] != '0' && // implied by json_hex_chars, above. + // str[0] != '0' && // implied by is_hex_char, above. (str[1] == '\\') && (str[2] == 'u')) {