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

make EnglishArticle regex static #1353

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
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;