| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | void append_utf8_codepoint(std::string & out, uint32_t codepoint) { |
| 36 | if (codepoint <= 0x7FU) { |
| 37 | out.push_back(static_cast<char>(codepoint)); |
| 38 | return; |
| 39 | } |
| 40 | if (codepoint <= 0x7FFU) { |
| 41 | out.push_back(static_cast<char>(0xC0U | (codepoint >> 6U))); |
| 42 | out.push_back(static_cast<char>(0x80U | (codepoint & 0x3FU))); |
| 43 | return; |
| 44 | } |
| 45 | if (codepoint <= 0xFFFFU) { |
| 46 | out.push_back(static_cast<char>(0xE0U | (codepoint >> 12U))); |
| 47 | out.push_back(static_cast<char>(0x80U | ((codepoint >> 6U) & 0x3FU))); |
| 48 | out.push_back(static_cast<char>(0x80U | (codepoint & 0x3FU))); |
| 49 | return; |
| 50 | } |
| 51 | if (codepoint <= 0x10FFFFU) { |
| 52 | out.push_back(static_cast<char>(0xF0U | (codepoint >> 18U))); |
| 53 | out.push_back(static_cast<char>(0x80U | ((codepoint >> 12U) & 0x3FU))); |
| 54 | out.push_back(static_cast<char>(0x80U | ((codepoint >> 6U) & 0x3FU))); |
| 55 | out.push_back(static_cast<char>(0x80U | (codepoint & 0x3FU))); |
| 56 | return; |
| 57 | } |
| 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) { |
no outgoing calls
no test coverage detected