Skip to content

Commit

Permalink
make EnglishArticle regex static (#1353)
Browse files Browse the repository at this point in the history
SimonCropp authored Feb 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent dd84ad9 commit eba5bca
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Humanizer/ArticlePrefixSort.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ namespace Humanizer
/// </summary>
public static class EnglishArticle
{
static Regex _regex = new("^((The)|(the)|(a)|(A)|(An)|(an))\\s\\w+");

/// <summary>
/// Removes the prefixed article and appends it to the same string.
/// </summary>
@@ -18,22 +20,21 @@ public static string[] AppendArticlePrefix(string[] items)
{
throw new ArgumentOutOfRangeException(nameof(items));
}

var regex = new Regex("^((The)|(the)|(a)|(A)|(An)|(an))\\s\\w+");
var transformed = new string[items.Length];

for (var i = 0; i < items.Length; i++)
{
if (regex.IsMatch(items[i]))
var item = items[i];
if (_regex.IsMatch(item))
{
var article = items[i].Substring(0, items[i].IndexOf(" ", StringComparison.CurrentCulture));
var removed = items[i].Remove(0, items[i].IndexOf(" ", StringComparison.CurrentCulture));
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();
}
else
{
transformed[i] = items[i].Trim();
transformed[i] = item.Trim();
}
}
Array.Sort(transformed);
@@ -53,45 +54,46 @@ public static string[] PrependArticleSuffix(string[] appended)
{
string suffix;
string original;
if (appended[i].EndsWith(EnglishArticles.The.ToString()))
var append = appended[i];
if (append.EndsWith(EnglishArticles.The.ToString()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" The", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" The", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (appended[i].EndsWith(EnglishArticles.A.ToString()))
else if (append.EndsWith(EnglishArticles.A.ToString()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" A", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" A", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (appended[i].EndsWith(EnglishArticles.An.ToString()))
else if (append.EndsWith(EnglishArticles.An.ToString()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" An", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" An", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (appended[i].EndsWith(EnglishArticles.A.ToString().ToLowerInvariant()))
else if (append.EndsWith(EnglishArticles.A.ToString().ToLowerInvariant()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" a", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" a", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (appended[i].EndsWith(EnglishArticles.An.ToString().ToLowerInvariant()))
else if (append.EndsWith(EnglishArticles.An.ToString().ToLowerInvariant()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" an", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" an", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else if (appended[i].EndsWith(EnglishArticles.The.ToString().ToLowerInvariant()))
else if (append.EndsWith(EnglishArticles.The.ToString().ToLowerInvariant()))
{
suffix = appended[i].Substring(appended[i].IndexOf(" the", StringComparison.CurrentCulture));
suffix = append.Substring(append.IndexOf(" the", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
}
else
{
inserted[i] = appended[i];
inserted[i] = append;
}
}
return inserted;

0 comments on commit eba5bca

Please sign in to comment.