diff --git a/regex/matching.go b/regex/matching.go index 79c38cc..17c2bcb 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -2,6 +2,7 @@ package regex import ( "fmt" + "slices" "sort" ) @@ -320,7 +321,7 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in } } - currentStates, _ = uniqueAppend(currentStates, tempStates...) + currentStates = slices.Concat(currentStates, tempStates) tempStates = nil // Take any transitions corresponding to current character @@ -329,11 +330,15 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in lastStateInList := false // Whether or not a last state was in our list of states var lastStatePtr *nfaState = nil // Pointer to the last-state, if it was found lastLookaroundInList := false // Whether or not a last state (that is a lookaround) was in our list of states - for _, state := range currentStates { + for numStatesMatched == 0 && lastStateInList == false { + if len(currentStates) == 0 { + break + } + state, _ := pop(¤tStates) matches, numMatches := state.matchesFor(str, i) if numMatches > 0 { numStatesMatched++ - tempStates = append(tempStates, matches...) + tempStates = append([]*nfaState(nil), matches...) foundPath = true for _, m := range matches { if m.threadGroups == nil { @@ -383,12 +388,17 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in lastStateInList = true } } - if lastStateInList { // A last-state was in the list of states. add the matchIndex to our MatchIndex list + if lastStateInList && numStatesMatched == 0 { // A last-state was in the list of states. add the matchIndex to our MatchIndex list for j := 1; j < numGroups+1; j++ { tempIndices[j] = lastStatePtr.threadGroups[j] } endIdx = i tempIndices[0] = Group{startIdx, endIdx} + if tempIndices[0].StartIdx == tempIndices[0].EndIdx { + return true, tempIndices, tempIndices[0].EndIdx + 1 + } else { + return true, tempIndices, tempIndices[0].EndIdx + } } // Check if we can find a zero-length match