use std::wregex to split the text
| 499 | |
| 500 | // use std::wregex to split the text |
| 501 | static std::vector<size_t> unicode_regex_split_stl(const std::wstring & wtext, const std::wstring & regex_expr, const std::vector<size_t> & offsets) { |
| 502 | std::wregex expr(regex_expr, std::regex_constants::optimize | std::regex_constants::nosubs); |
| 503 | std::vector<size_t> bpe_offsets; // store the offset of each word |
| 504 | bpe_offsets.reserve(offsets.size()); // Reserve memory for the approximate size |
| 505 | size_t start = 0; |
| 506 | for (auto offset : offsets) { |
| 507 | std::wcregex_iterator it(wtext.data() + start, wtext.data() + start + offset, expr); |
| 508 | std::wcregex_iterator end; |
| 509 | |
| 510 | int64_t start_idx = 0; |
| 511 | while (it != end) { |
| 512 | std::wcmatch match = *it; |
| 513 | if (match.position() > start_idx) { |
| 514 | bpe_offsets.emplace_back(match.position() - start_idx); |
| 515 | } |
| 516 | bpe_offsets.emplace_back(match.length()); |
| 517 | start_idx = match.position() + match.length(); |
| 518 | ++it; |
| 519 | } |
| 520 | |
| 521 | if (start_idx < (int64_t) offset) { |
| 522 | bpe_offsets.emplace_back(offset - start_idx); |
| 523 | } |
| 524 | start += offset; |
| 525 | } |
| 526 | |
| 527 | return bpe_offsets; |
| 528 | } |
| 529 | |
| 530 | // use std::regex to split the text |
| 531 | static std::vector<size_t> unicode_regex_split_stl(const std::string & text, const std::string & regex_expr, const std::vector<size_t> & offsets) { |
no test coverage detected