From b91f43b031320ceae1fef489b30bee6ef68a2c8e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 30 Nov 2017 14:32:29 +0800 Subject: Regex: optimize parsing a bit --- src/regex_impl.cc | 71 ++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) (limited to 'src/regex_impl.cc') diff --git a/src/regex_impl.cc b/src/regex_impl.cc index 80243608..77f1bd2e 100644 --- a/src/regex_impl.cc +++ b/src/regex_impl.cc @@ -189,28 +189,16 @@ private: return {}; } - bool accept(StringView expected) + bool modifiers() { auto it = m_pos.base(); - for (auto expected_it = expected.begin(); expected_it != expected.end(); ++expected_it) + if (m_regex.end() - it >= 4 and *it++ == '(' and *it++ == '?') { - if (it == m_regex.end() or *it++ != *expected_it) + auto m = *it++; + if ((m != 'i' and m != 'I') or *it++ != ')') return false; - } - m_pos = Iterator{it, m_regex}; - return true; - } - - bool modifiers() - { - if (accept("(?i)")) - { - m_ignore_case = true; - return true; - } - if (accept("(?I)")) - { - m_ignore_case = false; + m_ignore_case = (m == 'i'); + m_pos = Iterator{it, m_regex}; return true; } return false; @@ -239,25 +227,29 @@ private: break; case '(': { - Optional lookaround_op; - constexpr struct { StringView prefix; ParsedRegex::Op op; } lookarounds[] = { - { "(?=", ParsedRegex::LookAhead }, - { "(?!", ParsedRegex::NegativeLookAhead }, - { "(?<=", ParsedRegex::LookBehind }, - { "(?= 2 and *it++ == '?' and *it++ == ':') + { + m_pos = Iterator{it, m_regex}; + return false; + } + return true; + }; + NodeIndex content = disjunction(captures() ? m_parsed_regex.capture_count++ : -1); if (at_end() or *m_pos++ != ')') parse_error("unclosed parenthesis"); return content; -- cgit v1.2.3