Skip to content

Commit

Permalink
Fix article sorting for words containing an article-like part inside (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik authored Feb 21, 2024
1 parent 4dc2b72 commit 7bd807d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
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)]}";
}
}

0 comments on commit 7bd807d

Please sign in to comment.