Skip to content

Fix StringTrie so it handles partial matches better #2813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/src/comment_references/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ class StringTrie {
/// Does [this] node represent a valid entry in the trie?
bool valid = false;

/// Greedily match on the string trie. Returns the index of the first
/// non-operator character if valid, otherwise -1.
int match(String toMatch, [int index = 0]) {
if (index < 0 || index >= toMatch.length) return valid ? index : 1;
/// Greedily match on the string trie starting at the given index. Returns
/// the index of the first non-operator character if a match is valid,
/// otherwise -1.
int match(String toMatch, [int index = 0, int lastValid = -1]) {
if (index < 0 || index > toMatch.length) {
return lastValid;
}
if (index == toMatch.length) {
return valid ? index : lastValid;
}
var matchChar = toMatch.codeUnitAt(index);
if (children.containsKey(matchChar)) {
return children[matchChar].match(toMatch, index + 1);
lastValid = valid ? index : lastValid;
return children[matchChar].match(toMatch, index + 1, lastValid);
}
return valid ? index : -1;
return valid ? index : lastValid;
}

void addWord(String toAdd) {
Expand Down
22 changes: 22 additions & 0 deletions test/comment_referable/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,30 @@ void main() {
isEmpty);
}

group('StringTrie tests', () {
test('test partials', () {
var trie = StringTrie()..addWord('longerString');
expect(trie.match('l'), equals(-1));
expect(trie.match('longerStringWithExtras'), equals(12));
expect(trie.match('longerString'), equals(12));
expect(trie.match('prefix-longerString'), equals(-1));
});

test('substrings', () {
var trie = StringTrie();
trie.addWord('aString');
trie.addWord('aStr');
trie.addWord('a');
expect(trie.match('a'), equals(1));
expect(trie.match('aS'), equals(1));
expect(trie.match('aStr'), equals(4));
expect(trie.match('aStringTwo'), equals(7));
});
});

group('Basic comment reference parsing', () {
test('Check that basic references parse', () {
expectParseEquivalent('Funvas.u', ['Funvas', 'u']);
expectParseEquivalent('valid', ['valid']);
expectParseEquivalent('new valid', ['valid'], constructorHint: true);
expectParseEquivalent('valid()', ['valid'], callableHint: true);
Expand Down