Files
tbd-station-14/Content.Server/Speech/EntitySystems/FrenchAccentSystem.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

47 lines
1.4 KiB
C#

using Content.Server.Speech.Components;
using System.Text.RegularExpressions;
namespace Content.Server.Speech.EntitySystems;
/// <summary>
/// System that gives the speaker a faux-French accent.
/// </summary>
public sealed class FrenchAccentSystem : EntitySystem
{
[Dependency] private readonly ReplacementAccentSystem _replacement = default!;
private static readonly Regex RegexTh = new(@"th", RegexOptions.IgnoreCase);
private static readonly Regex RegexStartH = new(@"(?<!\w)h", RegexOptions.IgnoreCase);
private static readonly Regex RegexSpacePunctuation = new(@"(?<=\w\w)[!?;:](?!\w)", RegexOptions.IgnoreCase);
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FrenchAccentComponent, AccentGetEvent>(OnAccentGet);
}
public string Accentuate(string message, FrenchAccentComponent component)
{
var msg = message;
msg = _replacement.ApplyReplacements(msg, "french");
// replaces th with dz
msg = RegexTh.Replace(msg, "'z");
// removes the letter h from the start of words.
msg = RegexStartH.Replace(msg, "'");
// spaces out ! ? : and ;.
msg = RegexSpacePunctuation.Replace(msg, " $&");
return msg;
}
private void OnAccentGet(EntityUid uid, FrenchAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message, component);
}
}