Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string[] GetArgs(string received) {
- // Check that the incoming string is not empty
- if (received == string.Empty)
- return null;
- // Check to ensure the string does not just contain white space
- bool hasArgs = false;
- foreach (char c in received.Where(c => !char.IsWhiteSpace(c)))
- hasArgs = true;
- if (!hasArgs)
- return null;
- var wsIndices = new List<int>();
- var smIndices = new List<int>();
- // Get the indicies of white spaces and speech marks in the string
- for (int i = 0; i < received.Length; i++) {
- if (received[i] == ' ')
- wsIndices.Add(i);
- else if (received[i] == '"')
- smIndices.Add(i);
- }
- // If one arg, return it
- if (wsIndices.Count == 0 && smIndices.Count == 0)
- return new string[] { received };
- // Make sure the speech mark count is even
- if (smIndices.Count % 2 != 0)
- return null;
- // Convert speech marks to tuples
- var smTuples = new List<Tuple<int, int>>();
- for (int i = 0; i < smIndices.Count; i++)
- smTuples.Add(new Tuple<int, int>(smIndices[i], smIndices[++i]));
- // Remove white space indices that are between speech mark tuple
- foreach (Tuple<int, int> speechMarks in smTuples)
- for (int i = 0; i < wsIndices.Count; i++)
- if (wsIndices[i] > speechMarks.Item1 && wsIndices[i] < speechMarks.Item2) {
- wsIndices.RemoveAt(i);
- i--;
- }
- // Concatenate the removal indices
- var removalIndices = new List<int>();
- removalIndices = removalIndices.Concat(wsIndices).ToList();
- removalIndices = removalIndices.Concat(smIndices).ToList();
- removalIndices.Sort();
- // Remove indices from the string
- int removed = 0;
- for (int i = 0; i < removalIndices.Count; i++) {
- removalIndices[i] -= removed;
- received = received.Remove(removalIndices[i], 1);
- removed++;
- }
- // Remove duplicate indices (removed list now turns into arg start points)
- for (int k = 0; k < removalIndices.Count; k++)
- for (int i = 0; i < removalIndices.Count; i++)
- if (i != k && removalIndices[i] == removalIndices[k]) {
- removalIndices.RemoveAt(i);
- i--;
- }
- // Convert removal points into start and end points for arguments
- var startEndPoints = new List<Tuple<int, int>>();
- // Add a start point if there is not already an element for it
- if (!removalIndices.Contains(0))
- startEndPoints.Add(new Tuple<int, int>(0, removalIndices[0]));
- for (int i = 0; i < removalIndices.Count; i++) {
- // Add an end point if this is the last index
- if (i == removalIndices.Count - 1)
- startEndPoints.Add(new Tuple<int, int>(removalIndices[i], received.Length));
- else startEndPoints.Add(new Tuple<int, int>(removalIndices[i], removalIndices[i + 1]));
- }
- // Compile the list of arguments
- var argsList = new List<string>();
- foreach (Tuple<int, int> startEndPoint in startEndPoints)
- argsList.Add(received.Substring(startEndPoint.Item1, startEndPoint.Item2 - startEndPoint.Item1));
- // Remove any empty strings
- for (int i = 0; i < argsList.Count; i++)
- if (argsList[i] == string.Empty) {
- argsList.RemoveAt(i);
- i--;
- }
- return argsList.ToArray();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement