-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Refactor character validation to use char.IsAsciiLetter #26270
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
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 |
|---|---|---|
|
|
@@ -2023,11 +2023,14 @@ public static string Match(string text) | |
| for (int i = 0; i < text.Length; i++) | ||
| { | ||
| uint h = text[i]; | ||
| if (h >= 'A' && h <= 'Z') | ||
| if (h == '-') | ||
| { | ||
| } | ||
| else if (char.IsAsciiLetter((char)h)) | ||
| { | ||
| h |= 0x20; // ToLower | ||
| } | ||
| else if (!((h >= 'a' && h <= 'z') || h == '-')) | ||
| else | ||
| { | ||
| // If the character isn't in any of our patterns, | ||
| // don't bother hashing and reset the running length. | ||
|
|
@@ -2070,11 +2073,11 @@ public static string Match(string text) | |
| // This code can be used when adding a new pattern. | ||
| internal static uint HashNewPattern(string pattern) | ||
| { | ||
| char ToLower(char c) | ||
| uint ToLower(uint c) | ||
| { | ||
| if (c >= 'A' && c <= 'Z') | ||
| if (char.IsAsciiLetterUpper((char)c)) | ||
| { | ||
| c = (char) (c | 0x20); | ||
| c |= 0x20; | ||
|
Collaborator
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. Please don't change non-compiled code. |
||
| } | ||
|
|
||
| return c; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -732,18 +732,7 @@ protected override PSDriveInfo RemoveDrive(PSDriveInfo drive) | |
| /// <returns>True if the drive can be persisted or else false.</returns> | ||
| private static bool IsSupportedDriveForPersistence(PSDriveInfo drive) | ||
| { | ||
| bool isSupportedDriveForPersistence = false; | ||
| if (drive != null && !string.IsNullOrEmpty(drive.Name) && drive.Name.Length == 1) | ||
| { | ||
| char driveChar = Convert.ToChar(drive.Name, CultureInfo.InvariantCulture); | ||
|
|
||
| if (char.ToUpperInvariant(driveChar) >= 'A' && char.ToUpperInvariant(driveChar) <= 'Z') | ||
| { | ||
| isSupportedDriveForPersistence = true; | ||
| } | ||
| } | ||
|
|
||
| return isSupportedDriveForPersistence; | ||
| return drive is { Name.Length: 1 } && char.IsAsciiLetter(drive.Name[0]); | ||
|
Collaborator
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. The same about pattern. |
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -794,14 +794,6 @@ private static bool EndsWithPeriodOrSpace(string? path) | |
| return c == ' ' || c == '.'; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true if the given character is a valid drive letter | ||
| /// </summary> | ||
| private static bool IsValidDriveChar(char value) | ||
| { | ||
| return ((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')); | ||
| } | ||
|
|
||
| private static bool IsDevice(string path) | ||
| { | ||
| return IsExtended(path) | ||
|
|
@@ -859,7 +851,7 @@ private static bool IsPartiallyQualified(string path) | |
| && IsDirectorySeparator(path[2]) | ||
| // To match old behavior we'll check the drive character for validity as the path is technically | ||
| // not qualified if you don't have a valid drive. "=:\" is the "=" file's default data stream. | ||
| && IsValidDriveChar(path[0])); | ||
| && char.IsAsciiLetter(path[0])); | ||
|
Collaborator
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. Please keep old method name for readability but update implementation. |
||
| } | ||
| /// <summary> | ||
| /// True if the given character is a directory separator. | ||
|
|
||
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.
I'd prefer to avoid using patterns since we haven't nullability check enabled.
I guess something classic like
drive?.Name?.Length == 1would work here.