From 96fcb0c59d6c3a0e4013c2daee952b761e994f3a Mon Sep 17 00:00:00 2001 From: alexzzzs Date: Wed, 31 Dec 2025 00:47:13 +1100 Subject: [PATCH 1/3] Optimize FromUnderscoreDashSeparatedWords to reduce allocations --- src/Humanizer/StringHumanizeExtensions.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Humanizer/StringHumanizeExtensions.cs b/src/Humanizer/StringHumanizeExtensions.cs index cf4f24ead..a0fe74889 100644 --- a/src/Humanizer/StringHumanizeExtensions.cs +++ b/src/Humanizer/StringHumanizeExtensions.cs @@ -32,8 +32,24 @@ public static partial class StringHumanizeExtensions private static Regex FreestandingSpacingCharRegex() => FreestandingSpacingCharRegexField; #endif - static string FromUnderscoreDashSeparatedWords(string input) => - string.Join(" ", input.Split(['_', '-'])); + static string FromUnderscoreDashSeparatedWords(string input) + { + #if NET7_0_OR_GREATER + return string.Create(input.Length, input, (span, state) => + { + state.AsSpan().CopyTo(span); + for (var i = 0; i < span.Length; i++) + { + if (span[i] == '_' || span[i] == '-') + { + span[i] = ' '; + } + } + }); + #else + return input.Replace('_', ' ').Replace('-', ' '); + #endif + } static string FromPascalCase(string input) { @@ -160,4 +176,4 @@ internal static unsafe string Concat(char left, CharSpan right) => internal static unsafe string Concat(CharSpan left, char right) => Concat(left, new CharSpan(&right, 1)); #endif -} \ No newline at end of file +} From 8403780a16ccfd02f0c1a483f84c8eaf455d661b Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 7 Jan 2026 22:39:13 +1100 Subject: [PATCH 2/3] Use MemoryExtensions.Replace instead of manual for loop --- src/Humanizer/StringHumanizeExtensions.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Humanizer/StringHumanizeExtensions.cs b/src/Humanizer/StringHumanizeExtensions.cs index a0fe74889..64b1da4f0 100644 --- a/src/Humanizer/StringHumanizeExtensions.cs +++ b/src/Humanizer/StringHumanizeExtensions.cs @@ -32,25 +32,19 @@ public static partial class StringHumanizeExtensions private static Regex FreestandingSpacingCharRegex() => FreestandingSpacingCharRegexField; #endif - static string FromUnderscoreDashSeparatedWords(string input) + static string FromUnderscoreDashSeparatedWords(string input) { #if NET7_0_OR_GREATER return string.Create(input.Length, input, (span, state) => { state.AsSpan().CopyTo(span); - for (var i = 0; i < span.Length; i++) - { - if (span[i] == '_' || span[i] == '-') - { - span[i] = ' '; - } - } + span.Replace('_', ' '); + span.Replace('-', ' '); }); #else return input.Replace('_', ' ').Replace('-', ' '); #endif } - static string FromPascalCase(string input) { var result = string.Join(" ", PascalCaseWordPartsRegex() From f3a7a456c15c4ccb0c2ebf805d6aa8ef98ce303e Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 9 Jan 2026 00:16:16 +1100 Subject: [PATCH 3/3] fix indentation/whitespace --- src/Humanizer/StringHumanizeExtensions.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Humanizer/StringHumanizeExtensions.cs b/src/Humanizer/StringHumanizeExtensions.cs index 64b1da4f0..6142cbcba 100644 --- a/src/Humanizer/StringHumanizeExtensions.cs +++ b/src/Humanizer/StringHumanizeExtensions.cs @@ -32,19 +32,20 @@ public static partial class StringHumanizeExtensions private static Regex FreestandingSpacingCharRegex() => FreestandingSpacingCharRegexField; #endif - static string FromUnderscoreDashSeparatedWords(string input) + static string FromUnderscoreDashSeparatedWords(string input) { - #if NET7_0_OR_GREATER - return string.Create(input.Length, input, (span, state) => - { - state.AsSpan().CopyTo(span); - span.Replace('_', ' '); - span.Replace('-', ' '); - }); - #else - return input.Replace('_', ' ').Replace('-', ' '); - #endif +#if NET7_0_OR_GREATER + return string.Create(input.Length, input, (span, state) => + { + state.AsSpan().CopyTo(span); + span.Replace('_', ' '); + span.Replace('-', ' '); + }); +#else + return input.Replace('_', ' ').Replace('-', ' '); +#endif } + static string FromPascalCase(string input) { var result = string.Join(" ", PascalCaseWordPartsRegex()