You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/07/06 14:57:35 UTC

[GitHub] [arrow] projjal commented on a change in pull request #10195: ARROW-12595: [C++][Gandiva] Implement TO_HEX([binary] field), HEX, UNHEX and FROM_HEX([string]field] functions

projjal commented on a change in pull request #10195:
URL: https://github.com/apache/arrow/pull/10195#discussion_r664633954



##########
File path: cpp/src/gandiva/precompiled/string_ops.cc
##########
@@ -2072,4 +2074,102 @@ const char* byte_substr_binary_int32_int32(gdv_int64 context, const char* text,
   memcpy(ret, text + startPos, *out_len);
   return ret;
 }
+
+// Gets a binary object and returns its hexadecimal representation. That representation
+// maps each byte in the input to a 2-length string containing a hexadecimal number.
+// - Examples:
+//     - foo -> 666F6F = 66[f] 6F[o] 6F[o]
+//     - bar -> 626172 = 62[b] 61[a] 72[r]
+FORCE_INLINE
+const char* to_hex_binary(int64_t context, const char* text, int32_t text_len,
+                          int32_t* out_len) {
+  if (text_len == 0) {
+    *out_len = 0;
+    return "";
+  }
+
+  auto ret =
+      reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, text_len * 2 + 1));
+
+  if (ret == nullptr) {
+    gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string");
+    *out_len = 0;
+    return "";
+  }
+
+  uint32_t ret_index = 0;
+  uint32_t max_len = static_cast<uint32_t>(text_len) * 2;
+  uint32_t max_char_to_write = 4;
+
+  for (gdv_int32 i = 0; i < text_len; i++) {
+    DCHECK(ret_index >= 0 && ret_index < max_len);
+
+    int32_t ch = static_cast<int32_t>(text[i]) & 0xFF;
+
+    ret_index += snprintf(ret + ret_index, max_char_to_write, "%02X", ch);
+  }
+
+  *out_len = static_cast<int32_t>(ret_index);
+  return ret;
+}
+
+FORCE_INLINE
+const char* to_hex_big_int(int64_t context, int64_t data, int32_t* out_len) {
+  const int64_t hex_long_max_size = 2 * sizeof(int64_t);
+  auto ret =
+      reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, hex_long_max_size));
+
+  if (ret == nullptr) {
+    gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string");
+    *out_len = 0;
+    return "";
+  }
+  snprintf(ret, hex_long_max_size + 1, "%0" PRIX64, data);

Review comment:
       shouldn't it be "0x%" PRIX64?
   This is also probably why build is failing in some env




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org