From: Yedidya Feldblum Date: Sun, 30 Jul 2017 21:28:44 +0000 (-0700) Subject: Let MacAddress use a helper hex-table X-Git-Tag: v2017.07.31.00~4 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a961f2cd4c5f392e236efed49cc5a98a55d8334b;p=folly.git Let MacAddress use a helper hex-table Summary: [Folly] Let `MacAddress` use a helper hex-table. The table is in the `folly::detail` namespace so it is non-public to users of folly, but the rules may not be quite as strict in at least some cases within folly. Also the helper `unhex` function that this replaces is not `clang-format`-clean, so this lets us sidestep that. Reviewed By: Orvid Differential Revision: D5524229 fbshipit-source-id: 21d6938e20cd675e65499a5b64d2af934f980222 --- diff --git a/folly/MacAddress.cpp b/folly/MacAddress.cpp index c182999c..45892826 100644 --- a/folly/MacAddress.cpp +++ b/folly/MacAddress.cpp @@ -20,6 +20,7 @@ #include #include +#include using std::invalid_argument; using std::string; @@ -71,12 +72,6 @@ string MacAddress::toString() const { void MacAddress::parse(StringPiece str) { // Helper function to convert a single hex char into an integer - 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; - }; auto isSeparatorChar = [](char c) { return c == ':' || c == '-'; }; @@ -99,21 +94,21 @@ void MacAddress::parse(StringPiece str) { } // Parse the upper nibble - int upper = unhex(*p); - if (upper < 0) { + uint8_t upper = detail::hexTable[static_cast(*p)]; + if (upper & 0x10) { throw invalid_argument(to("invalid MAC address \"", str, "\": contains non-hex digit")); } ++p; // Parse the lower nibble - int lower; + uint8_t lower; if (p == str.end()) { lower = upper; upper = 0; } else { - lower = unhex(*p); - if (lower < 0) { + lower = detail::hexTable[static_cast(*p)]; + if (lower & 0x10) { // Also accept ':', '-', or '\0', to handle the case where one // of the bytes was represented by just a single digit. if (isSeparatorChar(*p)) { @@ -128,7 +123,7 @@ void MacAddress::parse(StringPiece str) { } // Update parsed with the newly parsed byte - parsed[byteIndex] = uint8_t((upper << 4) | lower); + parsed[byteIndex] = (upper << 4) | lower; } if (p != str.end()) {