Files
tbd-station-14/Content.Server/Speech/EntitySystems/LizardAccentSystem.cs
Pieter-Jan Briers 4a2a63a86b 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.
2024-05-06 08:57:32 +10:00

38 lines
1.2 KiB
C#

using System.Text.RegularExpressions;
using Content.Server.Speech.Components;
namespace Content.Server.Speech.EntitySystems;
public sealed class LizardAccentSystem : EntitySystem
{
private static readonly Regex RegexLowerS = new("s+");
private static readonly Regex RegexUpperS = new("S+");
private static readonly Regex RegexInternalX = new(@"(\w)x");
private static readonly Regex RegexLowerEndX = new(@"\bx([\-|r|R]|\b)");
private static readonly Regex RegexUpperEndX = new(@"\bX([\-|r|R]|\b)");
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LizardAccentComponent, AccentGetEvent>(OnAccent);
}
private void OnAccent(EntityUid uid, LizardAccentComponent component, AccentGetEvent args)
{
var message = args.Message;
// hissss
message = RegexLowerS.Replace(message, "sss");
// hiSSS
message = RegexUpperS.Replace(message, "SSS");
// ekssit
message = RegexInternalX.Replace(message, "$1kss");
// ecks
message = RegexLowerEndX.Replace(message, "ecks$1");
// eckS
message = RegexUpperEndX.Replace(message, "ECKS$1");
args.Message = message;
}
}