maartenbreddels commented on a change in pull request #7449: URL: https://github.com/apache/arrow/pull/7449#discussion_r447149548
########## File path: cpp/src/arrow/compute/kernels/scalar_string.cc ########## @@ -30,6 +31,124 @@ namespace internal { namespace { +// lookup tables +std::vector<uint32_t> lut_upper_codepoint; +std::vector<uint32_t> lut_lower_codepoint; +std::once_flag flag_case_luts; + +constexpr uint32_t REPLACEMENT_CHAR = + '?'; // the proper replacement char would be the 0xFFFD codepoint, but that can + // increase string length by a factor of 3 +constexpr int MAX_CODEPOINT_LUT = 0xffff; // up to this codepoint is in a lookup table + +static inline void utf8_encode(uint8_t*& str, uint32_t codepoint) { + if (codepoint < 0x80) { + *str++ = codepoint; + } else if (codepoint < 0x800) { + *str++ = 0xC0 + (codepoint >> 6); + *str++ = 0x80 + (codepoint & 0x3F); + } else if (codepoint < 0x10000) { + *str++ = 0xE0 + (codepoint >> 12); + *str++ = 0x80 + ((codepoint >> 6) & 0x3F); + *str++ = 0x80 + (codepoint & 0x3F); + } else if (codepoint < 0x200000) { + *str++ = 0xF0 + (codepoint >> 18); + *str++ = 0x80 + ((codepoint >> 12) & 0x3F); + *str++ = 0x80 + ((codepoint >> 6) & 0x3F); + *str++ = 0x80 + (codepoint & 0x3F); + } else { + *str++ = codepoint; + } +} + +static inline bool utf8_is_continuation(const uint8_t codeunit) { + return (codeunit & 0xC0) == 0x80; // upper two bits should be 10 +} + +static inline uint32_t utf8_decode(const uint8_t*& str, int64_t& length) { + if (*str < 0x80) { // + length -= 1; + return *str++; + } else if (*str < 0xC0) { // invalid non-ascii char + length -= 1; + str++; + return REPLACEMENT_CHAR; Review comment: there are different strategies that are 'common', replacement by a replacement character (similar to what I do here), or assuming Latin 1 encoding, and try to salvage it a bit. But I wonder what a common strategy should be, should we be forgiving on invalid data, should we bail out and have separate 'corrections' kernels? I'm not sure what the best place is for addressing this. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org