diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs
index 6a6f5d87e9..88a272d695 100644
--- a/src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs
+++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs
@@ -55,36 +55,70 @@ namespace System.Management.Automation
}
}
+ ///
+ /// Removes wrapping quotes from a string and returns the quote used, if present.
+ ///
+ ///
+ /// The string to process, potentially surrounded by single or double quotes.
+ /// This parameter is updated in-place to exclude the removed quotes.
+ ///
+ ///
+ /// The type of quote detected (single or double), or an empty string if no quote is found.
+ ///
+ ///
+ /// 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.
+ ///
internal static string HandleDoubleAndSingleQuote(ref string wordToComplete)
{
- string quote = string.Empty;
-
- if (!string.IsNullOrEmpty(wordToComplete) && (wordToComplete[0].IsSingleQuote() || wordToComplete[0].IsDoubleQuote()))
+ if (string.IsNullOrEmpty(wordToComplete))
{
- char frontQuote = wordToComplete[0];
- int length = wordToComplete.Length;
-
- if (length == 1)
- {
- wordToComplete = string.Empty;
- quote = frontQuote.IsSingleQuote() ? "'" : "\"";
- }
- else if (length > 1)
- {
- if ((wordToComplete[length - 1].IsDoubleQuote() && frontQuote.IsDoubleQuote()) || (wordToComplete[length - 1].IsSingleQuote() && frontQuote.IsSingleQuote()))
- {
- wordToComplete = wordToComplete.Substring(1, length - 2);
- quote = frontQuote.IsSingleQuote() ? "'" : "\"";
- }
- else if (!wordToComplete[length - 1].IsDoubleQuote() && !wordToComplete[length - 1].IsSingleQuote())
- {
- wordToComplete = wordToComplete.Substring(1);
- quote = frontQuote.IsSingleQuote() ? "'" : "\"";
- }
- }
+ return string.Empty;
}
- return quote;
+ char frontQuote = wordToComplete[0];
+ bool hasFrontSingleQuote = frontQuote.IsSingleQuote();
+ bool hasFrontDoubleQuote = frontQuote.IsDoubleQuote();
+
+ if (!hasFrontSingleQuote && !hasFrontDoubleQuote)
+ {
+ return string.Empty;
+ }
+
+ string quoteInUse = hasFrontSingleQuote ? "'" : "\"";
+
+ int length = wordToComplete.Length;
+ if (length == 1)
+ {
+ wordToComplete = string.Empty;
+ return quoteInUse;
+ }
+
+ char backQuote = wordToComplete[length - 1];
+ bool hasBackSingleQuote = backQuote.IsSingleQuote();
+ bool hasBackDoubleQuote = backQuote.IsDoubleQuote();
+
+ bool hasBothFrontAndBackQuotes =
+ (hasFrontSingleQuote && hasBackSingleQuote) || (hasFrontDoubleQuote && hasBackDoubleQuote);
+
+ if (hasBothFrontAndBackQuotes)
+ {
+ wordToComplete = wordToComplete.Substring(1, length - 2);
+ return quoteInUse;
+ }
+
+ bool hasFrontQuoteAndNoBackQuote =
+ (hasFrontSingleQuote || hasFrontDoubleQuote) && !hasBackSingleQuote && !hasBackDoubleQuote;
+
+ if (hasFrontQuoteAndNoBackQuote)
+ {
+ wordToComplete = wordToComplete.Substring(1);
+ return quoteInUse;
+ }
+
+ return string.Empty;
}
internal static bool CompletionRequiresQuotes(string completion, bool escape)