Regex: Fix reverse searching behaviour, again

This commit is contained in:
Maxime Coste 2017-10-11 21:05:02 +08:00
parent 9753bcd0ad
commit 8c8dcb3a84
2 changed files with 9 additions and 3 deletions

View File

@ -1195,6 +1195,12 @@ auto test_regex = UnitTest{[]{
kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "boz"); kak_assert(StringView{vm.captures()[0], vm.captures()[1]} == "boz");
} }
{
TestVM<MatchDirection::Backward> vm{R"(foo)"};
kak_assert(vm.exec("foofoo", RegexExecFlags::Search));
kak_assert(*vm.captures()[1] == 0);
}
{ {
TestVM<MatchDirection::Backward> vm{R"($)"}; TestVM<MatchDirection::Backward> vm{R"($)"};
kak_assert(vm.exec("foo\nbar\nbaz\nqux", RegexExecFlags::Search | RegexExecFlags::NotEndOfLine)); kak_assert(vm.exec("foo\nbar\nbaz\nqux", RegexExecFlags::Search | RegexExecFlags::NotEndOfLine));

View File

@ -864,12 +864,12 @@ static bool find_prev(const Buffer& buffer, const BufferIterator& pos,
if (pos != buffer.begin() and if (pos != buffer.begin() and
regex_search<BufferIterator, MatchDirection::Backward>( regex_search<BufferIterator, MatchDirection::Backward>(
buffer.begin(), pos, matches, ex, buffer.begin(), pos, matches, ex,
match_flags(buffer, buffer.begin(), pos))) match_flags(buffer, buffer.begin(), pos) | RegexExecFlags::NotInitialNull))
return true; return true;
wrapped = true; wrapped = true;
return regex_search<BufferIterator, MatchDirection::Backward>( return regex_search<BufferIterator, MatchDirection::Backward>(
buffer.begin(), buffer.end(), matches, ex, buffer.begin(), buffer.end(), matches, ex,
match_flags(buffer, buffer.begin(), buffer.end())); match_flags(buffer, buffer.begin(), buffer.end()) | RegexExecFlags::NotInitialNull);
} }
template<MatchDirection direction> template<MatchDirection direction>
@ -881,7 +881,7 @@ Selection find_next_match(const Context& context, const Selection& sel, const Re
wrapped = false; wrapped = false;
const bool found = (direction == MatchDirection::Forward) ? const bool found = (direction == MatchDirection::Forward) ?
find_next(buffer, utf8::next(pos, buffer.end()), matches, regex, wrapped) find_next(buffer, utf8::next(pos, buffer.end()), matches, regex, wrapped)
: find_prev(buffer, utf8::previous(pos, buffer.begin()), matches, regex, wrapped); : find_prev(buffer, pos, matches, regex, wrapped);
if (not found or matches[0].first == buffer.end()) if (not found or matches[0].first == buffer.end())
throw runtime_error(format("'{}': no matches found", regex.str())); throw runtime_error(format("'{}': no matches found", regex.str()));