| 15 | } |
| 16 | |
| 17 | utf8_parse_result common_parse_utf8_codepoint(std::string_view input, size_t offset) { |
| 18 | if (offset >= input.size()) { |
| 19 | return utf8_parse_result(utf8_parse_result::INCOMPLETE); |
| 20 | } |
| 21 | |
| 22 | // ASCII fast path |
| 23 | if (!(input[offset] & 0x80)) { |
| 24 | return utf8_parse_result(utf8_parse_result::SUCCESS, input[offset], 1); |
| 25 | } |
| 26 | |
| 27 | // Invalid: continuation byte as first byte |
| 28 | if (!(input[offset] & 0x40)) { |
| 29 | return utf8_parse_result(utf8_parse_result::INVALID); |
| 30 | } |
| 31 | |
| 32 | // 2-byte sequence |
| 33 | if (!(input[offset] & 0x20)) { |
| 34 | if (offset + 1 >= input.size()) { |
| 35 | return utf8_parse_result(utf8_parse_result::INCOMPLETE); |
| 36 | } |
| 37 | if ((input[offset + 1] & 0xc0) != 0x80) { |
| 38 | return utf8_parse_result(utf8_parse_result::INVALID); |
| 39 | } |
| 40 | auto result = ((input[offset] & 0x1f) << 6) | (input[offset + 1] & 0x3f); |
| 41 | return utf8_parse_result(utf8_parse_result::SUCCESS, result, 2); |
| 42 | } |
| 43 | |
| 44 | // 3-byte sequence |
| 45 | if (!(input[offset] & 0x10)) { |
| 46 | if (offset + 2 >= input.size()) { |
| 47 | return utf8_parse_result(utf8_parse_result::INCOMPLETE); |
| 48 | } |
| 49 | if ((input[offset + 1] & 0xc0) != 0x80 || (input[offset + 2] & 0xc0) != 0x80) { |
| 50 | return utf8_parse_result(utf8_parse_result::INVALID); |
| 51 | } |
| 52 | auto result = ((input[offset] & 0x0f) << 12) | ((input[offset + 1] & 0x3f) << 6) | (input[offset + 2] & 0x3f); |
| 53 | return utf8_parse_result(utf8_parse_result::SUCCESS, result, 3); |
| 54 | } |
| 55 | |
| 56 | // 4-byte sequence |
| 57 | if (!(input[offset] & 0x08)) { |
| 58 | if (offset + 3 >= input.size()) { |
| 59 | return utf8_parse_result(utf8_parse_result::INCOMPLETE); |
| 60 | } |
| 61 | if ((input[offset + 1] & 0xc0) != 0x80 || (input[offset + 2] & 0xc0) != 0x80 || (input[offset + 3] & 0xc0) != 0x80) { |
| 62 | return utf8_parse_result(utf8_parse_result::INVALID); |
| 63 | } |
| 64 | auto result = ((input[offset] & 0x07) << 18) | ((input[offset + 1] & 0x3f) << 12) | ((input[offset + 2] & 0x3f) << 6) | (input[offset + 3] & 0x3f); |
| 65 | return utf8_parse_result(utf8_parse_result::SUCCESS, result, 4); |
| 66 | } |
| 67 | |
| 68 | // Invalid first byte |
| 69 | return utf8_parse_result(utf8_parse_result::INVALID); |
| 70 | } |
| 71 | |
| 72 | bool common_utf8_is_complete(const std::string & s) { |
| 73 | if (s.empty()) { |
no test coverage detected