Skip to content

Commit

Permalink
optimize EnglishArticle.AppendArticlePrefix (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 21, 2024
1 parent 287d2ff commit daf7ea8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<ProjectReference Include="..\Humanizer\Humanizer.csproj" />
<!-- <PackageReference Include="Humanizer" Version="2.14.1" />-->
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Benchmarks/EnglishArticleBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MemoryDiagnoser(false)]
public class EnglishArticleBenchmarks
{
[Benchmark]
public string[] AppendArticlePrefix() =>
EnglishArticle.AppendArticlePrefix([ "Ant", "The Theater", "The apple", "Fox", "Bear"]);
}
3 changes: 2 additions & 1 deletion src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
BenchmarkRunner.Run<EnumBenchmarks>();
BenchmarkRunner.Run(
typeof(Program).Assembly);
1 change: 1 addition & 0 deletions src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ArticlePrefixSortTests
[InlineData(new[] { "Ant", "The Theater", "The apple", "Fox", "Bear" }, new[] { "Ant", "The apple", "Bear", "Fox", "The Theater" })]
[InlineData(new[] { "An Ant", "The Theater", "the apple", "a Fox", "Bear" }, new[] { "An Ant", "the apple", "Bear", "a Fox", "The Theater" })]
[InlineData(new[] { "Ant", "A Theater", "an apple", "Fox", "Bear" }, new[] { "Ant", "an apple", "Bear", "Fox", "A Theater" })]
[InlineData(new[] { " Ant ", " A Theater ", " an apple ", " Fox", "Bear " }, new[] { "A Theater", "an apple", "Ant", "Bear", "Fox" })]
public void SortStringArrayIgnoringArticlePrefixes(string[] input, string[] expectedOutput) =>
Assert.Equal(expectedOutput, EnglishArticle.PrependArticleSuffix(EnglishArticle.AppendArticlePrefix(input)));

Expand Down
12 changes: 6 additions & 6 deletions src/Humanizer/ArticlePrefixSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public static string[] AppendArticlePrefix(string[] items)

for (var i = 0; i < items.Length; i++)
{
var item = items[i];
var item = items[i].AsSpan();
if (_regex.IsMatch(item))
{
var article = item.Substring(0, item.IndexOf(" ", StringComparison.CurrentCulture));
var removed = item.Remove(0, item.IndexOf(" ", StringComparison.CurrentCulture));
var appended = $"{removed} {article}";
transformed[i] = appended.Trim();
var indexOf = item.IndexOf(' ');
var removed = item[indexOf..].TrimStart();
var article = item[..indexOf].TrimEnd();
transformed[i] = $"{removed} {article}";
}
else
{
transformed[i] = item.Trim();
transformed[i] = item.Trim().ToString();
}
}
Array.Sort(transformed);
Expand Down

0 comments on commit daf7ea8

Please sign in to comment.