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
@@ -26,6 +26,9 @@ namespace Content.Shared.Preferences
|
||||
[Serializable, NetSerializable]
|
||||
public sealed partial class HumanoidCharacterProfile : ICharacterProfile
|
||||
{
|
||||
private static readonly Regex RestrictedNameRegex = new("[^A-Z,a-z,0-9, -]");
|
||||
private static readonly Regex ICNameCaseRegex = new(@"^(?<word>\w)|\b(?<word>\w)(?=\w*$)");
|
||||
|
||||
public const int MaxNameLength = 32;
|
||||
public const int MaxDescLength = 512;
|
||||
|
||||
@@ -418,15 +421,13 @@ namespace Content.Shared.Preferences
|
||||
|
||||
if (configManager.GetCVar(CCVars.RestrictedNames))
|
||||
{
|
||||
name = Regex.Replace(name, @"[^A-Z,a-z,0-9, -]", string.Empty);
|
||||
name = RestrictedNameRegex.Replace(name, string.Empty);
|
||||
}
|
||||
|
||||
if (configManager.GetCVar(CCVars.ICNameCase))
|
||||
{
|
||||
// This regex replaces the first character of the first and last words of the name with their uppercase version
|
||||
name = Regex.Replace(name,
|
||||
@"^(?<word>\w)|\b(?<word>\w)(?=\w*$)",
|
||||
m => m.Groups["word"].Value.ToUpper());
|
||||
name = ICNameCaseRegex.Replace(name, m => m.Groups["word"].Value.ToUpper());
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(name))
|
||||
|
||||
Reference in New Issue
Block a user