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

Fix article sorting for words containing an article-like part inside #1421

Merged
merged 3 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/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ArticlePrefixSortTests
[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" })]
[InlineData(new[] { "The General Theory of Relativity" }, new[] { "The General Theory of Relativity" })]
public void SortStringArrayIgnoringArticlePrefixes(string[] input, string[] expectedOutput) =>
Assert.Equal(expectedOutput, EnglishArticle.PrependArticleSuffix(EnglishArticle.AppendArticlePrefix(input)));

Expand Down
53 changes: 13 additions & 40 deletions src/Humanizer/ArticlePrefixSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,61 +47,34 @@ public static string[] AppendArticlePrefix(string[] items)
public static string[] PrependArticleSuffix(string[] appended)
{
var inserted = new string[appended.Length];
var the = " the".AsSpan();
var an = " an".AsSpan();
var a = " a".AsSpan();

for (var i = 0; i < appended.Length; i++)
{
string suffix;
string original;
var append = appended[i];
if (append.EndsWith(EnglishArticles.The.ToString()))
var append = appended[i].AsSpan();
if (append.EndsWith(the, StringComparison.OrdinalIgnoreCase))
{
suffix = append.Substring(append.IndexOf(" The", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
inserted[i] = ToOriginalFormat(append, 3);
}
else if (append.EndsWith(EnglishArticles.A.ToString()))
else if (append.EndsWith(an, StringComparison.OrdinalIgnoreCase))
{
suffix = append.Substring(append.IndexOf(" A", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
inserted[i] = ToOriginalFormat(append, 2);
}
else if (append.EndsWith(EnglishArticles.An.ToString()))
else if (append.EndsWith(a, StringComparison.OrdinalIgnoreCase))
{
suffix = append.Substring(append.IndexOf(" An", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (append.EndsWith(EnglishArticles.A.ToString().ToLowerInvariant()))
{
suffix = append.Substring(append.IndexOf(" a", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (append.EndsWith(EnglishArticles.An.ToString().ToLowerInvariant()))
{
suffix = append.Substring(append.IndexOf(" an", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (append.EndsWith(EnglishArticles.The.ToString().ToLowerInvariant()))
{
suffix = append.Substring(append.IndexOf(" the", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
inserted[i] = ToOriginalFormat(append, 1);
}
else
{
inserted[i] = append;
inserted[i] = appended[i];
}
}
return inserted;
}

static string ToOriginalFormat(string[] appended, string suffix, int i)
{
var insertion = appended[i].Remove(appended[i].IndexOf(suffix, StringComparison.CurrentCulture));
var original = $"{suffix} {insertion}";
return original.Trim();
}
static string ToOriginalFormat(ReadOnlySpan<char> value, int suffixLength) =>
$"{value[^suffixLength..]} {value[..^(suffixLength + 1)]}";
}
}