Moved TextContainsWordFilter's logic to extension method
This commit is contained in:
@@ -180,26 +180,6 @@ namespace Telegrator.Filters
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if string contains a 'word'.
|
||||
/// 'Word' must be a separate member of the text, and not have any alphabetic characters next to it.
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="word"></param>
|
||||
/// <param name="comparison"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user