| 14 | namespace engine::models::qwen3_forced_aligner { |
| 15 | namespace { |
| 16 | |
| 17 | struct Utf8Char { |
| 18 | uint32_t codepoint = 0; |
| 19 | std::string text; |
| 20 | }; |
| 21 | |
| 22 | std::vector<Utf8Char> utf8_chars(const std::string & text) { |
| 23 | std::vector<Utf8Char> out; |
| 24 | for (size_t i = 0; i < text.size();) { |
| 25 | const unsigned char ch = static_cast<unsigned char>(text[i]); |
| 26 | uint32_t codepoint = 0; |
| 27 | size_t len = 1; |
| 28 | if ((ch & 0x80U) == 0) { |
| 29 | codepoint = ch; |
| 30 | } else if ((ch & 0xE0U) == 0xC0U) { |
| 31 | len = 2; |
| 32 | codepoint = ch & 0x1FU; |
| 33 | } else if ((ch & 0xF0U) == 0xE0U) { |
| 34 | len = 3; |
| 35 | codepoint = ch & 0x0FU; |
| 36 | } else if ((ch & 0xF8U) == 0xF0U) { |
| 37 | len = 4; |
| 38 | codepoint = ch & 0x07U; |
| 39 | } else { |
| 40 | throw std::runtime_error("Qwen3 forced aligner encountered invalid UTF-8"); |
| 41 | } |
| 42 | if (i + len > text.size()) { |
| 43 | throw std::runtime_error("Qwen3 forced aligner encountered truncated UTF-8"); |
| 44 | } |
| 45 | for (size_t j = 1; j < len; ++j) { |
| 46 | const unsigned char tail = static_cast<unsigned char>(text[i + j]); |
| 47 | if ((tail & 0xC0U) != 0x80U) { |
| 48 | throw std::runtime_error("Qwen3 forced aligner encountered invalid UTF-8 continuation byte"); |
| 49 | } |
| 50 | codepoint = (codepoint << 6U) | (tail & 0x3FU); |
| 51 | } |
| 52 | out.push_back({codepoint, text.substr(i, len)}); |
| 53 | i += len; |
no test coverage detected