Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main(string argument)
- {
- List<string> Arguments = ParseArgument(argument);
- }
- List<string> ParseArgument(string argument) {
- char QuoteType = ' ';
- bool InQuotes = false;
- var Output = new List<string>();
- bool MetaChar = false;
- StringBuilder CurrentWord = new StringBuilder();
- for (int i = 0; i < argument.Length; i++) {
- char CurrentChar = argument[i];
- if (CurrentChar == ' ') {
- if (!InQuotes && CurrentWord.Length > 0) {
- Output.Add(CurrentWord.ToString());
- CurrentWord = new StringBuilder();
- } else if (InQuotes) {
- CurrentWord.Append(CurrentChar);
- }
- } else if (CurrentChar == '"' || CurrentChar == '\'') {
- if (InQuotes) {
- if (MetaChar) {
- CurrentWord.Append(CurrentChar);
- } else {
- if (CurrentChar == QuoteType) {
- InQuotes = false;
- Output.Add(CurrentWord.ToString());
- CurrentWord = new StringBuilder();
- } else {
- CurrentWord.Append(CurrentChar);
- }
- }
- } else {
- InQuotes = true;
- QuoteType = CurrentChar;
- }
- } else if (CurrentChar == '\\') {
- if (MetaChar) {
- CurrentWord.Append(CurrentChar);
- } else {
- MetaChar = true;
- continue;
- }
- } else {
- CurrentWord.Append(CurrentChar);
- }
- MetaChar = false;
- }
- if (CurrentWord.Length > 0 && !InQuotes) {
- Output.Add(CurrentWord.ToString());
- }
- return Output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement