-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Reduce allocations in Escape() and Unescape() #10041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6cc75c8
4d7c316
ee3a005
68c349a
b409065
b4917fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -53,6 +53,10 @@ public sealed class WildcardPattern | |||
| // char that escapes special chars | ||||
iSazonov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| private const char escapeChar = '`'; | ||||
|
|
||||
| // Threshold for stack allocation. | ||||
| // The size is less than MaxShortPath = 260. | ||||
| private const int StackAllocThreshold = 256; | ||||
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
|
|
||||
| // we convert a wildcard pattern to a predicate | ||||
| private Predicate<string> _isMatch; | ||||
|
|
||||
|
|
@@ -203,15 +207,20 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||
| { | ||||
| if (pattern == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("pattern"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(pattern)); | ||||
| } | ||||
|
|
||||
| if (charsNotToEscape == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("charsNotToEscape"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(charsNotToEscape)); | ||||
| } | ||||
|
|
||||
| if (pattern == string.Empty) | ||||
| { | ||||
| return pattern; | ||||
| } | ||||
|
|
||||
| char[] temp = new char[pattern.Length * 2 + 1]; | ||||
| Span<char> temp = pattern.Length < StackAllocThreshold ? stackalloc char[pattern.Length * 2 + 1] : new char[pattern.Length * 2 + 1]; | ||||
|
||||
| int tempIndex = 0; | ||||
|
|
||||
| for (int i = 0; i < pattern.Length; i++) | ||||
|
|
@@ -231,13 +240,13 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||
|
|
||||
| string s = null; | ||||
|
|
||||
| if (tempIndex > 0) | ||||
| if (tempIndex == pattern.Length) | ||||
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| { | ||||
| s = new string(temp, 0, tempIndex); | ||||
| s = pattern; | ||||
| } | ||||
| else | ||||
| { | ||||
| s = string.Empty; | ||||
| s = new string(temp.Slice(0, tempIndex)); | ||||
| } | ||||
|
|
||||
| return s; | ||||
|
|
@@ -312,10 +321,16 @@ public static string Unescape(string pattern) | |||
| { | ||||
| if (pattern == null) | ||||
| { | ||||
| throw PSTraceSource.NewArgumentNullException("pattern"); | ||||
| throw PSTraceSource.NewArgumentNullException(nameof(pattern)); | ||||
| } | ||||
|
|
||||
| char[] temp = new char[pattern.Length]; | ||||
| if (pattern == string.Empty) | ||||
| { | ||||
| return pattern; | ||||
| } | ||||
|
|
||||
| Span<char> temp = pattern.Length < StackAllocThreshold ? stackalloc char[pattern.Length] : new char[pattern.Length]; | ||||
|
|
||||
| int tempIndex = 0; | ||||
| bool prevCharWasEscapeChar = false; | ||||
|
|
||||
|
|
@@ -361,13 +376,13 @@ public static string Unescape(string pattern) | |||
|
|
||||
| string s = null; | ||||
|
|
||||
| if (tempIndex > 0) | ||||
| if (tempIndex == pattern.Length) | ||||
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| { | ||||
| s = new string(temp, 0, tempIndex); | ||||
| s = pattern; | ||||
|
||||
| /// for a more cases see the unit-test file RegexTest.cs |
Perhaps MSFT team can port the Unit tests.
Uh oh!
There was an error while loading. Please reload this page.