Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 05e11cb

Browse files
committed
Add SafeVarRef
1 parent bf2696f commit 05e11cb

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,9 @@ public static bool EndsWithInvariant(this string str, string endsWith)
823823
return str.EndsWith(endsWith, PclExport.Instance.InvariantComparison);
824824
}
825825

826-
private static readonly Regex InvalidVarCharsRegex = new Regex(@"[^A-Za-z0-9]", PclExport.Instance.RegexOptions);
827-
private static readonly Regex SplitCamelCaseRegex = new Regex("([A-Z]|[0-9]+)", PclExport.Instance.RegexOptions);
826+
private static readonly Regex InvalidVarCharsRegex = new Regex(@"[^A-Za-z0-9_]", RegexOptions.Compiled);
827+
private static readonly Regex InvalidVarRefCharsRegex = new Regex(@"[^A-Za-z0-9._]", RegexOptions.Compiled);
828+
private static readonly Regex SplitCamelCaseRegex = new Regex("([A-Z]|[0-9]+)", RegexOptions.Compiled);
828829
private static readonly Regex HttpRegex = new Regex(@"^http://",
829830
PclExport.Instance.RegexOptions | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
830831

@@ -910,11 +911,11 @@ public static bool ContainsAny(this string text, params string[] testMatches)
910911
return false;
911912
}
912913

913-
public static string SafeVarName(this string text)
914-
{
915-
if (string.IsNullOrEmpty(text)) return null;
916-
return InvalidVarCharsRegex.Replace(text, "_");
917-
}
914+
public static string SafeVarName(this string text) => !string.IsNullOrEmpty(text)
915+
? InvalidVarCharsRegex.Replace(text, "_") : null;
916+
917+
public static string SafeVarRef(this string text) => !string.IsNullOrEmpty(text)
918+
? InvalidVarRefCharsRegex.Replace(text, "_") : null;
918919

919920
public static string Join(this List<string> items)
920921
{

0 commit comments

Comments
 (0)