Skip to content

Commit b0d2c47

Browse files
lemireDaniel Lemire
and
Daniel Lemire
authored
using a table-based convert_hex_to_binary function for better speed (#478)
* using a table-based convert_hex_to_binary function for better speed * Using table throughout. * Improving is_ascii_hex_digit * Revert "Using table throughout." This reverts commit 5050bdc. --------- Co-authored-by: Daniel Lemire <[email protected]>
1 parent 2c17312 commit b0d2c47

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

include/ada/unicode.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ ada_really_inline constexpr bool is_single_dot_path_segment(
154154
ada_really_inline constexpr bool is_lowercase_hex(const char c) noexcept;
155155

156156
/**
157-
* @details Convert hex to binary.
157+
* @details Convert hex to binary. Caller is responsible to ensure that
158+
* the parameter is an hexadecimal digit (0-9, A-F, a-f).
158159
*/
159-
unsigned constexpr convert_hex_to_binary(char c) noexcept;
160+
ada_really_inline unsigned constexpr convert_hex_to_binary(char c) noexcept;
160161

161162
/**
162163
* first_percent should be = input.find('%')

src/unicode.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,12 @@ ada_really_inline constexpr bool is_lowercase_hex(const char c) noexcept {
368368
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
369369
}
370370

371+
constexpr static char hex_to_binary_table[] = {
372+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11,
373+
12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
374+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};
371375
unsigned constexpr convert_hex_to_binary(const char c) noexcept {
372-
// this code can be optimized.
373-
if (c <= '9') {
374-
return c - '0';
375-
}
376-
char del = c >= 'a' ? 'a' : 'A';
377-
return 10 + (c - del);
376+
return hex_to_binary_table[c - '0'];
378377
}
379378

380379
std::string percent_decode(const std::string_view input, size_t first_percent) {

0 commit comments

Comments
 (0)