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 partial class ParrotAccentSystem : EntitySystem
|
||||
{
|
||||
private static readonly Regex WordCleanupRegex = new Regex("[^A-Za-z0-9 -]");
|
||||
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
@@ -27,7 +29,7 @@ public sealed partial class ParrotAccentSystem : EntitySystem
|
||||
if (_random.Prob(entity.Comp.LongestWordRepeatChance))
|
||||
{
|
||||
// Don't count non-alphanumeric characters as parts of words
|
||||
var cleaned = Regex.Replace(message, "[^A-Za-z0-9 -]", string.Empty);
|
||||
var cleaned = WordCleanupRegex.Replace(message, string.Empty);
|
||||
// Split on whitespace and favor words towards the end of the message
|
||||
var words = cleaned.Split(null).Reverse();
|
||||
// Find longest word
|
||||
|
||||
Reference in New Issue
Block a user