Skip to content

Commit 6899779

Browse files
committed
[RegExp] Optimize the tryMatch method inside Nfa using double-buffered sets.
1 parent b5e1f05 commit 6899779

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

lib/src/regexp/nfa.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class Nfa extends RegexpPattern {
1414
int tryMatch(String input, int start, int end) {
1515
var result = -1;
1616
var currentStates = <NfaState>{};
17+
var nextStates = <NfaState>{};
1718
_addStates(this.start, currentStates);
1819
if (currentStates.any((state) => state.isEnd)) {
1920
result = start;
2021
}
2122
for (var i = start; i < end; i++) {
2223
final value = input.codeUnitAt(i);
23-
final nextStates = <NfaState>{};
24+
nextStates.clear();
2425
for (final state in currentStates) {
2526
final nextState = state.transitions[value];
2627
if (nextState != null) {
@@ -33,7 +34,7 @@ class Nfa extends RegexpPattern {
3334
if (nextStates.isEmpty) {
3435
break;
3536
}
36-
currentStates = nextStates;
37+
(currentStates, nextStates) = (nextStates, currentStates);
3738
if (currentStates.any((state) => state.isEnd)) {
3839
result = i + 1;
3940
}

0 commit comments

Comments
 (0)