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

Method try_find_regex

smallthinker/common/chat-parser.cpp:174–192  ·  view source on GitHub ↗

Tries to find the regex, consumes it (pos right after it) and gives the prelude (right before it) and the groups to the callback.

Source from the content-addressed store, hash-verified

172
173// Tries to find the regex, consumes it (pos right after it) and gives the prelude (right before it) and the groups to the callback.
174std::optional<common_chat_msg_parser::find_regex_result> common_chat_msg_parser::try_find_regex(const common_regex & regex, size_t from, bool add_prelude_to_content) {
175 auto m = regex.search(input_, from == std::string::npos ? pos_ : from);
176 if (m.type == COMMON_REGEX_MATCH_TYPE_NONE) {
177 return std::nullopt;
178 }
179 auto prelude = input_.substr(pos_, m.groups[0].begin - pos_);
180 pos_ = m.groups[0].end;
181
182 if (add_prelude_to_content) {
183 add_content(prelude);
184 }
185 if (m.type == COMMON_REGEX_MATCH_TYPE_PARTIAL) {
186 if (is_partial()) {
187 throw common_chat_msg_partial_exception(regex.str());
188 }
189 return std::nullopt;
190 }
191 return find_regex_result{prelude, m.groups};
192}
193
194common_chat_msg_parser::find_regex_result common_chat_msg_parser::consume_regex(const common_regex & regex) {
195 if (auto result = try_consume_regex(regex)) {

Calls 4

substrMethod · 0.80
searchMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected