| 58 | throw std::runtime_error("invalid unicode codepoint in chatterbox tokenizer"); |
| 59 | } |
| 60 | |
| 61 | std::vector<std::string> split_utf8_codepoints(const std::string & text) { |
| 62 | std::vector<std::string> parts; |
| 63 | for (size_t pos = 0; pos < text.size();) { |
| 64 | const unsigned char lead = static_cast<unsigned char>(text[pos]); |
| 65 | size_t width = 1; |
| 66 | if ((lead & 0x80U) == 0) { |
| 67 | width = 1; |
| 68 | } else if ((lead & 0xE0U) == 0xC0U) { |
| 69 | width = 2; |
| 70 | } else if ((lead & 0xF0U) == 0xE0U) { |
| 71 | width = 3; |
| 72 | } else if ((lead & 0xF8U) == 0xF0U) { |
| 73 | width = 4; |
| 74 | } else { |
| 75 | throw std::runtime_error("invalid utf-8 byte sequence in text tokenizer"); |
| 76 | } |
| 77 | if (pos + width > text.size()) { |
| 78 | throw std::runtime_error("truncated utf-8 byte sequence in text tokenizer"); |
| 79 | } |
| 80 | parts.emplace_back(text.substr(pos, width)); |
| 81 | pos += width; |
| 82 | } |
| 83 | return parts; |
| 84 | } |
| 85 | |
| 86 | std::pair<uint32_t, size_t> read_utf8_codepoint_at(const std::string & text, size_t pos) { |
no test coverage detected