Moved TextContainsWordFilter's logic to extension method

This commit is contained in:
2025-08-24 22:44:55 +04:00
parent 884a4b126c
commit 64950b413c
2 changed files with 34 additions and 21 deletions
+1 -21
View File
@@ -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);
}
}
+33
View File
@@ -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>