diff --git a/include/ada/unicode.h b/include/ada/unicode.h index 97c8fbb67..81a79517d 100644 --- a/include/ada/unicode.h +++ b/include/ada/unicode.h @@ -154,9 +154,10 @@ ada_really_inline constexpr bool is_single_dot_path_segment( ada_really_inline constexpr bool is_lowercase_hex(const char c) noexcept; /** - * @details Convert hex to binary. + * @details Convert hex to binary. Caller is responsible to ensure that + * the parameter is an hexadecimal digit (0-9, A-F, a-f). */ -unsigned constexpr convert_hex_to_binary(char c) noexcept; +ada_really_inline unsigned constexpr convert_hex_to_binary(char c) noexcept; /** * first_percent should be = input.find('%') diff --git a/src/unicode.cpp b/src/unicode.cpp index c6100dd0f..035d4df5f 100644 --- a/src/unicode.cpp +++ b/src/unicode.cpp @@ -368,13 +368,12 @@ ada_really_inline constexpr bool is_lowercase_hex(const char c) noexcept { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); } +constexpr static char hex_to_binary_table[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, + 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15}; unsigned constexpr convert_hex_to_binary(const char c) noexcept { - // this code can be optimized. - if (c <= '9') { - return c - '0'; - } - char del = c >= 'a' ? 'a' : 'A'; - return 10 + (c - del); + return hex_to_binary_table[c - '0']; } std::string percent_decode(const std::string_view input, size_t first_percent) {