| 285 | // Reverse a utf8 sequence |
| 286 | FORCE_INLINE |
| 287 | const char* reverse_utf8(gdv_int64 context, const char* data, gdv_int32 data_len, |
| 288 | int32_t* out_len) { |
| 289 | if (data_len == 0) { |
| 290 | *out_len = 0; |
| 291 | return ""; |
| 292 | } |
| 293 | |
| 294 | char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, data_len)); |
| 295 | if (ret == nullptr) { |
| 296 | gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); |
| 297 | *out_len = 0; |
| 298 | return ""; |
| 299 | } |
| 300 | |
| 301 | gdv_int32 char_len; |
| 302 | for (gdv_int32 i = 0; i < data_len; i += char_len) { |
| 303 | char_len = utf8_char_length(data[i]); |
| 304 | |
| 305 | if (char_len == 0 || i + char_len > data_len) { // invalid byte or incomplete glyph |
| 306 | set_error_for_invalid_utf(context, data[i]); |
| 307 | *out_len = 0; |
| 308 | return ""; |
| 309 | } |
| 310 | |
| 311 | for (gdv_int32 j = 0; j < char_len; ++j) { |
| 312 | if (j > 0 && (data[i + j] & 0xC0) != 0x80) { // bytes following head-byte of glyph |
| 313 | set_error_for_invalid_utf(context, data[i + j]); |
| 314 | *out_len = 0; |
| 315 | return ""; |
| 316 | } |
| 317 | ret[data_len - i - char_len + j] = data[i + j]; |
| 318 | } |
| 319 | } |
| 320 | *out_len = data_len; |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | // Trims whitespaces from the left end of the input utf8 sequence |
| 325 | FORCE_INLINE |