From 9d6344719f3e5e1efbf238d2805798a24bc932a0 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 14 Feb 2025 11:37:28 -0500 Subject: [PATCH] Reverse order of trying branches if the quantifier is lazy --- regex/matching.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/regex/matching.go b/regex/matching.go index e1e7d9b..330f7c7 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -234,14 +234,14 @@ func addStateToList(str []rune, idx int, list []nfaState, state nfaState, thread } visited = append(visited, state) - if state.isKleene || state.isQuestion { + if (state.isKleene || state.isQuestion) && (state.isLazy == false) { // Greedy quantifiers copyThread(state.splitState, state) list := addStateToList(str, idx, list, *state.splitState, threadGroups, visited, preferLongest) copyThread(state.next, state) list = addStateToList(str, idx, list, *state.next, threadGroups, visited, preferLongest) return list } - if state.isAlternation { + if state.isAlternation || ((state.isKleene || state.isQuestion) && state.isLazy) { // Alternation or lazy quantifier copyThread(state.next, state) list := addStateToList(str, idx, list, *state.next, threadGroups, visited, preferLongest) copyThread(state.splitState, state)