Skip to content

Commit f54d95c

Browse files
committed
Fix word highlighting edge cases (Closes #656) (#658)
1 parent 5315a45 commit f54d95c

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

Daybreak/Services/TradeChat/WordHighlightingService.cs

+20-27
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,31 @@
77

88
namespace Daybreak.Services.TradeChat;
99

10-
internal sealed class WordHighlightingService : IWordHighlightingService
10+
internal sealed partial class WordHighlightingService : IWordHighlightingService
1111
{
12-
private static readonly Regex WordsSplitRegex = new("[\\s+\\|+]", RegexOptions.Compiled);
13-
private static readonly string[] BuyWords =
14-
[
15-
"wtb",
16-
"buy",
17-
"buying",
18-
"buyin",
19-
];
20-
private static readonly string[] SellWords =
21-
[
22-
"wts",
23-
"sell",
24-
"selling",
25-
"sellin"
26-
];
27-
private static readonly string[] TradeWords =
28-
[
29-
"wtt",
30-
"trade",
31-
"trading",
32-
"lf",
33-
"tradin"
34-
];
12+
private static readonly Regex WordsSplitRegex = SplitByWordsRegex();
13+
private static readonly Regex BuyWordsRegex = BuyWordsGroupRegex();
14+
private static readonly Regex SellWordsRegex = SellWordsGroupRegex();
15+
private static readonly Regex TradeWordsRegex = TradeWordsGroupRegex();
3516

3617
public IEnumerable<ColoredTextElement> ParseString(string s, SolidColorBrush foreground, SolidColorBrush buy, SolidColorBrush sell, SolidColorBrush trade)
3718
{
3819
var words = WordsSplitRegex.Split(s);
3920
foreach (var word in words)
4021
{
41-
if (BuyWords.Any(w => word.Contains(w, StringComparison.InvariantCultureIgnoreCase)))
22+
if (BuyWordsRegex.IsMatch(word))
4223
{
4324
yield return new ColoredTextElement { Color = buy, Text = word };
4425
continue;
4526
}
4627

47-
if (SellWords.Any(w => word.Contains(w, StringComparison.InvariantCultureIgnoreCase)))
28+
if (SellWordsRegex.IsMatch(word))
4829
{
4930
yield return new ColoredTextElement { Color = sell, Text = word };
5031
continue;
5132
}
5233

53-
if (TradeWords.Any(w => word.Contains(w, StringComparison.InvariantCultureIgnoreCase)))
34+
if (TradeWordsRegex.IsMatch(word))
5435
{
5536
yield return new ColoredTextElement { Color = trade, Text = word };
5637
continue;
@@ -59,4 +40,16 @@ public IEnumerable<ColoredTextElement> ParseString(string s, SolidColorBrush for
5940
yield return new ColoredTextElement { Color = foreground, Text = word };
6041
}
6142
}
43+
44+
[GeneratedRegex("[\\s+\\|+]", RegexOptions.Compiled)]
45+
private static partial Regex SplitByWordsRegex();
46+
47+
[GeneratedRegex("((wtb)|(buy)|(buying)|(buyin))", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
48+
private static partial Regex BuyWordsGroupRegex();
49+
50+
[GeneratedRegex("((wts)|(sell)|(selling)|(sellin))", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
51+
private static partial Regex SellWordsGroupRegex();
52+
53+
[GeneratedRegex("((wtt)|(trade)|(trading)|(lf)|(tradin))", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
54+
private static partial Regex TradeWordsGroupRegex();
6255
}

0 commit comments

Comments
 (0)