Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs (C#)

Code before:
        /// <param name="wordToComplete">The word or partial word to compare against the input string.</param>
        /// <returns>
        /// Returns <c>true</c> if the value matches the wordToComplete or the generated wildcard pattern; otherwise, <c>false</c>.
        /// </returns>
        /// <remarks>
        /// The method performs the following checks:
        /// 1. If the value contains escaped newline characters, the wordToComplete is normalized 
        ///    and unescaped, and a case-insensitive prefix match is performed.
        /// 2. If either the value or wordToComplete contains wildcard characters, a case-insensitive 
        ///    prefix match is performed. This is to protect against issues in WildcardPatternParser.Parse()
        ///    where strings like '[*' throw WildcardPatternException from wildcards not being escaped.
        /// 3. If neither of the above conditions apply, a wildcard pattern is generated from the 
        ///    wordToComplete, appending a wildcard character (*). The pattern is then used to match the value.
        /// </remarks>
        internal static bool IsMatch(string value, string wordToComplete)
        {
            if (ContainsEscapedNewlineString(value))
            {
                string normalizedWord = WildcardPattern.Unescape(wordToComplete.ReplaceLineEndings("`"));
                return value.StartsWith(normalizedWord, StringComparison.OrdinalIgnoreCase);
            }
            else if (WildcardPattern.ContainsWildcardCharacters(value) ||
                     WildcardPattern.ContainsWildcardCharacters(wordToComplete))
            {
                return value.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase);
            }

            return WildcardPattern
                .Get(wordToComplete + "*", WildcardOptions.IgnoreCase)
                .IsMatch(value);
        }

        /// <summary>
        /// Removes wrapping quotes from a string and returns the quote used, if present.
        /// </summary>
        /// <param name="wordToComplete">
        /// The string to process, potentially surrounded by single or double quotes.
        /// This parameter is updated in-place to exclude the removed quotes.
        /// </param>
        /// <returns>
        /// The type of quote detected (single or double), or an empty string if no quote is found.
        /// </returns>
        /// <remarks>
        /// This method checks for single or double quotes at the start and end of the string.
        /// If wrapping quotes are detected and match, both are removed; otherwise, only the front quote is removed.
        /// The string is updated in-place, and only matching front-and-back quotes are stripped.
        /// If no quotes are detected or the input is empty, the original string remains unchanged.
        /// </remarks>
        internal static string HandleDoubleAndSingleQuote(ref string wordToComplete)
        {
            if (string.IsNullOrEmpty(wordToComplete))

Review comment (security):
Do we really need to evaluate these both conditions?
```c#
string normalizedWord = WildcardPattern.Unescape(wordToComplete.ReplaceLineEndings("`"));
if (!value.StartsWith(normalizedWord, StringComparison.OrdinalIgnoreCase))
{
    return WildcardPattern
                .Get(wordToComplete + "*", WildcardOptions.IgnoreCase)
                .IsMatch(value);
}
```

Code after:
                    string listItemText = listItemTextMapping?.Invoke(value) ?? value;

                    yield return new CompletionResult(completionText, listItemText, resultType, toolTip);
                }
            }
        }

        /// <summary>
        /// Normalizes the input string to an expandable string format for PowerShell.
        /// </summary>
        /// <param name="value">The input string to be normalized.</param>
        /// <returns>The normalized string with special characters replaced by their PowerShell escape sequences.</returns>
        /// <remarks>
        /// This method replaces special characters in the input string with their PowerShell equivalent escape sequences:
        /// <list type="bullet">
        ///     <item><description>Replaces "\r" (carriage return) with "`r".</description></item>
        ///     <item><description>Replaces "\n" (newline) with "`n".</description></item>
        ///     <item><description>Replaces "\t" (tab) with "`t".</description></item>
        ///     <item><description>Replaces "\0" (null) with "`0".</description></item>
        ///     <item><description>Replaces "\a" (bell) with "`a".</description></item>
        ///     <item><description>Replaces "\b" (backspace) with "`b".</description></item>
        ///     <item><description>Replaces "\u001b" (escape character) with "`e".</description></item>
        ///     <item><description>Replaces "\f" (form feed) with "`f".</description></item>
        ///     <item><description>Replaces "\v" (vertical tab) with "`v".</description></item>
        /// </list>
        /// </remarks>
        internal static string NormalizeToExpandableString(string value)
            => value
                .Replace("\r", "`r")
                .Replace("\n", "`n")
                .Replace("\t", "`t")
                .Replace("\0", "`0")
                .Replace("\a", "`a")
                .Replace("\b", "`b")
                .Replace("\u001b", "`e")
                .Replace("\f", "`f")
                .Replace("\v", "`v");

        /// <summary>
        /// Defines a strategy for determining if a value matches a word or pattern.
        /// </summary>
        /// <param name="value">The input string to check for a match.</param>
        /// <param name="wordToComplete">The word or pattern to match against.</param>
        /// <returns>
        /// <c>true</c> if the value matches the specified word or pattern; otherwise, <c>false</c>.
        /// </returns>
        internal delegate bool MatchStrategy(string value, string wordToComplete);

        /// <summary>
        /// Determines if the given value matches the specified word using a literal, case-insensitive prefix match.
        /// </summary>