SG011 commented on code in PR #13497: URL: https://github.com/apache/arrow/pull/13497#discussion_r918627607
########## cpp/src/gandiva/gdv_string_function_stubs.cc: ########## @@ -264,6 +265,304 @@ const char* gdv_fn_lower_utf8(int64_t context, const char* data, int32_t data_le return out; } +// converts utf8 to utf16 encoding +GANDIVA_EXPORT +const char* gdv_fn_encode(int64_t context, const char* data, int32_t data_len, + const char* charset, int32_t charset_len, int32_t* out_len) { + if (data_len == 0) { + *out_len = 0; + return ""; + } + + char* out = + reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, 32 * data_len)); + if (out == nullptr) { + gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); + *out_len = 0; + return ""; + } + + int32_t char_len; + uint32_t char_codepoint; + int32_t y = 5; + int32_t z = 9; + + std::string charset_str(charset, charset_len); + + if (boost::iequals(charset_str, "UTF-16BE")) { + char* tmp = out; + + int ind_w = 0; + + for (int32_t i = 0; i < data_len; i += char_len) { + char_len = gdv_fn_utf8_char_length(data[i]); + + if (char_len == 1) { + int32_t input = static_cast<int32_t>(data[i]); + + auto num_bytes = snprintf(tmp + ind_w, y, "%.4X", input); + ind_w += num_bytes; + } else if (char_len == 2) { + const auto* in_char = (const uint8_t*)(data + i); + + // Decode the multibyte character + bool is_valid_utf8_char = + arrow::util::UTF8Decode((const uint8_t**)&in_char, &char_codepoint); + + // If it is an invalid utf8 character, UTF8Decode evaluates to false + if (!is_valid_utf8_char) { + gdv_fn_set_error_for_invalid_utf8(context, data[i]); + *out_len = 0; + return ""; + } + + auto num_bytes = snprintf(tmp + ind_w, y, "%.4X", char_codepoint); + ind_w += num_bytes; + } else if (char_len == 3) { + const auto* in_char = (const uint8_t*)(data + i); + + // Decode the multibyte character + bool is_valid_utf8_char = + arrow::util::UTF8Decode((const uint8_t**)&in_char, &char_codepoint); + + // If it is an invalid utf8 character, UTF8Decode evaluates to false + if (!is_valid_utf8_char) { + gdv_fn_set_error_for_invalid_utf8(context, data[i]); + *out_len = 0; + return ""; + } + + auto num_bytes = snprintf(tmp + ind_w, z, "%.8X", char_codepoint); + ind_w += num_bytes; + } else if (char_len == 4) { + const auto* in_char = (const uint8_t*)(data + i); + + // Decode the multibyte character + bool is_valid_utf8_char = + arrow::util::UTF8Decode((const uint8_t**)&in_char, &char_codepoint); + + // If it is an invalid utf8 character, UTF8Decode evaluates to false + if (!is_valid_utf8_char) { + gdv_fn_set_error_for_invalid_utf8(context, data[i]); + *out_len = 0; + return ""; + } + + auto num_bytes = snprintf(tmp + ind_w, z, "%.8X", char_codepoint); + ind_w += num_bytes; + } + } + *out_len = static_cast<int32_t>(ind_w); + return out; + } else if (boost::iequals(charset_str, "UTF-16LE")) { + char* tmp = out; + + for (int32_t i = 0; i < data_len; i += char_len) { + char_len = gdv_fn_utf8_char_length(data[i]); + + if (char_len == 1) { + int32_t input = static_cast<int32_t>(data[i]); + + char hex_string[20]; + + snprintf(hex_string, y, "%.4X", input); + + for (int32_t xx = 0; xx < 4; xx += 4) { Review Comment: For what? -- 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-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org