summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibRegex
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-04-23 03:55:19 +0430
committerLinus Groh <mail@linusgroh.de>2021-04-23 10:05:04 +0200
commitbf9c04a3dadc6d2785054ee772e06c016b278e28 (patch)
tree4ffa75e063ab1033f17ff917f39fe9c79082fc7b /Userland/Libraries/LibRegex
parentbb40d4d5fff1ca729c14171561d03621d765854e (diff)
downloadserenity-bf9c04a3dadc6d2785054ee772e06c016b278e28.zip
LibRegex: Implement multiline stateful matches
Diffstat (limited to 'Userland/Libraries/LibRegex')
-rw-r--r--Userland/Libraries/LibRegex/RegexMatcher.cpp21
1 files changed, 19 insertions, 2 deletions
diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp
index c5e481b4d9..566ac74fd5 100644
--- a/Userland/Libraries/LibRegex/RegexMatcher.cpp
+++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp
@@ -82,9 +82,21 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
input.regex_options = m_regex_options | regex_options.value_or({}).value();
input.start_offset = m_pattern.start_offset;
output.operations = 0;
+ size_t lines_to_skip = 0;
- if (input.regex_options.has_flag_set(AllFlags::Internal_Stateful))
- VERIFY(views.size() == 1);
+ if (input.regex_options.has_flag_set(AllFlags::Internal_Stateful)) {
+ if (views.size() > 1 && input.start_offset > views.first().length()) {
+ dbgln("Started with start={}, goff={}, skip={}", input.start_offset, input.global_offset, lines_to_skip);
+ for (auto& view : views) {
+ if (input.start_offset < view.length() + 1)
+ break;
+ ++lines_to_skip;
+ input.start_offset -= view.length() + 1;
+ input.global_offset += view.length() + 1;
+ }
+ dbgln("Ended with start={}, goff={}, skip={}", input.start_offset, input.global_offset, lines_to_skip);
+ }
+ }
if (c_match_preallocation_count) {
output.matches.ensure_capacity(c_match_preallocation_count);
@@ -127,6 +139,11 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
continue_search = false;
for (auto& view : views) {
+ if (lines_to_skip != 0) {
+ ++input.line;
+ --lines_to_skip;
+ continue;
+ }
input.view = view;
dbgln_if(REGEX_DEBUG, "[match] Starting match with view ({}): _{}_", view.length(), view);