| 9 | rx_reversed_partial(regex_to_reversed_partial_regex(pattern)) {} |
| 10 | |
| 11 | common_regex_match common_regex::search(const std::string & input, size_t pos, bool as_match) const { |
| 12 | std::smatch match; |
| 13 | if (pos > input.size()) { |
| 14 | throw std::runtime_error("Position out of bounds"); |
| 15 | } |
| 16 | auto start = input.begin() + pos; |
| 17 | auto found = as_match |
| 18 | ? std::regex_match(start, input.end(), match, rx) |
| 19 | : std::regex_search(start, input.end(), match, rx); |
| 20 | if (found) { |
| 21 | common_regex_match res; |
| 22 | res.type = COMMON_REGEX_MATCH_TYPE_FULL; |
| 23 | for (size_t i = 0; i < match.size(); ++i) { |
| 24 | auto begin = pos + match.position(i); |
| 25 | res.groups.emplace_back(begin, begin + match.length(i)); |
| 26 | } |
| 27 | return res; |
| 28 | } |
| 29 | std::match_results<std::string::const_reverse_iterator> srmatch; |
| 30 | if (std::regex_search(input.rbegin(), input.rend() - pos, srmatch, rx_reversed_partial, std::regex_constants::match_continuous)) { |
| 31 | auto group = srmatch[1].str(); |
| 32 | if (group.length() != 0) { |
| 33 | auto it = srmatch[1].second.base(); |
| 34 | // auto position = static_cast<size_t>(std::distance(input.begin(), it)); |
| 35 | if ((!as_match) || it == input.begin()) { |
| 36 | common_regex_match res; |
| 37 | res.type = COMMON_REGEX_MATCH_TYPE_PARTIAL; |
| 38 | const size_t begin = std::distance(input.begin(), it); |
| 39 | const size_t end = input.size(); |
| 40 | if (begin == std::string::npos || end == std::string::npos || begin > end) { |
| 41 | throw std::runtime_error("Invalid range"); |
| 42 | } |
| 43 | res.groups.push_back({begin, end}); |
| 44 | return res; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | Transforms a regex pattern to a partial match pattern that operates on a reversed input string to find partial final matches of the original pattern. |