Browse Source

Ignore repeated whitespaces in commands, and support linebreaks and tabs

tags/docs-0.9
RogueException 9 years ago
parent
commit
c88a92f5e6
1 changed files with 19 additions and 12 deletions
  1. +19
    -12
      src/Discord.Net.Commands/CommandParser.cs

+ 19
- 12
src/Discord.Net.Commands/CommandParser.cs View File

@@ -4,7 +4,7 @@ namespace Discord.Commands
{ {
internal static class CommandParser internal static class CommandParser
{ {
private enum CommandParserPart
private enum ParserPart
{ {
None, None,
Parameter, Parameter,
@@ -32,7 +32,7 @@ namespace Discord.Commands
else if (currentChar == '\\') else if (currentChar == '\\')
isEscaped = true; isEscaped = true;


if ((!isEscaped && currentChar == ' ') || endPosition >= inputLength)
if ((!isEscaped && IsWhiteSpace(currentChar)) || endPosition >= inputLength)
{ {
int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition; int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition;
string temp = input.Substring(startPosition, length); string temp = input.Substring(startPosition, length);
@@ -55,11 +55,12 @@ namespace Discord.Commands
commands = map.GetCommands(); //Work our way backwards to find a command that matches our input commands = map.GetCommands(); //Work our way backwards to find a command that matches our input
return commands != null; return commands != null;
} }
private static bool IsWhiteSpace(char c) => c == ' ' || c == '\n' || c == '\r' || c == '\t';


//TODO: Check support for escaping //TODO: Check support for escaping
public static CommandErrorType? ParseArgs(string input, int startPos, Command command, out string[] args) public static CommandErrorType? ParseArgs(string input, int startPos, Command command, out string[] args)
{ {
CommandParserPart currentPart = CommandParserPart.None;
ParserPart currentPart = ParserPart.None;
int startPosition = startPos; int startPosition = startPos;
int endPosition = startPos; int endPosition = startPos;
int inputLength = input.Length; int inputLength = input.Length;
@@ -94,20 +95,26 @@ namespace Discord.Commands
else if (currentChar == '\\') else if (currentChar == '\\')
isEscaped = true; isEscaped = true;


if (endPosition == startPosition + 1 && IsWhiteSpace(currentChar)) //Has no text yet, and is another whitespace
{
startPosition = endPosition;
continue;
}

switch (currentPart) switch (currentPart)
{ {
case CommandParserPart.None:
case ParserPart.None:
if ((!isEscaped && currentChar == '\"')) if ((!isEscaped && currentChar == '\"'))
{ {
currentPart = CommandParserPart.DoubleQuotedParameter;
currentPart = ParserPart.DoubleQuotedParameter;
startPosition = endPosition; startPosition = endPosition;
} }
else if ((!isEscaped && currentChar == '\'')) else if ((!isEscaped && currentChar == '\''))
{ {
currentPart = CommandParserPart.QuotedParameter;
currentPart = ParserPart.QuotedParameter;
startPosition = endPosition; startPosition = endPosition;
} }
else if ((!isEscaped && currentChar == ' ') || endPosition >= inputLength)
else if ((!isEscaped && IsWhiteSpace(currentChar)) || endPosition >= inputLength)
{ {
int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition; int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition;
string temp = input.Substring(startPosition, length); string temp = input.Substring(startPosition, length);
@@ -115,28 +122,28 @@ namespace Discord.Commands
startPosition = endPosition; startPosition = endPosition;
else else
{ {
currentPart = CommandParserPart.None;
currentPart = ParserPart.None;
argList.Add(temp); argList.Add(temp);
startPosition = endPosition; startPosition = endPosition;
} }
} }
break; break;
case CommandParserPart.QuotedParameter:
case ParserPart.QuotedParameter:
if ((!isEscaped && currentChar == '\'')) if ((!isEscaped && currentChar == '\''))
{ {
string temp = input.Substring(startPosition, endPosition - startPosition - 1); string temp = input.Substring(startPosition, endPosition - startPosition - 1);
currentPart = CommandParserPart.None;
currentPart = ParserPart.None;
argList.Add(temp); argList.Add(temp);
startPosition = endPosition; startPosition = endPosition;
} }
else if (endPosition >= inputLength) else if (endPosition >= inputLength)
return CommandErrorType.InvalidInput; return CommandErrorType.InvalidInput;
break; break;
case CommandParserPart.DoubleQuotedParameter:
case ParserPart.DoubleQuotedParameter:
if ((!isEscaped && currentChar == '\"')) if ((!isEscaped && currentChar == '\"'))
{ {
string temp = input.Substring(startPosition, endPosition - startPosition - 1); string temp = input.Substring(startPosition, endPosition - startPosition - 1);
currentPart = CommandParserPart.None;
currentPart = ParserPart.None;
argList.Add(temp); argList.Add(temp);
startPosition = endPosition; startPosition = endPosition;
} }


Loading…
Cancel
Save