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

Function btrim_utf8_utf8

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

Trims characters present in the trim text from both ends of the base text

Source from the content-addressed store, hash-verified

463
464// Trims characters present in the trim text from both ends of the base text
465FORCE_INLINE
466const char* btrim_utf8_utf8(gdv_int64 context, const char* basetext,
467 gdv_int32 basetext_len, const char* trimtext,
468 gdv_int32 trimtext_len, int32_t* out_len) {
469 if (basetext_len == 0) {
470 *out_len = 0;
471 return "";
472 } else if (trimtext_len == 0) {
473 *out_len = basetext_len;
474 return basetext;
475 }
476
477 gdv_int32 start_ptr, end_ptr, char_len, byte_cnt = 1;
478 // scan the base text from left to right and increment the start and decrement the
479 // end pointers till there are characters which are not present in the trim text
480 for (start_ptr = 0; start_ptr < basetext_len; start_ptr += char_len) {
481 char_len = utf8_char_length(basetext[start_ptr]);
482 if (char_len == 0 || start_ptr + char_len > basetext_len) {
483 // invalid byte or incomplete glyph
484 set_error_for_invalid_utf(context, basetext[start_ptr]);
485 *out_len = 0;
486 return "";
487 }
488 if (!is_substr_utf8_utf8(trimtext, trimtext_len, basetext + start_ptr, char_len)) {
489 break;
490 }
491 }
492 for (end_ptr = basetext_len - 1; end_ptr >= start_ptr; --end_ptr) {
493 char_len = utf8_char_length(basetext[end_ptr]);
494 if (char_len == 0) { // trailing byte in multibyte character
495 ++byte_cnt;
496 continue;
497 }
498 // this is the first byte of a character, hence check if char_len = char_cnt
499 if (byte_cnt != char_len) { // invalid byte or incomplete glyph
500 set_error_for_invalid_utf(context, basetext[end_ptr]);
501 *out_len = 0;
502 return "";
503 }
504 byte_cnt = 1; // reset the counter*/
505 if (!is_substr_utf8_utf8(trimtext, trimtext_len, basetext + end_ptr, char_len)) {
506 break;
507 }
508 }
509
510 // when all characters are trimmed, start_ptr has been incremented to basetext_len and
511 // end_ptr still points to basetext_len - 1, hence we need to handle this case
512 if (start_ptr > end_ptr) {
513 *out_len = 0;
514 return "";
515 }
516
517 end_ptr += utf8_char_length(basetext[end_ptr]); // point to the next character
518 *out_len = end_ptr - start_ptr;
519 return basetext + start_ptr;
520}
521
522FORCE_INLINE

Callers 1

TESTFunction · 0.85

Calls

no outgoing calls

Tested by 1

TESTFunction · 0.68