From 2b97e4e124abd675ccbeef80fff4722d9f7b0fe3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 11 Oct 2017 19:24:01 +0800 Subject: Regex: Fix handling of ^ and $ in backward matching mode --- src/regex_impl.hh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/regex_impl.hh') diff --git a/src/regex_impl.hh b/src/regex_impl.hh index 6f2137ce..9a8efdd7 100644 --- a/src/regex_impl.hh +++ b/src/regex_impl.hh @@ -130,23 +130,28 @@ public: bool exec(Iterator begin, Iterator end, RegexExecFlags flags) { - const bool forward = direction == MatchDirection::Forward; + if (flags & RegexExecFlags::NotInitialNull and begin == end) + return false; + + constexpr bool forward = direction == MatchDirection::Forward; const bool prev_avail = flags & RegexExecFlags::PrevAvailable; + m_begin = Utf8It{utf8::iterator{forward ? begin : end, prev_avail ? begin-1 : begin, end}}; m_end = Utf8It{utf8::iterator{forward ? end : begin, prev_avail ? begin-1 : begin, end}}; - m_flags = flags; - - if (flags & RegexExecFlags::NotInitialNull and m_begin == m_end) - return false; + if (forward) + m_flags = flags; + else // Flip line begin/end flags as we flipped the instructions on compilation. + m_flags = (RegexExecFlags)(flags & ~(RegexExecFlags::NotEndOfLine | RegexExecFlags::NotBeginOfLine)) | + ((flags & RegexExecFlags::NotEndOfLine) ? RegexExecFlags::NotBeginOfLine : RegexExecFlags::None) | + ((flags & RegexExecFlags::NotBeginOfLine) ? RegexExecFlags::NotEndOfLine : RegexExecFlags::None); - const bool no_saves = (m_flags & RegexExecFlags::NoSaves); + const bool no_saves = (flags & RegexExecFlags::NoSaves); Utf8It start{m_begin}; const bool* start_chars = m_program.start_chars ? m_program.start_chars->map : nullptr; - if (flags & RegexExecFlags::Search) to_next_start(start, m_end, start_chars); -- cgit v1.2.3