Fixed Kleene Star matching
This commit is contained in:
26
matching.go
26
matching.go
@@ -31,22 +31,30 @@ func match(start *State, str string) (startIdx int, endIdx int, matched bool) {
|
||||
for _, state := range currentStates {
|
||||
if len(state.transitions[int(str[i])]) > 0 {
|
||||
tempStates = append(tempStates, state.transitions[int(str[i])]...)
|
||||
} else {
|
||||
// This enables the 'greedy' behavior - last-state status is only checked if we can't match anything else
|
||||
if state.isLast {
|
||||
endIdx = i
|
||||
return startIdx, endIdx, true
|
||||
}
|
||||
}
|
||||
}
|
||||
copy(currentStates, tempStates)
|
||||
tempStates = nil
|
||||
|
||||
// If any of the current-states is a last state, return true.
|
||||
for _, state := range currentStates {
|
||||
if state.isLast {
|
||||
endIdx = i
|
||||
return startIdx, endIdx, true
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
// We don't seem to have reached a last-state. Return fals
|
||||
return -1, -1, false
|
||||
// End-of-string reached. Check if any of our states is in the end position.
|
||||
for _, state := range currentStates {
|
||||
if state.isLast {
|
||||
endIdx = i
|
||||
return startIdx, endIdx, true
|
||||
} else {
|
||||
return -1, -1, false
|
||||
}
|
||||
}
|
||||
|
||||
// Default
|
||||
return -1, -1, false
|
||||
}
|
||||
|
Reference in New Issue
Block a user