Skip to content

Commit 3a65660

Browse files
committed
fix: sonar
1 parent fce61cb commit 3a65660

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/VirtoCommerce.Platform.Core/Extensions/StringExtensions.cs

+14-11
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88

99
namespace VirtoCommerce.Platform.Core.Common
1010
{
11-
public static class StringExtensions
11+
public static partial class StringExtensions
1212
{
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+
1622
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];
1824

1925
public static bool IsAbsoluteUrl(this string url)
2026
{
@@ -323,14 +329,11 @@ public static bool IsValidEmail(this string input)
323329

324330
public static string PascalToKebabCase(this string name)
325331
{
326-
if (name == null)
327-
{
328-
throw new ArgumentNullException(nameof(name));
329-
}
332+
ArgumentNullException.ThrowIfNull(name);
330333

331-
name = _regexIllegal.Replace(name, "_").TrimEnd('_');
334+
name = IllegalRegex().Replace(name, "_").TrimEnd('_');
332335
// 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();
334337
}
335338
}
336339
}

0 commit comments

Comments
 (0)