| 343 | } |
| 344 | |
| 345 | static inline bool UTF8Decode(const uint8_t** data, uint32_t* codepoint) { |
| 346 | const uint8_t* str = *data; |
| 347 | if (*str < 0x80) { // ascii |
| 348 | *codepoint = *str++; |
| 349 | } else if (ARROW_PREDICT_FALSE(*str < 0xC0)) { // invalid non-ascii char |
| 350 | return false; |
| 351 | } else if (*str < 0xE0) { |
| 352 | uint8_t code_unit_1 = (*str++) & 0x1F; // take last 5 bits |
| 353 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 354 | return false; |
| 355 | } |
| 356 | uint8_t code_unit_2 = (*str++) & 0x3F; // take last 6 bits |
| 357 | *codepoint = (code_unit_1 << 6) + code_unit_2; |
| 358 | } else if (*str < 0xF0) { |
| 359 | uint8_t code_unit_1 = (*str++) & 0x0F; // take last 4 bits |
| 360 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 361 | return false; |
| 362 | } |
| 363 | uint8_t code_unit_2 = (*str++) & 0x3F; // take last 6 bits |
| 364 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 365 | return false; |
| 366 | } |
| 367 | uint8_t code_unit_3 = (*str++) & 0x3F; // take last 6 bits |
| 368 | *codepoint = (code_unit_1 << 12) + (code_unit_2 << 6) + code_unit_3; |
| 369 | } else if (*str < 0xF8) { |
| 370 | uint8_t code_unit_1 = (*str++) & 0x07; // take last 3 bits |
| 371 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 372 | return false; |
| 373 | } |
| 374 | uint8_t code_unit_2 = (*str++) & 0x3F; // take last 6 bits |
| 375 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 376 | return false; |
| 377 | } |
| 378 | uint8_t code_unit_3 = (*str++) & 0x3F; // take last 6 bits |
| 379 | if (ARROW_PREDICT_FALSE(!Utf8IsContinuation(*str))) { |
| 380 | return false; |
| 381 | } |
| 382 | uint8_t code_unit_4 = (*str++) & 0x3F; // take last 6 bits |
| 383 | *codepoint = |
| 384 | (code_unit_1 << 18) + (code_unit_2 << 12) + (code_unit_3 << 6) + code_unit_4; |
| 385 | } else { // invalid non-ascii char |
| 386 | return false; |
| 387 | } |
| 388 | *data = str; |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | static inline bool UTF8DecodeReverse(const uint8_t** data, uint32_t* codepoint) { |
| 393 | const uint8_t* str = *data; |
no test coverage detected