-
Notifications
You must be signed in to change notification settings - Fork 3
/
CaseConverter.cs
207 lines (123 loc) · 6.41 KB
/
CaseConverter.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Gsemac.Text {
public class CaseConverter :
ICaseConverter {
// Public members
public CaseConverter(StringCase casing) :
this(casing, CaseConversionOptions.Default) {
}
public CaseConverter(StringCase casing, CaseConversionOptions options) {
this.casing = casing;
this.options = options;
}
public string Convert(string input) {
return ToCase(input, casing, options);
}
public static string ToCase(string input, StringCase casing) {
return ToCase(input, casing, CaseConversionOptions.Default);
}
public static string ToCase(string input, StringCase casing, CaseConversionOptions options) {
if (string.IsNullOrEmpty(input))
return input;
switch (casing) {
case StringCase.Unchanged:
return input;
case StringCase.Lower:
return input.ToLowerInvariant();
case StringCase.Upper:
return input.ToUpperInvariant();
case StringCase.Proper:
return ToProper(input, options);
case StringCase.Sentence:
return ToSentence(input, options);
default:
throw new ArgumentOutOfRangeException(nameof(input));
}
}
public static string ToLower(string input) {
if (string.IsNullOrEmpty(input))
return input;
return input.ToLowerInvariant();
}
public static string ToProper(string input) {
return ToProper(input, CaseConversionOptions.Default);
}
public static string ToProper(string input, CaseConversionOptions options) {
if (string.IsNullOrEmpty(input))
return input;
// TextInfo.ToTitleCase preserves sequences of all-caps, assuming that the sequence represents an acronym.
// If we do not wish to preserve acronyms, we'll make the entire string lowercase first.
if (!options.HasFlag(CaseConversionOptions.PreserveAcronyms))
input = input.ToLowerInvariant();
string result = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input);
// Fix possessive S (e.g. "'s") because TextInfo.ToTitleCase will capitalize them (e.g. "John's" -> "John'S").
result = Regex.Replace(result, @"\b(['’])S\b", "$1s");
if (options.HasFlag(CaseConversionOptions.CapitalizeRomanNumerals))
result = CapitalizeRomanNumerals(result);
return result;
}
public static string ToSentence(string input) {
return ToSentence(input, CaseConversionOptions.Default, SentenceCaseOptions.Default);
}
public static string ToSentence(string input, CaseConversionOptions options) {
return ToSentence(input, options, SentenceCaseOptions.Default);
}
public static string ToSentence(string input, SentenceCaseOptions options) {
return ToSentence(input, CaseConversionOptions.Default, options);
}
public static string ToSentence(string input, CaseConversionOptions options, SentenceCaseOptions sentenceCasingOptions) {
if (string.IsNullOrEmpty(input))
return input;
if (!options.HasFlag(CaseConversionOptions.PreserveAcronyms))
input = input.ToLowerInvariant();
string result = input;
if (sentenceCasingOptions.HasFlag(SentenceCaseOptions.DetectMultipleSentences)) {
// Detect multiple sentences in the same string, and convert them all to sentence case.
string pattern = sentenceCasingOptions.HasFlag(SentenceCaseOptions.RequireWhitespaceAfterPunctuation) ?
@"(?:^|[\.!?]\s)\s*(\w)" :
@"(?:^|[\.!?])\s*(\w)";
result = Regex.Replace(input, pattern, m => m.Value.ToUpperInvariant());
}
else {
// Treat the entire string as a single sentence (only capitalize the first letter).
result = Regex.Replace(input, @"^\s*(\w)", m => m.Value.ToUpperInvariant());
}
if (options.HasFlag(CaseConversionOptions.CapitalizeRomanNumerals))
result = CapitalizeRomanNumerals(result);
return result;
}
public static string ToUpper(string input) {
if (string.IsNullOrEmpty(input))
return input;
return input.ToUpperInvariant();
}
public static StringCase DetectCase(string input) {
if (string.IsNullOrWhiteSpace(input))
return StringCase.Unchanged;
if (input.Equals(input.ToUpperInvariant()))
return StringCase.Upper;
if (input.Equals(input.ToLowerInvariant()))
return StringCase.Lower;
char[] firstCharsOfEachWord = Regex.Matches(input, @"\s*\b(.).+?\b").Cast<Match>().Select(m => m.Groups[1].Value.First()).ToArray();
// If the first letter of each word is uppercase, assume we have Proper Case.
if (firstCharsOfEachWord.All(c => !char.IsLower(c)))
return StringCase.Proper;
// If the first letter of the first word is uppercase, assume we have Sentence Case.
if (firstCharsOfEachWord.Any() && !char.IsLower(firstCharsOfEachWord.First()))
return StringCase.Sentence;
// We could not detect the case.
return StringCase.Unchanged;
}
// Private members
private readonly StringCase casing;
private readonly CaseConversionOptions options;
private static string CapitalizeRomanNumerals(string input) {
// Regex adapted from Regular Expressions Cookbook, 6.9. Roman Numerals, example "Modern Roman numerals, strict":
// https://www.oreilly.com/library/view/regular-expressions-cookbook/9780596802837/ch06s09.html
const string romanNumeralsPattern = @"\b(?=[MDCLXVI])M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})\b";
return Regex.Replace(input, romanNumeralsPattern, m => m.Value.ToUpperInvariant(), RegexOptions.IgnoreCase);
}
}
}