generated from atc-net/atc-template-dotnet-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
331 lines (283 loc) · 8.62 KB
/
StringExtensions.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
namespace Atc.Wpf.SourceGenerators.Extensions;
[SuppressMessage("Design", "CA1308:Teplace the call to 'ToLowerInvariant' with 'ToUpperInvariant'", Justification = "OK.")]
public static class StringExtensions
{
private static readonly Regex NameofRegex = new(
@"^nameof\((?<name>\w+)\)$",
RegexOptions.Compiled | RegexOptions.ExplicitCapture,
TimeSpan.FromMilliseconds(100));
private static readonly Dictionary<string, string> TypeAliases = new(StringComparer.Ordinal)
{
{ nameof(Boolean), "bool" },
{ nameof(Byte), "byte" },
{ nameof(SByte), "sbyte" },
{ nameof(Char), "char" },
{ nameof(Decimal), "decimal" },
{ nameof(Double), "double" },
{ nameof(Single), "float" },
{ nameof(Int32), "int" },
{ nameof(UInt32), "uint" },
{ nameof(Int16), "short" },
{ nameof(UInt16), "ushort" },
{ nameof(Int64), "long" },
{ nameof(UInt64), "ulong" },
{ nameof(Object), "object" },
{ nameof(String), "string" },
};
public static string EnsureNameofContent(
this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (!value.StartsWith("nameof(", StringComparison.Ordinal) ||
!value.EndsWith(")", StringComparison.Ordinal))
{
return $"nameof({value})";
}
if (value.StartsWith("nameof(nameof(", StringComparison.Ordinal))
{
value = value
.Replace("nameof(nameof(", "nameof(")
.Replace("))", ")");
}
return value;
}
public static string EnsureFirstCharacterToUpper(
this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
return value.Length switch
{
0 => value,
1 => value.ToUpperInvariant(),
_ => char.ToUpperInvariant(value[0]) + value.Substring(1),
};
}
public static string EnsureFirstCharacterToLower(
this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
return value.Length switch
{
0 => value,
1 => value.ToLowerInvariant(),
_ => char.ToLowerInvariant(value[0]) + value.Substring(1),
};
}
public static string ExtractInnerContent(
this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
var start = value.IndexOf('(');
if (start == -1)
{
return value;
}
var end = value.LastIndexOf(')');
return end == -1
? value
: value.Substring(start + 1, end - start - 1);
}
public static Dictionary<string, string?> ExtractAttributeConstructorParameters(
this string value)
{
var result = new Dictionary<string, string?>(StringComparer.Ordinal);
if (string.IsNullOrEmpty(value))
{
return result;
}
var content = RemoveOuterBrackets(value);
var parameters = ExtractParameterString(content);
if (string.IsNullOrEmpty(parameters))
{
return result;
}
var arguments = SplitArguments(parameters);
ProcessArguments(arguments, result);
return result;
}
public static string StripPrefixFromField(
this string fieldName)
{
if (fieldName is null)
{
throw new ArgumentNullException(nameof(fieldName));
}
if (fieldName.StartsWith("m_", StringComparison.Ordinal))
{
fieldName = fieldName.Substring(2);
}
else if (fieldName.StartsWith("_", StringComparison.Ordinal))
{
fieldName = fieldName.Substring(1);
}
return fieldName;
}
public static string EnsureCSharpAliasIfNeeded(
this string typeName)
=> TypeAliases.TryGetValue(typeName, out var alias)
? alias
: typeName;
public static bool IsSimpleType(
this string value)
=> value
is "bool"
or "decimal"
or "double"
or "float"
or "int"
or "long"
or "string";
private static string RemoveOuterBrackets(
string value)
{
if (value.StartsWith("[", StringComparison.Ordinal) &&
value.EndsWith("]", StringComparison.Ordinal))
{
return value.Substring(1, value.Length - 2);
}
return value;
}
private static string ExtractParameterString(
string value)
{
var startIndex = value.IndexOf('(');
if (startIndex < 0)
{
return string.Empty;
}
var count = 0;
var endIndex = -1;
for (var i = startIndex; i < value.Length; i++)
{
if (value[i] == '(')
{
count++;
}
else if (value[i] == ')')
{
count--;
if (count != 0)
{
continue;
}
endIndex = i;
break;
}
}
return endIndex < 0
? string.Empty
: value.Substring(startIndex + 1, endIndex - startIndex - 1);
}
private static void ProcessArguments(
IEnumerable<string> arguments,
Dictionary<string, string?> result)
{
var unnamedIndex = 0;
foreach (var arg in arguments)
{
var trimmedArg = arg.Trim();
var eqIndex = trimmedArg.IndexOf('=');
if (eqIndex > 0)
{
var key = trimmedArg.Substring(0, eqIndex).Trim();
var value = trimmedArg.Substring(eqIndex + 1).Trim();
value = StripQuotes(value);
if (value.StartsWith("[", StringComparison.Ordinal) &&
value.EndsWith("]", StringComparison.Ordinal))
{
value = value
.Substring(1, value.Length - 2)
.Trim();
}
result[key] = string.IsNullOrEmpty(value) ? string.Empty : value;
}
else
{
var key = unnamedIndex == 0 ? "Name" : unnamedIndex.ToString(CultureInfo.InvariantCulture);
var value = StripQuotes(trimmedArg);
var match = NameofRegex.Match(value);
if (match.Success)
{
value = match.Groups["name"].Value;
}
result[key] = string.IsNullOrEmpty(value)
? string.Empty
: value;
unnamedIndex++;
}
}
}
private static List<string> SplitArguments(
string value)
{
var args = new List<string>();
var currentArg = new StringBuilder();
var bracketCount = 0;
var inQuotes = false;
var quoteChar = '\0';
foreach (var c in value)
{
if (inQuotes)
{
currentArg.Append(c);
if (c == quoteChar)
{
inQuotes = false;
}
continue;
}
if (c is '"' or '\'')
{
inQuotes = true;
quoteChar = c;
currentArg.Append(c);
continue;
}
switch (c)
{
case '[':
bracketCount++;
break;
case ']':
bracketCount--;
break;
}
if (c == ',' && bracketCount == 0 && !inQuotes)
{
args.Add(currentArg.ToString().Trim());
currentArg.Clear();
}
else
{
currentArg.Append(c);
}
}
if (currentArg.Length > 0)
{
args.Add(currentArg.ToString().Trim());
}
return args;
}
private static string StripQuotes(
string value)
{
if ((value.StartsWith("\"", StringComparison.Ordinal) && value.EndsWith("\"", StringComparison.Ordinal)) ||
(value.StartsWith("'", StringComparison.Ordinal) && value.EndsWith("'", StringComparison.Ordinal)))
{
return value.Substring(1, value.Length - 2);
}
return value;
}
}