-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Refactor Escape() method #18230
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
Refactor Escape() method #18230
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |||||
| using System.Collections.Generic; | ||||||
| using System.Diagnostics.Contracts; | ||||||
| using System.Globalization; | ||||||
| using System.Linq; | ||||||
| using System.Text; | ||||||
| using System.Text.RegularExpressions; | ||||||
| using System.Management.Automation.Internal; | ||||||
|
|
@@ -206,25 +205,20 @@ public bool IsMatch(string input) | |||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Escape special chars, except for those specified in <paramref name="charsNotToEscape"/>, in a string by replacing them with their escape codes. | ||||||
| /// Escape wildcard characters in a string by replacing them with their escape codes. | ||||||
| /// </summary> | ||||||
| /// <param name="pattern">The input string containing the text to convert.</param> | ||||||
| /// <param name="charsNotToEscape">Array of characters that not to escape.</param> | ||||||
| /// <param name="escapeOnlyBrackets">Escape all wildcard characters if false. Escape only brackets if true.</param> | ||||||
| /// <returns> | ||||||
| /// A string of characters with any metacharacters, except for those specified in <paramref name="charsNotToEscape"/>, converted to their escaped form. | ||||||
| /// A string of characters with any wildcard characters converted to their escaped form. | ||||||
| /// </returns> | ||||||
| internal static string Escape(string pattern, char[] charsNotToEscape) | ||||||
| internal static string Escape(string pattern, bool escapeOnlyBrackets) | ||||||
| { | ||||||
| if (pattern == null) | ||||||
| { | ||||||
| throw PSTraceSource.NewArgumentNullException(nameof(pattern)); | ||||||
| } | ||||||
|
|
||||||
| if (charsNotToEscape == null) | ||||||
| { | ||||||
| throw PSTraceSource.NewArgumentNullException(nameof(charsNotToEscape)); | ||||||
| } | ||||||
|
|
||||||
| if (pattern == string.Empty) | ||||||
| { | ||||||
| return pattern; | ||||||
|
|
@@ -233,33 +227,34 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||||
| 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++) | ||||||
| if (escapeOnlyBrackets) | ||||||
| { | ||||||
| char ch = pattern[i]; | ||||||
|
|
||||||
| // | ||||||
| // if it is a wildcard char, escape it | ||||||
| // | ||||||
| if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch)) | ||||||
| for (int i = 0; i < pattern.Length; i++) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| temp[tempIndex++] = escapeChar; | ||||||
| } | ||||||
|
|
||||||
| temp[tempIndex++] = ch; | ||||||
| } | ||||||
|
|
||||||
| string s = null; | ||||||
| char ch = pattern[i]; | ||||||
| if (ch == '[' || ch == ']') | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xtqqczze The PR is only for removing Utils.Separators.StarOrQuestion. No plans for style changes. |
||||||
| { | ||||||
| temp[tempIndex++] = escapeChar; | ||||||
| } | ||||||
|
|
||||||
| if (tempIndex == pattern.Length) | ||||||
| { | ||||||
| s = pattern; | ||||||
| temp[tempIndex++] = ch; | ||||||
| } | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| s = new string(temp.Slice(0, tempIndex)); | ||||||
| for (int i = 0; i < pattern.Length; i++) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| char ch = pattern[i]; | ||||||
| if (IsWildcardChar(ch)) | ||||||
| { | ||||||
| temp[tempIndex++] = escapeChar; | ||||||
| } | ||||||
|
|
||||||
| temp[tempIndex++] = ch; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return s; | ||||||
| return tempIndex == pattern.Length ? pattern : new string(temp.Slice(0, tempIndex)); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
|
|
@@ -271,7 +266,7 @@ internal static string Escape(string pattern, char[] charsNotToEscape) | |||||
| /// </returns> | ||||||
| public static string Escape(string pattern) | ||||||
| { | ||||||
| return Escape(pattern, Array.Empty<char>()); | ||||||
| return Escape(pattern, escapeOnlyBrackets: false); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a existing bug, the condition should probably have been
pattern.Length * 2 < StackAllocThreshold.I suggest refactoring to the recommended pattern (and in
Unescapetoo):