MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / unicode_regex_split_stl

Function unicode_regex_split_stl

smallthinker/src/unicode.cpp:496–523  ·  view source on GitHub ↗

use std::wregex to split the text

Source from the content-addressed store, hash-verified

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

Callers 1

unicode_regex_splitFunction · 0.85

Calls 4

reserveMethod · 0.80
lengthMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected