-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
367 lines (300 loc) · 11.4 KB
/
StringExtensions.cs
File metadata and controls
367 lines (300 loc) · 11.4 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using static System.String;
namespace NetDevPack.Utilities
{
public static class StringExtensions
{
private static char sensitive = '*';
private static char at = '@';
private static readonly Regex UrlizeRegex = new Regex(@"[^A-Za-z0-9_~]+", RegexOptions.Multiline | RegexOptions.Compiled);
private static readonly Regex EmailRegex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", RegexOptions.Compiled);
public static string UrlEncode(this string url)
{
return Uri.EscapeDataString(url);
}
public static bool NotEqual(this string original, string compareTo)
{
return !original.Equals(compareTo);
}
public static bool IsEmail(this string field)
{
// Return true if strIn is in valid e-mail format.
return field.IsPresent() && EmailRegex.IsMatch(field);
}
public static bool IsMissing(this string value)
{
return IsNullOrEmpty(value);
}
public static bool IsPresent(this string value)
{
return !IsNullOrWhiteSpace(value);
}
private static string UrlCombine(string path1, string path2)
{
path1 = path1.TrimEnd('/') + "/";
path2 = path2.TrimStart('/');
return Path.Combine(path1, path2)
.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
public static string UrlPathCombine(this string path1, params string[] path2)
{
path1 = path1.TrimEnd('/') + "/";
foreach (var s in path2)
{
path1 = UrlCombine(path1, s).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
return path1;
}
public static string AddSpacesToSentence(this string state)
{
var text = state.ToCharArray();
var chars = new char[text.Length + HowManyCapitalizedChars(text) - 1];
chars[0] = text[0];
int j = 1;
for (int i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]))
{
if (text[i - 1] != ' ' && !char.IsUpper(text[i - 1]) ||
(char.IsUpper(text[i - 1]) && i < text.Length - 1 && !char.IsUpper(text[i + 1])))
{
chars[j++] = ' ';
chars[j++] = text[i];
continue;
}
}
chars[j++] = text[i];
}
#if NETSTANDARD2_0
return new string(chars);
#else
return new string(chars.AsSpan());
#endif
}
private static int HowManyCapitalizedChars(char[] state)
{
var count = 0;
for (var i = 0; i < state.Length; i++)
{
if (char.IsUpper(state[i]))
count++;
}
return count;
}
public static bool HasTrailingSlash(this string url)
{
return url != null && url.EndsWith("/");
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1
public static string TruncateSensitiveInformation(this string part)
{
return part.AsSpan().TruncateSensitiveInformation();
}
/// <summary>
/// Replace everything to ***, except the first and last char
/// </summary>
/// <returns></returns>
public static string TruncateSensitiveInformation(this ReadOnlySpan<char> part)
{
var firstAndLastLetterBuffer = new char[2];
var firstAndLastLetter = new Span<char>(firstAndLastLetterBuffer);
if (part != Empty)
{
part[..1].CopyTo(firstAndLastLetter[..1]);
part[^1..].CopyTo(firstAndLastLetter[1..]);
return Create(part.Length, firstAndLastLetterBuffer, (span, s) =>
{
s.AsSpan(0, 1).CopyTo(span);
for (var i = 1; i < span.Length - 1; i++)
{
span[i] = sensitive;
}
s.AsSpan(s.Length - 1).CopyTo(span[^1..]);
});
}
else
{
return Empty;
}
}
#else
/// <summary>
/// Replace everything to ***, except the first and last char
/// </summary>
/// <returns></returns>
public static string TruncateSensitiveInformation(this string part)
{
if (part.IsPresent())
{
var truncatedString = new char[part.Length];
truncatedString[0] = part[0];
for (var i = 1; i < part.Length - 1; i++)
{
truncatedString[i] = sensitive;
}
truncatedString[part.Length - 1] = part[part.Length - 1];
return new string(truncatedString);
}
return string.Empty;
}
#endif
#if NET5_0_OR_GREATER || NETSTANDARD2_1
/// <summary>
/// Truncate e-mail for frontend exibition:
/// myemail@microsoft.com -> m*****@m***t.com
/// </summary>
/// <param name="email"></param>
public static string TruncateEmail(this string email)
{
var beforeAt = email.AsSpan(0, email.IndexOf(at)).TruncateSensitiveInformation().AsSpan();
var afterAt = email.AsSpan(email.IndexOf(at) + 1).TruncateSensitiveInformation().AsSpan();
var finalSpan = new Span<char>(new char[email.Length]);
beforeAt.CopyTo(finalSpan);
finalSpan[beforeAt.Length] = at;
afterAt.CopyTo(finalSpan.Slice(beforeAt.Length + 1));
return finalSpan.ToString();
}
#endif
public static string ToSha256(this string value)
{
using var crypt = SHA256.Create();
var hash = new StringBuilder();
byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(value));
foreach (byte theByte in crypto)
{
hash.Append(theByte.ToString("x2"));
}
return hash.ToString();
}
/// <summary>
/// Remove áóé etc.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string RemoveDiacritics(this string text)
{
var normalizedString = text.Normalize(NormalizationForm.FormD);
var finalText = new char[text.Length];
var lastIndex = 0;
foreach (var c in normalizedString)
{
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
finalText[lastIndex++] = c;
}
}
Array.Resize(ref finalText, lastIndex);
return new string(finalText).Normalize(NormalizationForm.FormC);
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1
/// <summary>
/// Turn string into URL dashed style.
/// E.g: My custom url title provided by user -> my-custom-url-title-provided-by-user
/// Useful to create Url paths and improve SEO.
/// </summary>
public static string Urlize(this string str)
{
var tituloEditado = str.Trim().ToLower().RemoveDiacritics();
tituloEditado = UrlizeRegex.Replace(tituloEditado, "-");
if (tituloEditado.StartsWith('-'))
tituloEditado = tituloEditado[1..];
if (tituloEditado.EndsWith('-'))
tituloEditado = tituloEditado[..^1];
return tituloEditado;
}
#else
/// <summary>
/// Turn string into URL dashed style.
/// E.g: My custom url title provided by user -> my-custom-url-title-provided-by-user
/// Useful to create Url paths and improve SEO.
/// </summary>
public static string Urlize(this string str)
{
var tituloEditado = str.Trim().ToLower().RemoveDiacritics();
tituloEditado = UrlizeRegex.Replace(tituloEditado, "-");
if (tituloEditado.StartsWith("-"))
tituloEditado = tituloEditado.Substring(1);
if (tituloEditado.EndsWith("-"))
tituloEditado = tituloEditado.Substring(0, tituloEditado.Length - 1);
return tituloEditado;
}
#endif
public static string OnlyNumbers(this string str)
{
var onlyNumbers = new char[str.Length];
var lastIndex = 0;
foreach (var c in str)
{
if (c < '0' || c > '9') continue;
onlyNumbers[lastIndex++] = c;
}
Array.Resize(ref onlyNumbers, lastIndex);
return new string(onlyNumbers);
}
public static string FromBase64ToString(this string str, Encoding enc = null)
{
return (enc ?? Encoding.Default).GetString(FromBase64(str));
}
public static byte[] FromBase64(this string str)
{
return Convert.FromBase64String(str);
}
public static string ToBase64(this string str, Encoding enc = null)
{
return ToBase64((enc ?? Encoding.Default).GetBytes(str));
}
public static string ToBase64(this byte[] data, Encoding enc = null)
{
return Convert.ToBase64String(data);
}
public static byte[] FromPlainHexDumpStyleToByteArray(this string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
private static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
/// <summary>
/// Equivalent to xxd -p
/// see: https://linux.die.net/man/1/xxd
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string ToPlainHexDumpStyle(this byte[] data)
{
return BitConverter.ToString(data).Replace("-", "").ToLower();
}
public static string Capitalize(this string value, bool isRestLower)
{
var spanChars = value.AsSpan();
var newSpan = new Span<char>(new char[value.Length]);
spanChars.CopyTo(newSpan);
if (isRestLower)
{
spanChars.ToLowerInvariant(newSpan);
}
newSpan[0] = char.ToUpper(spanChars[0]);
return newSpan.ToString();
}
}
}