From d9d2140ea2af084b0455f84bc9948e3d639cdfb8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 4 Feb 2019 17:33:29 +1100 Subject: [PATCH] Fix regex not always selecting the leftmost longest match (Actually the rightmost longest match when searching backwards) Fixes #2710 --- src/regex_impl.cc | 6 ++++++ src/regex_impl.hh | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/regex_impl.cc b/src/regex_impl.cc index 8742e19c..1ff73472 100644 --- a/src/regex_impl.cc +++ b/src/regex_impl.cc @@ -1489,6 +1489,12 @@ auto test_regex = UnitTest{[]{ kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "baz"); } + { + TestVM vm{R"(a[^\n]*\n|\n)"}; + kak_assert(vm.exec("foo\nbar\nb", RegexExecFlags::None)); + kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "ar\n"); + } + { TestVM<> vm{R"(()*)"}; kak_assert(not vm.exec(" ")); diff --git a/src/regex_impl.hh b/src/regex_impl.hh index 962fbf61..ee374429 100644 --- a/src/regex_impl.hh +++ b/src/regex_impl.hh @@ -510,7 +510,7 @@ private: forward ? utf8::to_next(pos, config.subject_end) : utf8::to_previous(pos, config.subject_begin); - if (search) + if (search and not m_found_match) { if (start_desc and m_threads.next_is_empty()) to_next_start(pos, config, *start_desc);