Advertisement
fosterboy123

A fantastic waste of time

Feb 25th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1.  public static string[] GetArgs(string received) {
  2.             // Check that the incoming string is not empty
  3.             if (received == string.Empty)
  4.                 return null;
  5.  
  6.             // Check to ensure the string does not just contain white space
  7.             bool hasArgs = false;
  8.             foreach (char c in received.Where(c => !char.IsWhiteSpace(c)))
  9.                 hasArgs = true;
  10.  
  11.             if (!hasArgs)
  12.                 return null;
  13.  
  14.             var wsIndices = new List<int>();
  15.             var smIndices = new List<int>();
  16.  
  17.             // Get the indicies of white spaces and speech marks in the string
  18.             for (int i = 0; i < received.Length; i++) {
  19.                 if (received[i] == ' ')
  20.                     wsIndices.Add(i);
  21.                 else if (received[i] == '"')
  22.                     smIndices.Add(i);
  23.             }
  24.  
  25.             // If one arg, return it
  26.             if (wsIndices.Count == 0 && smIndices.Count == 0)
  27.                 return new string[] { received };
  28.  
  29.             // Make sure the speech mark count is even
  30.             if (smIndices.Count % 2 != 0)
  31.                 return null;
  32.  
  33.             // Convert speech marks to tuples
  34.             var smTuples = new List<Tuple<int, int>>();
  35.             for (int i = 0; i < smIndices.Count; i++)
  36.                 smTuples.Add(new Tuple<int, int>(smIndices[i], smIndices[++i]));
  37.  
  38.             // Remove white space indices that are between speech mark tuple
  39.             foreach (Tuple<int, int> speechMarks in smTuples)
  40.                 for (int i = 0; i < wsIndices.Count; i++)
  41.                     if (wsIndices[i] > speechMarks.Item1 && wsIndices[i] < speechMarks.Item2) {
  42.                         wsIndices.RemoveAt(i);
  43.                         i--;
  44.                     }
  45.  
  46.             // Concatenate the removal indices
  47.             var removalIndices = new List<int>();
  48.             removalIndices = removalIndices.Concat(wsIndices).ToList();
  49.             removalIndices = removalIndices.Concat(smIndices).ToList();
  50.             removalIndices.Sort();
  51.  
  52.             // Remove indices from the string
  53.             int removed = 0;
  54.             for (int i = 0; i < removalIndices.Count; i++) {
  55.                 removalIndices[i] -= removed;
  56.                 received = received.Remove(removalIndices[i], 1);
  57.                 removed++;
  58.             }
  59.  
  60.             // Remove duplicate indices (removed list now turns into arg start points)
  61.             for (int k = 0; k < removalIndices.Count; k++)
  62.                 for (int i = 0; i < removalIndices.Count; i++)
  63.                     if (i != k && removalIndices[i] == removalIndices[k]) {
  64.                         removalIndices.RemoveAt(i);
  65.                         i--;
  66.                     }
  67.  
  68.             // Convert removal points into start and end points for arguments
  69.             var startEndPoints = new List<Tuple<int, int>>();
  70.             // Add a start point if there is not already an element for it
  71.             if (!removalIndices.Contains(0))
  72.                 startEndPoints.Add(new Tuple<int, int>(0, removalIndices[0]));
  73.             for (int i = 0; i < removalIndices.Count; i++) {
  74.                 // Add an end point if this is the last index
  75.                 if (i == removalIndices.Count - 1)
  76.                     startEndPoints.Add(new Tuple<int, int>(removalIndices[i], received.Length));
  77.                 else startEndPoints.Add(new Tuple<int, int>(removalIndices[i], removalIndices[i + 1]));
  78.             }
  79.  
  80.             // Compile the list of arguments
  81.             var argsList = new List<string>();
  82.             foreach (Tuple<int, int> startEndPoint in startEndPoints)
  83.                 argsList.Add(received.Substring(startEndPoint.Item1, startEndPoint.Item2 - startEndPoint.Item1));
  84.  
  85.             // Remove any empty strings
  86.             for (int i = 0; i < argsList.Count; i++)
  87.                 if (argsList[i] == string.Empty) {
  88.                     argsList.RemoveAt(i);
  89.                     i--;
  90.                 }
  91.  
  92.             return argsList.ToArray();
  93.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement