From 70ce570fe9361d5b90494d159faa4fd512ac479d Mon Sep 17 00:00:00 2001 From: Tayrtahn Date: Thu, 12 Jun 2025 01:09:05 -0400 Subject: [PATCH] Cleanup chat highlighting code (#38235) * Cache regexes * Convert static LocManager to resolved * Use Any instead of Count > 0 * Use var instead of explicit types * Make _highlights readonly --- .../Chat/ChatUIController.Highlighting.cs | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Content.Client/UserInterface/Systems/Chat/ChatUIController.Highlighting.cs b/Content.Client/UserInterface/Systems/Chat/ChatUIController.Highlighting.cs index e413999b2c..1670823aab 100644 --- a/Content.Client/UserInterface/Systems/Chat/ChatUIController.Highlighting.cs +++ b/Content.Client/UserInterface/Systems/Chat/ChatUIController.Highlighting.cs @@ -14,12 +14,17 @@ namespace Content.Client.UserInterface.Systems.Chat; /// public sealed partial class ChatUIController : IOnSystemChanged { + [Dependency] private readonly ILocalizationManager _loc = default!; [UISystemDependency] private readonly CharacterInfoSystem _characterInfo = default!; + private static readonly Regex StartDoubleQuote = new("\"$"); + private static readonly Regex EndDoubleQuote = new("^\"|(?<=^@)\""); + private static readonly Regex StartAtSign = new("^@"); + /// /// The list of words to be highlighted in the chatbox. /// - private List _highlights = new(); + private readonly List _highlights = new(); /// /// The string holding the hex color used to highlight words. @@ -42,7 +47,7 @@ public sealed partial class ChatUIController : IOnSystemChanged { _highlightsColor = value; }, true); // Load highlights if any were saved. - string highlights = _config.GetCVar(CCVars.ChatHighlights); + var highlights = _config.GetCVar(CCVars.ChatHighlights); if (!string.IsNullOrEmpty(highlights)) { @@ -84,12 +89,12 @@ public sealed partial class ChatUIController : IOnSystemChanged (c == '"')) > 0) + if (keyword.Any(c => c == '"')) { // Matches the last double quote character. - keyword = Regex.Replace(keyword, "\"$", "(?!\\w)"); + keyword = StartDoubleQuote.Replace(keyword, "(?!\\w)"); // When matching for the first double quote character we also consider the possibility // of the double quote being preceded by a @ character. - keyword = Regex.Replace(keyword, "^\"|(?<=^@)\"", "(? (c == ' ' || c == '-')) == 1) @@ -144,9 +149,9 @@ public sealed partial class ChatUIController : IOnSystemChanged