Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize EnglishArticle.AppendArticlePrefix #1418

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
SimonCropp marked this conversation as resolved.
Show resolved Hide resolved
var article = item[..indexOf].TrimEnd();
transformed[i] = $"{removed} {article}";
}
else
{
transformed[i] = item.Trim();
transformed[i] = item.Trim().ToString();
}
}
Array.Sort(transformed);
Expand Down