MCPcopy Create free account
hub / github.com/apache/arrow / left_utf8_int32

Function left_utf8_int32

cpp/src/gandiva/precompiled/string_ops.cc:2333–2377  ·  view source on GitHub ↗

Returns the x leftmost characters of a given string. Cases: LEFT("TestString", 10) => "TestString" LEFT("TestString", 3) => "Tes" LEFT("TestString", -3) => "TestStr"

Source from the content-addressed store, hash-verified

2331// LEFT("TestString", 3) => "Tes"
2332// LEFT("TestString", -3) => "TestStr"
2333FORCE_INLINE
2334const char* left_utf8_int32(gdv_int64 context, const char* text, gdv_int32 text_len,
2335 gdv_int32 number, gdv_int32* out_len) {
2336 // returns the 'number' left most characters of a given text
2337 if (text_len == 0 || number == 0) {
2338 *out_len = 0;
2339 return "";
2340 }
2341
2342 int32_t char_count = utf8_length(context, text, text_len);
2343
2344 // char_count is zero if input has invalid utf8 char
2345 if (char_count == 0) {
2346 *out_len = 0;
2347 return "";
2348 }
2349
2350 // case where left('abcdef', -6) -> "" and left('abcdef', -7) -> ""
2351 if (number < 0 && -(number) >= char_count) {
2352 *out_len = 0;
2353 return "";
2354 }
2355
2356 // iterate over the utf8 string validating each character
2357 int char_len;
2358 int current_char_count = 0;
2359 int byte_index = 0;
2360 for (int i = 0; i < text_len; i += char_len) {
2361 char_len = utf8_char_length(text[i]);
2362 byte_index += char_len;
2363 ++current_char_count;
2364 // Define the rules to stop the iteration over the string
2365 // case where left('abc', 5) -> 'abc'
2366 if (number > 0 && current_char_count == number) {
2367 break;
2368 }
2369 // case where left('abc', -5) ==> ''
2370 if (number < 0 && current_char_count == number + char_count) {
2371 break;
2372 }
2373 }
2374
2375 *out_len = byte_index;
2376 return text;
2377}
2378
2379// Returns the x rightmost characters of a given string. Cases:
2380// RIGHT("TestString", 10) => "TestString"

Callers 1

TESTFunction · 0.85

Calls 1

utf8_lengthFunction · 0.85

Tested by 1

TESTFunction · 0.68