Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Jeffrey <chjjeffrey@gmail.com>2016-07-07 07:57:39 +0300
committerAnna Henningsen <anna@addaleax.net>2016-07-17 19:21:33 +0300
commit151d316b99d56a4614c95cc59ea7a0c9c0f1928b (patch)
tree43521487851641f945a3f82d4d7b9bf8dc591a0c /src/string_bytes.cc
parent35e8c9481fa515bc6c122a63492def5e598537b4 (diff)
buffer: optimize hex_decode
PR-URL: https://github.com/nodejs/node/pull/7602 Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/string_bytes.cc')
-rw-r--r--src/string_bytes.cc37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/string_bytes.cc b/src/string_bytes.cc
index 6f15ad48848..668a3b1efe2 100644
--- a/src/string_bytes.cc
+++ b/src/string_bytes.cc
@@ -143,16 +143,27 @@ const int8_t unbase64_table[256] =
};
-template <typename TypeName>
-unsigned hex2bin(TypeName c) {
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'A' && c <= 'F')
- return 10 + (c - 'A');
- if (c >= 'a' && c <= 'f')
- return 10 + (c - 'a');
- return static_cast<unsigned>(-1);
-}
+static const int8_t unhex_table[256] =
+ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+ };
+
+#define unhex(x) \
+ static_cast<unsigned>(unhex_table[static_cast<uint8_t>(x)])
template <typename TypeName>
@@ -162,11 +173,11 @@ size_t hex_decode(char* buf,
const size_t srcLen) {
size_t i;
for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
- unsigned a = hex2bin(src[i * 2 + 0]);
- unsigned b = hex2bin(src[i * 2 + 1]);
+ unsigned a = unhex(src[i * 2 + 0]);
+ unsigned b = unhex(src[i * 2 + 1]);
if (!~a || !~b)
return i;
- buf[i] = a * 16 + b;
+ buf[i] = (a << 4) | b;
}
return i;