| 614 | } |
| 615 | |
| 616 | std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8) { |
| 617 | std::vector<uint32_t> result; |
| 618 | result.reserve(utf8.size()); |
| 619 | size_t offset = 0; |
| 620 | while (offset < utf8.size()) { |
| 621 | try { |
| 622 | result.push_back(unicode_cpt_from_utf8(utf8, offset)); |
| 623 | } |
| 624 | catch (const std::invalid_argument & /*ex*/) { |
| 625 | // Silently ignore invalid UTF-8 input to avoid leaking the exception beyond llama_tokenize |
| 626 | ++offset; |
| 627 | result.emplace_back(0xFFFD); // replacement character |
| 628 | } |
| 629 | } |
| 630 | return result; |
| 631 | } |
| 632 | |
| 633 | unicode_cpt_flags unicode_cpt_flags_from_cpt(const uint32_t cpt) { |
| 634 | static const unicode_cpt_flags undef(unicode_cpt_flags::UNDEFINED); |