bkietz commented on code in PR #37792:
URL: https://github.com/apache/arrow/pull/37792#discussion_r1352783793


##########
cpp/src/arrow/util/string.cc:
##########
@@ -73,20 +71,34 @@ std::string HexEncode(std::string_view str) { return 
HexEncode(str.data(), str.s
 
 std::string Escape(std::string_view str) { return Escape(str.data(), 
str.size()); }
 
-Status ParseHexValue(const char* data, uint8_t* out) {
-  char c1 = data[0];
-  char c2 = data[1];
+constexpr uint8_t kInvalidHexDigit = -1;
 
-  const char* kAsciiTableEnd = kAsciiTable + 16;
-  const char* pos1 = std::lower_bound(kAsciiTable, kAsciiTableEnd, c1);
-  const char* pos2 = std::lower_bound(kAsciiTable, kAsciiTableEnd, c2);
+constexpr uint8_t ParseHexDigit(char c) {
+  if (c >= '0' && c <= '9') return c - '0';
+  if (c >= 'A' && c <= 'F') return c - 'A' + 10;
+  return kInvalidHexDigit;
+}
+
+Status ParseHexValue(const char* data, uint8_t* out) {
+  uint8_t high = ParseHexDigit(data[0]);
+  uint8_t low = ParseHexDigit(data[1]);
 
   // Error checking
-  if (pos1 == kAsciiTableEnd || pos2 == kAsciiTableEnd || *pos1 != c1 || *pos2 
!= c2) {
+  if (high == kInvalidHexDigit || low == kInvalidHexDigit) {
     return Status::Invalid("Encountered non-hex digit");
   }
 
-  *out = static_cast<uint8_t>((pos1 - kAsciiTable) << 4 | (pos2 - 
kAsciiTable));
+  *out = static_cast<uint8_t>(high << 4 | low);
+  return Status::OK();
+}
+
+Status ParseHexValues(std::string_view hex_string, uint8_t* out) {

Review Comment:
   
https://github.com/bkietz/arrow/blob/741674989a0b7d3b5b165e3e61074143f12ac1f5/cpp/src/arrow/util/string_test.cc#L60



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to