szaszm commented on code in PR #1387:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1387#discussion_r974441805
##########
libminifi/src/utils/RegexUtils.cpp:
##########
@@ -279,13 +372,39 @@ SMatch getLastRegexMatch(const std::string& string, const
utils::Regex& regex) {
current_str = search_result.suffix();
}
- auto diff = string.size() - last_match.string_.size();
- last_match.string_ = string;
+ if (!last_match.ready()) {
+ return last_match;
+ }
+
+ struct MatchInfo {
+ bool matched;
+ size_t begin;
+ size_t end;
+ };
+
+ // we must save the sub matches' info in a way that does
+ // not get invalidated by SMatch::reset, and can be transferred
+ // to the updated match
+ std::vector<MatchInfo> match_infos;
+ match_infos.reserve(last_match.size());
for (auto& match : last_match.matches_) {
- if (match.match.rm_so >= 0) {
- match.match.rm_so += diff;
- match.match.rm_eo += diff;
- }
+ match_infos.emplace_back(MatchInfo{
+ .matched = match.matched,
+ .begin = gsl::narrow<size_t>(std::distance(last_match.string_.cbegin(),
match.first)),
+ .end = gsl::narrow<size_t>(std::distance(last_match.string_.cbegin(),
match.second))
+ });
+ }
+ // offset of the start of the last match into the original string
+ auto offset = string.size() - last_match.string_.size();
+ last_match.reset(string);
+ last_match.ready_ = true;
+ for (auto& info : match_infos) {
+ size_t match_off = info.matched ? offset : 0;
+ last_match.matches_.emplace_back(SMatch::Regmatch{
Review Comment:
I would use push_back here for stronger type checking.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]