From 051a8551f3b3a6773fb38dabe43993ea788c9640 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 22 Nov 2024 00:10:58 -0500 Subject: [PATCH] Match zero-length match at end of string, even if the start node is an assertion (end of string, lookarounds, etc.) --- matching.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/matching.go b/matching.go index 9941059..c66971e 100644 --- a/matching.go +++ b/matching.go @@ -34,10 +34,10 @@ func takeZeroState(states []*State) (rtv []*State, isZero bool) { } // zeroMatchPossible returns true if a zero-length match is possible -// from any of the given states. +// from any of the given states, given the string and our position in it. // It uses the same algorithm to find zero-states as the one inside the loop, // so I should probably put it in a function. -func zeroMatchPossible(states ...*State) bool { +func zeroMatchPossible(str []rune, idx int, states ...*State) bool { zerostates, iszero := takeZeroState(states) tempstates := make([]*State, 0, len(zerostates)+len(states)) tempstates = append(tempstates, states...) @@ -51,7 +51,7 @@ func zeroMatchPossible(states ...*State) bool { } } for _, state := range tempstates { - if state.isEmpty && state.assert == NONE && state.isLast { + if state.isEmpty && (state.assert == NONE || state.checkAssertion(str, idx)) && state.isLast { return true } } @@ -117,8 +117,8 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde if offset == len(str) { // Get all zero-state matches. If we can get to a zero-state without matching anything, we // can add a zero-length match. This is all true only if the start state itself matches nothing. - if start.isEmpty && start.assert == NONE { - if zeroMatchPossible(start) { + if start.isEmpty { + if zeroMatchPossible(str, offset, start) { return true, MatchIndex{offset, offset}, offset + 1 } } @@ -196,7 +196,7 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde } // Check if we can find a zero-length match if foundPath == false { - if zeroMatchPossible(currentStates...) { + if zeroMatchPossible(str, i, currentStates...) { tempIndices, _ = unique_append(tempIndices, MatchIndex{startIdx, startIdx}) } // If we haven't moved in the string, increment the counter by 1