tryMatch method
Returns the end index (exclusive) of the longest prefix of input matched
by this pattern, or -1 if no prefix of input matches.
Implementation
@override
int tryMatch(String input, int start, int end) {
var result = -1;
var currentStates = <NfaState>{};
var nextStates = <NfaState>{};
_addStates(this.start, currentStates, start, end);
if (currentStates.any((state) => state.isEnd)) {
result = start;
}
for (var i = start; i < end; i++) {
final value = input.codeUnitAt(i);
nextStates.clear();
for (final state in currentStates) {
final nextState = state.transitions[value];
if (nextState != null) {
_addStates(nextState, nextStates, i + 1, end);
}
for (final nextState in state.dots) {
_addStates(nextState, nextStates, i + 1, end);
}
}
if (nextStates.isEmpty) {
break;
}
(currentStates, nextStates) = (nextStates, currentStates);
if (currentStates.any((state) => state.isEnd)) {
result = i + 1;
}
}
return result;
}