Trims characters present in the trim text from the right end of the base text
| 418 | |
| 419 | // Trims characters present in the trim text from the right end of the base text |
| 420 | FORCE_INLINE |
| 421 | const char* rtrim_utf8_utf8(gdv_int64 context, const char* basetext, |
| 422 | gdv_int32 basetext_len, const char* trimtext, |
| 423 | gdv_int32 trimtext_len, int32_t* out_len) { |
| 424 | if (basetext_len == 0) { |
| 425 | *out_len = 0; |
| 426 | return ""; |
| 427 | } else if (trimtext_len == 0) { |
| 428 | *out_len = basetext_len; |
| 429 | return basetext; |
| 430 | } |
| 431 | |
| 432 | gdv_int32 char_len, end_ptr, byte_cnt = 1; |
| 433 | // scan the base text from right to left and decrement the end pointer till |
| 434 | // there is a character which is not present in the trim text |
| 435 | for (end_ptr = basetext_len - 1; end_ptr >= 0; --end_ptr) { |
| 436 | char_len = utf8_char_length(basetext[end_ptr]); |
| 437 | if (char_len == 0) { // trailing bytes of multibyte character |
| 438 | ++byte_cnt; |
| 439 | continue; |
| 440 | } |
| 441 | // this is the first byte of a character, hence check if char_len = char_cnt |
| 442 | if (byte_cnt != char_len) { // invalid byte or incomplete glyph |
| 443 | set_error_for_invalid_utf(context, basetext[end_ptr]); |
| 444 | *out_len = 0; |
| 445 | return ""; |
| 446 | } |
| 447 | byte_cnt = 1; // reset the counter*/ |
| 448 | if (!is_substr_utf8_utf8(trimtext, trimtext_len, basetext + end_ptr, char_len)) { |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // when all characters in the basetext are part of the trimtext |
| 454 | if (end_ptr == -1) { |
| 455 | *out_len = 0; |
| 456 | return ""; |
| 457 | } |
| 458 | |
| 459 | end_ptr += utf8_char_length(basetext[end_ptr]); // point to the next character |
| 460 | *out_len = end_ptr; |
| 461 | return basetext; |
| 462 | } |
| 463 | |
| 464 | // Trims characters present in the trim text from both ends of the base text |
| 465 | FORCE_INLINE |