Match zero-length match at end of string, even if the start node is an assertion (end of string, lookarounds, etc.)

master
Aadhavan Srinivasan 1 month ago
parent 11c0a0552f
commit 051a8551f3

@ -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

Loading…
Cancel
Save