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:
Pieter-Jan Briers
2024-05-06 00:57:32 +02:00
committed by GitHub
parent 70d3cf7ba4
commit 4a2a63a86b
10 changed files with 74 additions and 60 deletions

View File

@@ -7,6 +7,8 @@ namespace Content.Server.Speech.EntitySystems
{
public sealed class ScrambledAccentSystem : EntitySystem
{
private static readonly Regex RegexLoneI = new(@"(?<=\ )i(?=[\ \.\?]|$)");
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
@@ -34,7 +36,7 @@ namespace Content.Server.Speech.EntitySystems
msg = msg[0].ToString().ToUpper() + msg.Remove(0, 1);
// Capitalize lone i's
msg = Regex.Replace(msg, @"(?<=\ )i(?=[\ \.\?]|$)", "I");
msg = RegexLoneI.Replace(msg, "I");
return msg;
}