/// <summary> /// Implement the same logic as found at /// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx /// The 3 special characters are quote, backslash and whitespaces, in order /// of priority. /// The semantics of a quote is: whatever the state of the lexer, copy /// all characters verbatim until the next quote or EOF. /// The semantics of backslash is: If the next character is a backslash or a quote, /// copy the next character. Otherwise, copy the backslash and the next character. /// The semantics of whitespace is: end the current argument and move on to the next one. /// </summary> private IEnumerable<string> SplitArgs(string commandLine) { var state = new State(commandLine); while (!state.EOF) { switch (state.Current) { case '"': ProcessQuote(state); break; case '\\': ProcessBackslash(state); break; case ' ': case '\t': if (state.StringBuilder.Length > 0) state.AddArgument(); state.MoveNext(); break; default: state.AppendCurrent(); state.MoveNext(); break; } } if (state.StringBuilder.Length > 0) state.AddArgument(); return state.Arguments; }
private void ProcessQuote(State state) { state.MoveNext(); while (!state.EOF) { if (state.Current == '"') { state.MoveNext(); break; } state.AppendCurrent(); state.MoveNext(); } state.AddArgument(); }
/// <summary> /// Implement the same logic as found at /// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx /// The 3 special characters are quote, backslash and whitespaces, in order /// of priority. /// The semantics of a quote is: whatever the state of the lexer, copy /// all characters verbatim until the next quote or EOF. /// The semantics of backslash is: If the next character is a backslash or a quote, /// copy the next character. Otherwise, copy the backslash and the next character. /// The semantics of whitespace is: end the current argument and move on to the next one. /// </summary> private IEnumerable <string> SplitArgs(string commandLine) { var state = new State(commandLine); while (!state.EOF) { switch (state.Current) { case '"': ProcessQuote(state); break; case '\\': ProcessBackslash(state); break; case ' ': case '\t': if (state.StringBuilder.Length > 0) { state.AddArgument(); } state.MoveNext(); break; default: state.AppendCurrent(); state.MoveNext(); break; } } if (state.StringBuilder.Length > 0) { state.AddArgument(); } return(state.Arguments); }