Match zero-length match at end of string, even if the start node is an assertion (end of string, lookarounds, etc.)
This commit is contained in:
12
matching.go
12
matching.go
@@ -34,10 +34,10 @@ func takeZeroState(states []*State) (rtv []*State, isZero bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// zeroMatchPossible returns true if a zero-length match is possible
|
// 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,
|
// It uses the same algorithm to find zero-states as the one inside the loop,
|
||||||
// so I should probably put it in a function.
|
// 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)
|
zerostates, iszero := takeZeroState(states)
|
||||||
tempstates := make([]*State, 0, len(zerostates)+len(states))
|
tempstates := make([]*State, 0, len(zerostates)+len(states))
|
||||||
tempstates = append(tempstates, states...)
|
tempstates = append(tempstates, states...)
|
||||||
@@ -51,7 +51,7 @@ func zeroMatchPossible(states ...*State) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, state := range tempstates {
|
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
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,8 +117,8 @@ func findAllMatchesHelper(start *State, str []rune, offset int) (bool, MatchInde
|
|||||||
if offset == len(str) {
|
if offset == len(str) {
|
||||||
// Get all zero-state matches. If we can get to a zero-state without matching anything, we
|
// 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.
|
// 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 start.isEmpty {
|
||||||
if zeroMatchPossible(start) {
|
if zeroMatchPossible(str, offset, start) {
|
||||||
return true, MatchIndex{offset, offset}, offset + 1
|
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
|
// Check if we can find a zero-length match
|
||||||
if foundPath == false {
|
if foundPath == false {
|
||||||
if zeroMatchPossible(currentStates...) {
|
if zeroMatchPossible(str, i, currentStates...) {
|
||||||
tempIndices, _ = unique_append(tempIndices, MatchIndex{startIdx, startIdx})
|
tempIndices, _ = unique_append(tempIndices, MatchIndex{startIdx, startIdx})
|
||||||
}
|
}
|
||||||
// If we haven't moved in the string, increment the counter by 1
|
// If we haven't moved in the string, increment the counter by 1
|
||||||
|
Reference in New Issue
Block a user