Fixed Kleene Star matching

This commit is contained in:
2024-10-22 17:07:01 -04:00
parent d191686168
commit bc11777ad5
3 changed files with 31 additions and 22 deletions

View File

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