-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTextUtility.Core.cs
More file actions
110 lines (90 loc) · 2.76 KB
/
TextUtility.Core.cs
File metadata and controls
110 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
#endregion
public static partial class TextUtility
{
#region Public Methods
/// <summary>
/// Gets the plural form of the specified word.
/// </summary>
/// <param name="word">The word to make plural.</param>
public static string MakePlural(string word) => Pluralizer.MakePlural(word);
#endregion
#region Private Types
private static class Pluralizer
{
#region Private Data Members
private static readonly Dictionary<string, string> SpecialCases = new(StringComparer.OrdinalIgnoreCase)
{
// These should all be in lowercase (for PreserveCase logic).
{ "deer", "deer" },
{ "tooth", "teeth" },
};
#endregion
#region Public Methods
// I don't want to pull in a whole other library for this. This code works fine for the cases I need it for,
// which are item types on a PC's file system. Here are some heavier weight alternatives:
// https://www.nuget.org/packages/Pluralize.NET.Core/
// https://stackoverflow.com/a/47410837
// http://users.monash.edu/~damian/papers/HTML/Plurals.html
// https://github.com/Microsoft/referencesource/blob/master/System.Data.Entity.Design/...
// ... System/Data/Entity/Design/PluralizationService/EnglishPluralizationService.cs
[SuppressMessage("Design", "MEN010:Avoid magic numbers", Justification = "The length values are clear in context.")]
[return: NotNullIfNotNull(nameof(word))]
public static string? MakePlural(string? word)
{
string? result = word;
if (word.IsNotEmpty())
{
if (SpecialCases.TryGetValue(word, out string? plural))
{
result = plural;
}
else if (IsSuffix(word, "ss", "ch"))
{
result = word + "es";
}
else if (IsSuffix(word, "rix", "dex"))
{
result = Prefix(word, -2) + "ices";
}
else if (IsSuffix(word, "man"))
{
result = Prefix(word, -2) + "en";
}
else if (IsSuffix(word, "ay", "ey", "iy", "oy", "uy"))
{
result = word + "s";
}
else if (IsSuffix(word, "y"))
{
result = Prefix(word, -1) + "ies";
}
else if (!IsSuffix(word, "s"))
{
result = word + "s";
}
else
{
result = word;
}
result = PreserveCase(word, result);
}
return result;
}
#endregion
#region Private Methods
private static string Prefix(string value, int count) => value.Substring(0, count < 0 ? (value.Length + count) : count);
private static bool IsSuffix(string value, params string[] options)
=> options.Any(option => value.EndsWith(option, StringComparison.OrdinalIgnoreCase));
#endregion
}
#endregion
}
}