}
output.resize(input.size() / 2);
int j = 0;
- auto unhex = [](char c) -> int {
- return c >= '0' && c <= '9' ? c - '0' :
- c >= 'A' && c <= 'F' ? c - 'A' + 10 :
- c >= 'a' && c <= 'f' ? c - 'a' + 10 :
- -1;
- };
for (size_t i = 0; i < input.size(); i += 2) {
- int highBits = unhex(input[i]);
- int lowBits = unhex(input[i + 1]);
- if (highBits < 0 || lowBits < 0) {
+ int highBits = detail::hexTable[static_cast<uint8_t>(input[i])];
+ int lowBits = detail::hexTable[static_cast<uint8_t>(input[i + 1])];
+ if ((highBits | lowBits) & 0x10) {
+ // One of the characters wasn't a hex digit
return false;
}
output[j++] = (highBits << 4) + lowBits;
#include <folly/String.h>
#include <boost/algorithm/string.hpp>
-#include <cstdarg>
#include <folly/Benchmark.h>
+#include <folly/Random.h>
+#include <cstdarg>
#include <random>
using namespace folly;
const size_t kURIBmStringLength = 256;
const uint32_t kURIPassThroughPercentage = 50;
+fbstring hexlifyInput;
+fbstring hexlifyOutput;
+const size_t kHexlifyLength = 1024;
+
void initBenchmark() {
std::mt19937 rnd;
}
uribmEscapedString = uriEscape<fbstring>(uribmString);
+
+ // hexlify
+ hexlifyInput.resize(kHexlifyLength);
+ Random::secureRandom(&hexlifyInput[0], kHexlifyLength);
+ folly::hexlify(hexlifyInput, hexlifyOutput);
}
BENCHMARK(BM_cEscape, iters) {
}
}
+BENCHMARK(BM_unhexlify, iters) {
+ // iters/sec = bytes output per sec
+ std::string unhexed;
+ folly::StringPiece hex = hexlifyOutput;
+ for (; iters >= hex.size(); iters -= hex.size()) {
+ folly::unhexlify(hex, unhexed);
+ }
+ iters -= iters % 2; // round down to an even number of chars
+ hex = hex.subpiece(0, iters);
+ folly::unhexlify(hex, unhexed);
+}
+
} // namespace
//////////////////////////////////////////////////////////////////////