Skip to content

Commit

Permalink
remove redundant alloc due to ToCharArray (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 16, 2024
1 parent 013cbbd commit e2a2876
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/Humanizer/StringHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ private static string FromPascalCase(string input)
{
var result = string.Join(" ", PascalCaseWordPartsRegex
.Matches(input).Cast<Match>()
.Select(match => match.Value.ToCharArray().All(char.IsUpper) &&
(match.Value.Length > 1 || (match.Index > 0 && input[match.Index - 1] == ' ') || match.Value == "I")
? match.Value
: match.Value.ToLower()));
.Select(match =>
{
var value = match.Value;
return value.All(char.IsUpper) &&
(value.Length > 1 || (match.Index > 0 && input[match.Index - 1] == ' ') || value == "I")
? value
: value.ToLower();
}));

if (result.Replace(" ", "").ToCharArray().All(c => char.IsUpper(c)) &&
if (result.Replace(" ", "").All(char.IsUpper) &&
result.Contains(" "))
{
result = result.ToLower();
Expand All @@ -51,7 +55,7 @@ private static string FromPascalCase(string input)
public static string Humanize(this string input)
{
// if input is all capitals (e.g. an acronym) then return it without change
if (input.ToCharArray().All(char.IsUpper))
if (input.All(char.IsUpper))
{
return input;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer/Transformer/ToTitleCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public string Transform(string input, CultureInfo culture)
}

private static bool AllCapitals(string input) =>
input.ToCharArray().All(char.IsUpper);
input.All(char.IsUpper);

private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture, bool firstWord)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string Truncate(string value, int length, string truncationString, Trunca

var alphaNumericalCharactersProcessed = 0;

if (value.ToCharArray().Count(char.IsLetterOrDigit) <= length)
if (value.Count(char.IsLetterOrDigit) <= length)
{
return value;
}
Expand Down

0 comments on commit e2a2876

Please sign in to comment.