Fixed kleene star matching at end of string - failed test a* and ppppppppaaaaaaaa

master
Aadhavan Srinivasan 2 months ago
parent 9d786997df
commit ce156c4405

@ -34,6 +34,7 @@ func matchHelper(start *State, str string, indices []matchIndex, offset int) []m
return indices return indices
} }
foundPath := false
startIdx := 0 startIdx := 0
endIdx := 0 endIdx := 0
currentStates := make([]*State, 0) currentStates := make([]*State, 0)
@ -47,12 +48,13 @@ func matchHelper(start *State, str string, indices []matchIndex, offset int) []m
startIdx = i startIdx = i
i++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character i++ // Advance to next character (if we aren't at a 0-state, which doesn't match anything), so that we can check for transitions. If we advance at a 0-state, we will never get a chance to match the first character
} }
// TODO - If start state is kleene star, try to match the next state startingFrom := i
currentStates = append(currentStates, start) currentStates = append(currentStates, start)
// Main loop // Main loop
for i < len(str) { for i < len(str) {
foundPath = false
zeroStates := make([]*State, 0) zeroStates := make([]*State, 0)
// Keep taking zero-states, until there are no more left to take // Keep taking zero-states, until there are no more left to take
zeroStates, isZero := takeZeroState(currentStates) zeroStates, isZero := takeZeroState(currentStates)
@ -69,16 +71,23 @@ func matchHelper(start *State, str string, indices []matchIndex, offset int) []m
for _, state := range currentStates { for _, state := range currentStates {
if len(state.transitions[int(str[i])]) > 0 { if len(state.transitions[int(str[i])]) > 0 {
tempStates = append(tempStates, state.transitions[int(str[i])]...) tempStates = append(tempStates, state.transitions[int(str[i])]...)
foundPath = true
} else { } else {
// This enables the 'greedy' behavior - last-state status is only checked if we can't match anything else // This enables the 'greedy' behavior - last-state status is only checked if we can't match anything else
if state.isLast { if state.isLast {
endIdx = i endIdx = i
indices = append(indices, matchIndex{startIdx + offset, endIdx + offset}) indices = append(indices, matchIndex{startIdx + offset, endIdx + offset})
} }
// Recursion - match with rest of string
return matchHelper(start, str[i:], indices, offset+i)
} }
} }
if foundPath == false {
// Recursion - match with rest of string if we have nowhere to go. If we haven't moved in the string, increment the counter by 1 to ensure we don't keep trying the same string over and over
if i == startingFrom {
i++
}
return matchHelper(start, str[i:], indices, offset+i)
}
currentStates = make([]*State, len(tempStates)) currentStates = make([]*State, len(tempStates))
copy(currentStates, tempStates) copy(currentStates, tempStates)
tempStates = nil tempStates = nil

Loading…
Cancel
Save