Skip to content

Commit fb3832e

Browse files
Optimize StringDehumanizeExtensions by caching compiled regexes
This change replaces inline, uncompiled regexes in ToPascalCase and ToSnakeCase with static readonly fields configured with RegexOptions.Compiled and RegexOptions.CultureInvariant. This avoids the overhead of parsing and compiling the patterns on every call, leading to better performance and fewer allocations. - Added static readonly Regex fields for all patterns. - Updated methods to use the cached instances. - Added RegexOptions.CultureInvariant for consistency with the class's goal. - Added a blank line before the return statement in ToCamelCase to follow style guidelines.
1 parent c17c67d commit fb3832e

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

osu.Game/Extensions/StringDehumanizeExtensions.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ namespace osu.Game.Extensions
3636
/// </summary>
3737
public static class StringDehumanizeExtensions
3838
{
39+
private static readonly Regex pascal_case_regex = new Regex(@"(?:^|_|-| +)(.)", RegexOptions.Compiled | RegexOptions.CultureInvariant);
40+
private static readonly Regex snake_case_step1_regex = new Regex(@"([\p{Lu}]+)([\p{Lu}][\p{Ll}])", RegexOptions.Compiled | RegexOptions.CultureInvariant);
41+
private static readonly Regex snake_case_step2_regex = new Regex(@"([\p{Ll}\d])([\p{Lu}])", RegexOptions.Compiled | RegexOptions.CultureInvariant);
42+
private static readonly Regex snake_case_step3_regex = new Regex(@"[-\s]", RegexOptions.Compiled | RegexOptions.CultureInvariant);
43+
3944
/// <summary>
4045
/// Converts the string to "Pascal case" (also known as "upper camel case").
4146
/// </summary>
@@ -46,7 +51,7 @@ public static class StringDehumanizeExtensions
4651
/// </example>
4752
public static string ToPascalCase(this string input)
4853
{
49-
return Regex.Replace(input, "(?:^|_|-| +)(.)", match => match.Groups[1].Value.ToUpperInvariant());
54+
return pascal_case_regex.Replace(input, match => match.Groups[1].Value.ToUpperInvariant());
5055
}
5156

5257
/// <summary>
@@ -60,6 +65,7 @@ public static string ToPascalCase(this string input)
6065
public static string ToCamelCase(this string input)
6166
{
6267
string word = input.ToPascalCase();
68+
6369
return word.Length > 0 ? char.ToLowerInvariant(word[0]) + word.Substring(1) : word;
6470
}
6571

@@ -73,9 +79,9 @@ public static string ToCamelCase(this string input)
7379
/// </example>
7480
public static string ToSnakeCase(this string input)
7581
{
76-
return Regex.Replace(
77-
Regex.Replace(
78-
Regex.Replace(input, @"([\p{Lu}]+)([\p{Lu}][\p{Ll}])", "$1_$2"), @"([\p{Ll}\d])([\p{Lu}])", "$1_$2"), @"[-\s]", "_").ToLowerInvariant();
82+
return snake_case_step3_regex.Replace(
83+
snake_case_step2_regex.Replace(
84+
snake_case_step1_regex.Replace(input, "$1_$2"), "$1_$2"), "_").ToLowerInvariant();
7985
}
8086

8187
/// <summary>

0 commit comments

Comments
 (0)