Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. void Main(string argument)
  2. {
  3. List<string> Arguments = ParseArgument(argument);
  4. }
  5.  
  6. List<string> ParseArgument(string argument) {
  7. char QuoteType = ' ';
  8. bool InQuotes = false;
  9. var Output = new List<string>();
  10. bool MetaChar = false;
  11. StringBuilder CurrentWord = new StringBuilder();
  12.  
  13. for (int i = 0; i < argument.Length; i++) {
  14. char CurrentChar = argument[i];
  15. if (CurrentChar == ' ') {
  16. if (!InQuotes && CurrentWord.Length > 0) {
  17. Output.Add(CurrentWord.ToString());
  18. CurrentWord = new StringBuilder();
  19. } else if (InQuotes) {
  20. CurrentWord.Append(CurrentChar);
  21. }
  22. } else if (CurrentChar == '"' || CurrentChar == '\'') {
  23. if (InQuotes) {
  24. if (MetaChar) {
  25. CurrentWord.Append(CurrentChar);
  26. } else {
  27. if (CurrentChar == QuoteType) {
  28. InQuotes = false;
  29. Output.Add(CurrentWord.ToString());
  30. CurrentWord = new StringBuilder();
  31. } else {
  32. CurrentWord.Append(CurrentChar);
  33. }
  34. }
  35. } else {
  36. InQuotes = true;
  37. QuoteType = CurrentChar;
  38. }
  39. } else if (CurrentChar == '\\') {
  40. if (MetaChar) {
  41. CurrentWord.Append(CurrentChar);
  42. } else {
  43. MetaChar = true;
  44. continue;
  45. }
  46. } else {
  47. CurrentWord.Append(CurrentChar);
  48. }
  49. MetaChar = false;
  50. }
  51. if (CurrentWord.Length > 0 && !InQuotes) {
  52. Output.Add(CurrentWord.ToString());
  53. }
  54.  
  55. return Output;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement