Replace char[] array in CompletionRequiresQuotes with cached SearchValues<char> (#24907)

This commit is contained in:
Armaan Mcleod
2025-02-01 21:17:42 +05:00
committed by GitHub
parent 459fc8d285
commit ed982b4338
@@ -8591,6 +8591,12 @@ namespace System.Management.Automation
return false;
}
private static readonly SearchValues<char> s_defaultCharsToCheck = SearchValues.Create("$`");
private static readonly SearchValues<char> s_escapeCharsToCheck = SearchValues.Create("$[]`");
private static bool ContainsCharsToCheck(ReadOnlySpan<char> 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;