returns a string containing the specified number of rightmost characters of the original string.
| 70 | |
| 71 | // returns a string containing the specified number of rightmost characters of the original string. |
| 72 | SIValue AR_RIGHT(SIValue *argv, int argc, void *private_data) { |
| 73 | if(SIValue_IsNull(argv[0])) return SI_NullVal(); |
| 74 | |
| 75 | int64_t newlen = -1; |
| 76 | if(SI_TYPE(argv[1]) == T_INT64) { |
| 77 | newlen = argv[1].longval; |
| 78 | } |
| 79 | if(newlen < 0) { |
| 80 | ErrorCtx_SetError("length must be a non-negative integer"); |
| 81 | return SI_NullVal(); |
| 82 | } |
| 83 | |
| 84 | const char *str = argv[0].stringval; |
| 85 | int64_t start = str_length(str) - newlen; |
| 86 | |
| 87 | if(start <= 0) { |
| 88 | // No need to truncate this string based on the requested length |
| 89 | return SI_DuplicateStringVal(str); |
| 90 | } |
| 91 | |
| 92 | utf8proc_int32_t c; |
| 93 | int64_t start_bytes = 0; |
| 94 | for (int i = 0; i < start; i++) { |
| 95 | start_bytes += utf8proc_iterate((const utf8proc_uint8_t *)(str+start_bytes), -1, &c); |
| 96 | } |
| 97 | |
| 98 | return SI_DuplicateStringVal(str + start_bytes); |
| 99 | } |
| 100 | |
| 101 | // returns the original string with trailing whitespace removed. |
| 102 | SIValue AR_RTRIM(SIValue *argv, int argc, void *private_data) { |
nothing calls this directly
no test coverage detected