diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index fe3802f859..d816892bd7 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -8591,6 +8591,12 @@ namespace System.Management.Automation return false; } + private static readonly SearchValues s_defaultCharsToCheck = SearchValues.Create("$`"); + private static readonly SearchValues s_escapeCharsToCheck = SearchValues.Create("$[]`"); + + private static bool ContainsCharsToCheck(ReadOnlySpan text, bool escape) + => text.ContainsAny(escape ? s_escapeCharsToCheck : s_defaultCharsToCheck); + private static bool CompletionRequiresQuotes(string completion, bool escape) { // If the tokenizer sees the completion as more than two tokens, or if there is some error, then @@ -8600,18 +8606,13 @@ namespace System.Management.Automation ParseError[] errors; Language.Parser.ParseInput(completion, out tokens, out errors); - char[] charToCheck = escape ? new[] { '$', '[', ']', '`' } : new[] { '$', '`' }; - // Expect no errors and 2 tokens (1 is for our completion, the other is eof) // Or if the completion is a keyword, we ignore the errors bool requireQuote = !(errors.Length == 0 && tokens.Length == 2); if ((!requireQuote && tokens[0] is StringToken) || (tokens.Length == 2 && (tokens[0].TokenFlags & TokenFlags.Keyword) != 0)) { - requireQuote = false; - var value = tokens[0].Text; - if (value.IndexOfAny(charToCheck) != -1) - requireQuote = true; + requireQuote = ContainsCharsToCheck(tokens[0].Text, escape); } return requireQuote;