MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / unicode_regex_split_stl

Function unicode_regex_split_stl

subprojects/llama.cpp/src/unicode.cpp:501–528  ·  view source on GitHub ↗

use std::wregex to split the text

Source from the content-addressed store, hash-verified

499
500// use std::wregex to split the text
501static 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
531static 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

sizeMethod · 0.65
lengthMethod · 0.65
dataMethod · 0.45
positionMethod · 0.45

Tested by

no test coverage detected