diff --git a/Telegrator/Filters/MessageTextFilters.cs b/Telegrator/Filters/MessageTextFilters.cs index ecc9b09..7241136 100644 --- a/Telegrator/Filters/MessageTextFilters.cs +++ b/Telegrator/Filters/MessageTextFilters.cs @@ -180,26 +180,6 @@ namespace Telegrator.Filters /// protected override bool CanPassNext(FilterExecutionContext context) - { - int index = Text.IndexOf(Word, StartIndex, Comparison); - if (index == -1) - return false; - - if (index > 0) - { - char prev = Text[index - 1]; - if (char.IsLetter(prev)) - return false; - } - - if (index + Word.Length < Text.Length) - { - char post = Text[index + Word.Length]; - if (char.IsLetter(post)) - return false; - } - - return true; - } + => Text.ContainsWord(Word, Comparison, StartIndex); } } diff --git a/Telegrator/TypesExtensions.cs b/Telegrator/TypesExtensions.cs index 398da21..2d91ab6 100644 --- a/Telegrator/TypesExtensions.cs +++ b/Telegrator/TypesExtensions.cs @@ -21,6 +21,7 @@ using Telegrator.Providers; using Telegrator.StateKeeping; using Telegrator.StateKeeping.Abstracts; using Telegrator.StateKeeping.Components; +using static System.Net.Mime.MediaTypeNames; namespace Telegrator { @@ -1252,6 +1253,38 @@ namespace Telegrator chars[index] = char.ToLower(chars[index]); return new string(chars); } + + /// + /// Checks if string contains a 'word'. + /// 'Word' must be a separate member of the text, and not have any alphabetic characters next to it. + /// + /// + /// + /// + /// + /// + public static bool ContainsWord(this string source, string word, StringComparison comparison = StringComparison.InvariantCulture, int startIndex = 0) + { + int index = source.IndexOf(word, startIndex, comparison); + if (index == -1) + return false; + + if (index > 0) + { + char prev = source[index - 1]; + if (char.IsLetter(prev)) + return false; + } + + if (index + word.Length < source.Length) + { + char post = source[index + word.Length]; + if (char.IsLetter(post)) + return false; + } + + return true; + } } ///