|
8 | 8 |
|
9 | 9 | namespace VirtoCommerce.Platform.Core.Common
|
10 | 10 | {
|
11 |
| - public static class StringExtensions |
| 11 | + public static partial class StringExtensions |
12 | 12 | {
|
13 |
| - private static readonly Regex _regex1 = new Regex(@"([A-Z]+)([A-Z][a-z])", RegexOptions.Compiled); |
14 |
| - private static readonly Regex _regex2 = new Regex(@"([a-z\d])([A-Z])", RegexOptions.Compiled); |
15 |
| - private static readonly Regex _regexIllegal = new Regex(@"[\[, \]]", RegexOptions.Compiled); |
| 13 | + [GeneratedRegex(@"([A-Z]+)([A-Z][a-z])")] |
| 14 | + private static partial Regex FirstUpperCaseRegex(); |
| 15 | + |
| 16 | + [GeneratedRegex(@"([a-z\d])([A-Z])")] |
| 17 | + private static partial Regex FirstLowerCaseRegex(); |
| 18 | + |
| 19 | + [GeneratedRegex(@"[\[, \]]")] |
| 20 | + private static partial Regex IllegalRegex(); |
| 21 | + |
16 | 22 | private static readonly Regex _emailRegex = new Regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-||_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+([a-z]+|\d|-|\.{0,1}|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])?([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
|
17 |
| - private static readonly string[] _allowedUriSchemes = new string[] { Uri.UriSchemeFile, Uri.UriSchemeFtp, Uri.UriSchemeHttp, Uri.UriSchemeHttps, Uri.UriSchemeMailto, Uri.UriSchemeNetPipe, Uri.UriSchemeNetTcp }; |
| 23 | + private static readonly string[] _allowedUriSchemes = [Uri.UriSchemeFile, Uri.UriSchemeFtp, Uri.UriSchemeHttp, Uri.UriSchemeHttps, Uri.UriSchemeMailto, Uri.UriSchemeNetPipe, Uri.UriSchemeNetTcp]; |
18 | 24 |
|
19 | 25 | public static bool IsAbsoluteUrl(this string url)
|
20 | 26 | {
|
@@ -323,14 +329,11 @@ public static bool IsValidEmail(this string input)
|
323 | 329 |
|
324 | 330 | public static string PascalToKebabCase(this string name)
|
325 | 331 | {
|
326 |
| - if (name == null) |
327 |
| - { |
328 |
| - throw new ArgumentNullException(nameof(name)); |
329 |
| - } |
| 332 | + ArgumentNullException.ThrowIfNull(name); |
330 | 333 |
|
331 |
| - name = _regexIllegal.Replace(name, "_").TrimEnd('_'); |
| 334 | + name = IllegalRegex().Replace(name, "_").TrimEnd('_'); |
332 | 335 | // Replace any capital letters, apart from the first character, with _x, the same way Ruby does
|
333 |
| - return _regex2.Replace(_regex1.Replace(name, "$1_$2"), "$1_$2").ToLower(); |
| 336 | + return FirstLowerCaseRegex().Replace(FirstUpperCaseRegex().Replace(name, "$1_$2"), "$1_$2").ToLower(); |
334 | 337 | }
|
335 | 338 | }
|
336 | 339 | }
|
0 commit comments