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
@@ -7,6 +7,8 @@ namespace Content.Server.Speech.EntitySystems;
|
||||
|
||||
public sealed class PirateAccentSystem : EntitySystem
|
||||
{
|
||||
private static readonly Regex FirstWordAllCapsRegex = new(@"^(\S+)");
|
||||
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly ReplacementAccentSystem _replacement = default!;
|
||||
|
||||
@@ -26,7 +28,7 @@ public sealed class PirateAccentSystem : EntitySystem
|
||||
return msg;
|
||||
//Checks if the first word of the sentence is all caps
|
||||
//So the prefix can be allcapped and to not resanitize the captial
|
||||
var firstWordAllCaps = !Regex.Match(msg, @"^(\S+)").Value.Any(char.IsLower);
|
||||
var firstWordAllCaps = !FirstWordAllCapsRegex.Match(msg).Value.Any(char.IsLower);
|
||||
|
||||
var pick = _random.Pick(component.PirateWords);
|
||||
var pirateWord = Loc.GetString(pick);
|
||||
|
||||
Reference in New Issue
Block a user