returns a string containing the specified number of leftmost characters of the original string.
| 24 | |
| 25 | // returns a string containing the specified number of leftmost characters of the original string. |
| 26 | SIValue AR_LEFT(SIValue *argv, int argc, void *private_data) { |
| 27 | if(SIValue_IsNull(argv[0])) return SI_NullVal(); |
| 28 | |
| 29 | int64_t newlen = -1; |
| 30 | if(SI_TYPE(argv[1]) == T_INT64) { |
| 31 | newlen = argv[1].longval; |
| 32 | } |
| 33 | if(newlen < 0) { |
| 34 | ErrorCtx_SetError("length must be a non-negative integer"); |
| 35 | return SI_NullVal(); |
| 36 | } |
| 37 | |
| 38 | if(strlen(argv[0].stringval) <= newlen) { |
| 39 | // No need to truncate this string based on the requested length |
| 40 | return SI_DuplicateStringVal(argv[0].stringval); |
| 41 | } |
| 42 | |
| 43 | // determine new string byte size |
| 44 | utf8proc_int32_t c; |
| 45 | int64_t newlen_bytes = 0; |
| 46 | const char *str = argv[0].stringval; |
| 47 | for (int i = 0; i < newlen; i++) { |
| 48 | newlen_bytes += utf8proc_iterate((const utf8proc_uint8_t *)(str+newlen_bytes), -1, &c); |
| 49 | } |
| 50 | |
| 51 | char *left_str = rm_malloc((newlen_bytes + 1) * sizeof(char)); |
| 52 | strncpy(left_str, str, newlen_bytes * sizeof(char)); |
| 53 | left_str[newlen_bytes] = '\0'; |
| 54 | |
| 55 | return SI_TransferStringVal(left_str); |
| 56 | } |
| 57 | |
| 58 | // returns the original string with leading whitespace removed. |
| 59 | SIValue AR_LTRIM(SIValue *argv, int argc, void *private_data) { |
nothing calls this directly
no test coverage detected