Skip to content

Commit

Permalink
fix: VanillaStringMatch now behaves closer to the base game
Browse files Browse the repository at this point in the history
  • Loading branch information
Computerdores committed Apr 17, 2024
1 parent c5a8c3b commit d9a7dae
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions AdvancedTerminalAPI/AdvancedTerminalAPI/Vanillin/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ public static class Util {
public static T VanillaStringMatch<T>(this IEnumerable<T> enumerable, string word,
Converter<T, string> stringConverter, Predicate<T> predicate = null, int specificity = 3) {
IEnumerable<T> array = enumerable as T[] ?? enumerable.ToArray();
T tWord = array.
FirstOrDefault(n =>
string.Equals(stringConverter(n), word, StringComparison.CurrentCultureIgnoreCase) &&

T tWord = array.FirstOrDefault(n =>
string.Equals(stringConverter(n), word, StringComparison.CurrentCultureIgnoreCase) &&
(predicate?.Invoke(n) ?? true)
);

if (tWord != null || word.Length < specificity) return tWord;

for (int spec = word.Length; spec >= specificity && tWord == null; spec--) {
tWord = array.FirstOrDefault(n =>
stringConverter(n).StartsWith(word[..spec], StringComparison.CurrentCultureIgnoreCase) &&
(predicate?.Invoke(n) ?? true)
);
if (tWord == null && word.Length >= specificity)
tWord = array.
FirstOrDefault(n =>
stringConverter(n).StartsWith(word[..specificity], StringComparison.CurrentCultureIgnoreCase) &&
(predicate?.Invoke(n) ?? true)
);
}

return tWord;
}

Expand Down

0 comments on commit d9a7dae

Please sign in to comment.