From 95654e3e34270c0a25a1eba1403ef31620748f6b Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 27 Oct 2024 14:56:28 -0400 Subject: [PATCH] Take all possible 0-states (until no more left to take) before checking if we are in an acceptable position --- matching.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/matching.go b/matching.go index 09700cd..5afe6ca 100644 --- a/matching.go +++ b/matching.go @@ -98,12 +98,15 @@ func findAllMatchesHelper(start *State, str string, indices []matchIndex, offset i++ } - // End-of-string reached. Go to any 0-states. Then check if any of our states are in the end position. - for _, state := range currentStates { - if len(state.transitions[EPSILON]) > 0 { - tempStates = append(tempStates, state.transitions[EPSILON]...) - } + // End-of-string reached. Go to any 0-states, until there are no more 0-states to go to. Then check if any of our states are in the end position. + // This is the exact same algorithm used inside the loop, so I should probably put it in a function. + zeroStates, isZero := takeZeroState(currentStates) + tempStates = append(tempStates, zeroStates...) + for isZero == true { + zeroStates, isZero = takeZeroState(tempStates) + tempStates = append(tempStates, zeroStates...) } + currentStates = append(currentStates, tempStates...) tempStates = nil