From b13ef0a046f7642a4c2ade6fb8220fe2aad13d9d Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Wed, 21 Feb 2024 14:17:32 +1000 Subject: [PATCH 1/2] Fix article sorting for words containing a suffix in a middle --- .../ArticlePrefixSortTests.cs | 1 + src/Humanizer/ArticlePrefixSort.cs | 57 ++++++------------- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs index ffb973e4a..bca7cc7c3 100644 --- a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs +++ b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs @@ -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))); diff --git a/src/Humanizer/ArticlePrefixSort.cs b/src/Humanizer/ArticlePrefixSort.cs index e92fd8e33..7843bc063 100644 --- a/src/Humanizer/ArticlePrefixSort.cs +++ b/src/Humanizer/ArticlePrefixSort.cs @@ -1,4 +1,6 @@ -namespace Humanizer +using System.Diagnostics; + +namespace Humanizer { /// /// Contains methods for removing, appending and prepending article prefixes for sorting strings ignoring the article. @@ -47,61 +49,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())) - { - suffix = append.Substring(append.IndexOf(" The", StringComparison.CurrentCulture)); - original = ToOriginalFormat(appended, suffix, i); - inserted[i] = original; - } - else if (append.EndsWith(EnglishArticles.A.ToString())) - { - suffix = append.Substring(append.IndexOf(" A", StringComparison.CurrentCulture)); - original = ToOriginalFormat(appended, suffix, i); - inserted[i] = original; - } - else if (append.EndsWith(EnglishArticles.An.ToString())) + var append = appended[i].AsSpan(); + if (append.EndsWith(the, StringComparison.OrdinalIgnoreCase)) { - suffix = append.Substring(append.IndexOf(" An", StringComparison.CurrentCulture)); - original = ToOriginalFormat(appended, suffix, i); - inserted[i] = original; + inserted[i] = ToOriginalFormat(append, 3); } - else if (append.EndsWith(EnglishArticles.A.ToString().ToLowerInvariant())) + 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().ToLowerInvariant())) + 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.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 value, int suffixLength) => + $"{value[^suffixLength..]} {value[..^(suffixLength + 1)]}"; } } From 4c0ad84ad3a5548fb44c3f2c89bf408b60780246 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Wed, 21 Feb 2024 14:19:48 +1000 Subject: [PATCH 2/2] remove unused using --- src/Humanizer/ArticlePrefixSort.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Humanizer/ArticlePrefixSort.cs b/src/Humanizer/ArticlePrefixSort.cs index 7843bc063..9075f37a3 100644 --- a/src/Humanizer/ArticlePrefixSort.cs +++ b/src/Humanizer/ArticlePrefixSort.cs @@ -1,6 +1,4 @@ -using System.Diagnostics; - -namespace Humanizer +namespace Humanizer { /// /// Contains methods for removing, appending and prepending article prefixes for sorting strings ignoring the article.