Skip to content
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

Fix #1385 #1390

Merged
merged 2 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,25 @@ public void It_handles_multiple_options_with_quoted_arguments()
destination,
"--verbose");
}

[Fact]
public void Internal_quotes_do_not_cause_string_to_be_split()
{
var commandLine = @"POST --raw='{""Id"":1,""Name"":""Alice""}'";

_splitter.Split(commandLine)
.Should()
.BeEquivalentTo("POST", "--raw='{Id:1,Name:Alice}'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. The double quotes should be preserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimal Changes have been done just to fix the mentioned issue and not to modify any existing logic.
It was preexisting logic to stripe away all the double-quotes. I tried to preserve it but it was conflicting with other test cases.

}

[Fact]
public void Internal_whitespaces_are_preserved_and_do_not_cause_string_to_be_split()
{
var commandLine = @"command --raw='{""Id"":1,""Movie Name"":""The Three Musketeers""}'";

_splitter.Split(commandLine)
.Should()
.BeEquivalentTo("command", "--raw='{Id:1,Movie Name:The Three Musketeers}'");
}
}
}
98 changes: 44 additions & 54 deletions src/System.CommandLine/Parsing/CommandLineStringSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private enum Boundary
{
TokenStart,
WordEnd,
QuoteStart,
QuoteEnd
}

Expand All @@ -25,52 +26,63 @@ public IEnumerable<string> Split(string commandLine)
var pos = 0;

var seeking = Boundary.TokenStart;
int? skipQuoteAtIndex = null;
var seekingQuote = Boundary.QuoteStart;

while (pos < memory.Length)
{
var c = memory.Span[pos];

if (char.IsWhiteSpace(c))
{
switch (seeking)
if (seekingQuote == Boundary.QuoteStart)
{
case Boundary.WordEnd:
yield return CurrentToken();
startTokenIndex = pos;
seeking = Boundary.TokenStart;
break;

case Boundary.TokenStart:
startTokenIndex = pos;
break;

case Boundary.QuoteEnd:
break;
switch (seeking)
{
case Boundary.WordEnd:
yield return CurrentToken();
startTokenIndex = pos;
seeking = Boundary.TokenStart;
break;

case Boundary.TokenStart:
startTokenIndex = pos;
break;
}
}
}
else if (c == '\"')
{
switch (seeking)
if (seeking == Boundary.TokenStart)
{
case Boundary.QuoteEnd:
yield return CurrentToken();
startTokenIndex = pos;
seeking = Boundary.TokenStart;
break;

case Boundary.TokenStart:
startTokenIndex = pos + 1;
seeking = Boundary.QuoteEnd;
break;

case Boundary.WordEnd:
seeking = Boundary.QuoteEnd;
skipQuoteAtIndex = pos;
break;
switch (seekingQuote)
{
case Boundary.QuoteEnd:
yield return CurrentToken();
startTokenIndex = pos;
seekingQuote = Boundary.QuoteStart;
break;

case Boundary.QuoteStart:
startTokenIndex = pos + 1;
seekingQuote = Boundary.QuoteEnd;
break;
}
}
else
{
switch (seekingQuote)
{
case Boundary.QuoteEnd:
seekingQuote = Boundary.QuoteStart;
break;

case Boundary.QuoteStart:
seekingQuote = Boundary.QuoteEnd;
break;
}
}
}
else if (seeking == Boundary.TokenStart)
else if (seeking == Boundary.TokenStart && seekingQuote == Boundary.QuoteStart)
{
seeking = Boundary.WordEnd;
startTokenIndex = pos;
Expand All @@ -95,29 +107,7 @@ public IEnumerable<string> Split(string commandLine)

string CurrentToken()
{
if (skipQuoteAtIndex is null)
{
return memory.Slice(
startTokenIndex,
IndexOfEndOfToken())
.ToString();
}
else
{
var beforeQuote = memory.Slice(
startTokenIndex,
skipQuoteAtIndex.Value - startTokenIndex);

var indexOfCharAfterQuote = skipQuoteAtIndex.Value + 1;

var afterQuote = memory.Slice(
indexOfCharAfterQuote,
pos - skipQuoteAtIndex.Value - 1);

skipQuoteAtIndex = null;

return $"{beforeQuote}{afterQuote}";
}
return memory.Slice(startTokenIndex, IndexOfEndOfToken()).ToString().Replace("\"", "");
}

int IndexOfEndOfToken() => pos - startTokenIndex;
Expand Down