Counting code points in the UTF-8 encoded string using the same method, 'to_unicode', as MaskSubStrUtf8 uses. So we can have a consistent behavior. Returns -1 if the string contains malformed(illegal/incomplete) code points.
| 193 | /// as MaskSubStrUtf8 uses. So we can have a consistent behavior. |
| 194 | /// Returns -1 if the string contains malformed(illegal/incomplete) code points. |
| 195 | static int GetUtf8CodePointCount(FunctionContext* ctx, const StringVal& val) { |
| 196 | utf8_codecvt<char>::state_type cvt_state; |
| 197 | const char* p = reinterpret_cast<char*>(val.ptr); |
| 198 | const char* p_end = p + val.len; |
| 199 | int char_cnt = 0; |
| 200 | while (p != p_end) { |
| 201 | uint32_t c = utf8_codecvt<char>::to_unicode(cvt_state, p, p_end); |
| 202 | if (c == utf::illegal || c == utf::incomplete) { |
| 203 | ctx->SetError(Substitute("The $0-th code point $1 is $2", |
| 204 | char_cnt, AnyValUtil::ToString(val), |
| 205 | c == utf::illegal ? "illegal" : "incomplete").c_str()); |
| 206 | return -1; |
| 207 | } |
| 208 | ++char_cnt; |
| 209 | } |
| 210 | return char_cnt; |
| 211 | } |
| 212 | |
| 213 | /// Mask the given string except the first 'un_mask_char_count' chars. Ported from |
| 214 | /// org.apache.hadoop.hive.ql.udf.generic.GenericUDFMaskShowFirstN. |
no test coverage detected