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
Remove static constants #18154
base: master
Are you sure you want to change the base?
Remove static constants #18154
Conversation
3c8bb42
to
1c39590
Compare
| @@ -1473,7 +1473,7 @@ private static CanDoPathLookupResult CanDoPathLookup(string possiblePath) | |||
| /// <summary> | |||
| /// The command name to search for. | |||
| /// </summary> | |||
| private string _commandName; | |||
| private readonly string _commandName; | |||
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.
It is requested by analyzer.
|
I suggest splitting |
c5dc0f3
to
05022a1
Compare
|
We could avoid some string allocations with the following |
| @@ -1263,7 +1263,7 @@ private static string GetFirstLineSubString(string stringToComplete, out bool ha | |||
| hasNewLine = false; | |||
| if (!string.IsNullOrEmpty(stringToComplete)) | |||
| { | |||
| var index = stringToComplete.IndexOfAny(Utils.Separators.CrLf); | |||
| var index = stringToComplete.AsSpan().IndexOfAny("\r\n"); | |||
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 is less readable than the existing code.
- You will need to reason that
"\r\n"will be cast toReadOnlySpanand then the chars are used instead of the string itself. Also, I don't think the saving is measurable. - It's helpful to keep all separators together in
Utils.Separators, for looking up and referencing.
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.
We can get better codegen using the char overloads:
| var index = stringToComplete.AsSpan().IndexOfAny("\r\n"); | |
| var index = stringToComplete.AsSpan().IndexOfAny('\r', '\n'); |
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.
Yes, we should use this overload.
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.
Done.
| @@ -140,7 +140,7 @@ public bool MoveNext() | |||
|
|
|||
| // For security reasons, if the command is coming from outside the runspace and it looks like a path, | |||
| // we want to pre-check that path before doing any probing of the network or drives | |||
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.IndexOfAny(Utils.Separators.DirectoryOrDrive) >= 0) | |||
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny("\\/:") >= 0) | |||
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.
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny("\\/:") >= 0) | |
| if (_commandOrigin == CommandOrigin.Runspace && _commandName.AsSpan().IndexOfAny('\\', '/', ':') >= 0) |
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.
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.
It is a question for .Net team why these options have so different codegen.
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.
One reason must be IndexOfAny("a-string") requires implicit casting from string to ReadOnlySpan<char>.
We should use the explicit overloads instead.
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.
Done.
I opened dotnet/runtime#76354
| @@ -1150,7 +1150,7 @@ private static CommandInfo TryModuleAutoLoading(string commandName, ExecutionCon | |||
| string moduleName; | |||
|
|
|||
| // Now we check if there exists the second '\' | |||
| var secondBackslash = moduleCommandName.IndexOfAny(Utils.Separators.Backslash); | |||
| var secondBackslash = moduleCommandName.IndexOf('\\'); | |||
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.
| var secondBackslash = moduleCommandName.IndexOf('\\'); | |
| int secondBackslash = moduleCommandName.IndexOf('\\'); |
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 PR already touches this line so we can fix code style too?
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.
There is an enormous number of var. It is better to fix in another PR if we would want.
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs
Outdated
Show resolved
Hide resolved
| { | ||
| wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion); |
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.
You point to removed line.
| @@ -1536,7 +1536,6 @@ private void setupPathSearcher() | |||
| if (_canDoPathLookupResult == CanDoPathLookupResult.Yes) | |||
| { | |||
| _canDoPathLookup = true; | |||
| _commandName = _commandName.TrimEnd(Utils.Separators.PathSearchTrimEnd); | |||
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.
Please revert changes related to TrimEnd since it was moved to #18166.
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.
Done.
This reverts commit d0eccf5.
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? |
| { | ||
| wordToComplete = WildcardPattern.Escape(wordToComplete, Utils.Separators.StarOrQuestion); | ||
| wordToComplete = WildcardPattern.Escape(wordToComplete, "[]"); |
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.
@iSazonov Why is Utils.Separators.StarOrQuestion replaced with "[]", not "*?"
| @@ -905,7 +905,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont | |||
| { | |||
| switch (completionContext.TokenBeforeCursor.Kind) | |||
| { | |||
|
|
|||
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.
Can we run dotnet-format on the repo to fix whitespace, so we don't see these changes in PRs?
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.
We can
|
I suggest changes to |


PR Summary
Please review commit by commit.
Historically PowerShell code contains a lot of static constants because of limitations in old .Net APIs. For example, now we have
string.Split(char)and can use it instead ofstring.Split(char[]).I had to refactor some methods.
Escape()method now is more short, simple, clear and fast - now we exactly point chars we want to escape.(for ex., now we don't remove trail spaces that are valid in file names)
PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).