Cache regex instances in most cases (#27699)
Using static Regex functions that take in a pattern is bad because the pattern constantly needs to be re-parsed. With https://github.com/space-wizards/RobustToolbox/pull/5107, the engine has an analyzer to warn for this practice now. This commit brings most of content up to snuff already, though some of the tricker code I left for somebody else.
This commit is contained in:
committed by
GitHub
parent
70d3cf7ba4
commit
4a2a63a86b
@@ -5,8 +5,12 @@ namespace Content.Server.Speech.EntitySystems;
|
||||
|
||||
public sealed class SouthernAccentSystem : EntitySystem
|
||||
{
|
||||
private static readonly Regex RegexIng = new(@"ing\b");
|
||||
private static readonly Regex RegexAnd = new(@"\band\b");
|
||||
private static readonly Regex RegexDve = new("d've");
|
||||
|
||||
[Dependency] private readonly ReplacementAccentSystem _replacement = default!;
|
||||
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -20,9 +24,9 @@ public sealed class SouthernAccentSystem : EntitySystem
|
||||
message = _replacement.ApplyReplacements(message, "southern");
|
||||
|
||||
//They shoulda started runnin' an' hidin' from me!
|
||||
message = Regex.Replace(message, @"ing\b", "in'");
|
||||
message = Regex.Replace(message, @"\band\b", "an'");
|
||||
message = Regex.Replace(message, "d've", "da");
|
||||
message = RegexIng.Replace(message, "in'");
|
||||
message = RegexAnd.Replace(message, "an'");
|
||||
message = RegexDve.Replace(message, "da");
|
||||
args.Message = message;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user