Returns the x rightmost characters of a given string. Cases: RIGHT("TestString", 10) => "TestString" RIGHT("TestString", 3) => "ing" RIGHT("TestString", -3) => "tString"
| 2381 | // RIGHT("TestString", 3) => "ing" |
| 2382 | // RIGHT("TestString", -3) => "tString" |
| 2383 | FORCE_INLINE |
| 2384 | const char* right_utf8_int32(gdv_int64 context, const char* text, gdv_int32 text_len, |
| 2385 | gdv_int32 number, gdv_int32* out_len) { |
| 2386 | // returns the 'number' left most characters of a given text |
| 2387 | if (text_len == 0 || number == 0) { |
| 2388 | *out_len = 0; |
| 2389 | return ""; |
| 2390 | } |
| 2391 | |
| 2392 | // initially counts the number of utf8 characters in the defined text |
| 2393 | int32_t char_count = utf8_length(context, text, text_len); |
| 2394 | |
| 2395 | // char_count is zero if input has invalid utf8 char |
| 2396 | if (char_count == 0) { |
| 2397 | *out_len = 0; |
| 2398 | return ""; |
| 2399 | } |
| 2400 | |
| 2401 | // case where right('abcdef', -6) -> "" and right('abcdef', -7) -> "" |
| 2402 | if (number < 0 && -(number) >= char_count) { |
| 2403 | *out_len = 0; |
| 2404 | return ""; |
| 2405 | } |
| 2406 | |
| 2407 | int32_t start_char_pos; // the char result start position (inclusive) |
| 2408 | |
| 2409 | if (number > 0) { |
| 2410 | // case where right('abc', 5) ==> 'abc' start_char_pos=1. |
| 2411 | start_char_pos = (char_count > number) ? char_count - number : 0; |
| 2412 | } else { |
| 2413 | start_char_pos = number * -1; |
| 2414 | } |
| 2415 | |
| 2416 | // calculate the start byte position |
| 2417 | int32_t start_byte_pos = utf8_byte_pos(context, text, text_len, start_char_pos); |
| 2418 | |
| 2419 | // calculate output length |
| 2420 | *out_len = (text_len - start_byte_pos); |
| 2421 | return text + start_byte_pos; |
| 2422 | } |
| 2423 | |
| 2424 | FORCE_INLINE |
| 2425 | const char* binary_string(gdv_int64 context, const char* text, gdv_int32 text_len, |