|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeRun.Utils.CommandLine
{
internal class CommandLineArgumentsTokenizer
{
public CommandLineArgumentsTokenizer()
{
}
bool InString;
StringBuilder CurrentToken;
List<string> Tokens;
StringBuilder Processed = new StringBuilder();
public string[] Tokenize(string text)
{
Tokens = new List<string>();
CurrentToken = new StringBuilder();
InString = false;
foreach (char ch in text)
{
if (ch == '\"')
{
if (InString)
{
InString = false;
ContinueToken(ch);
}
else
{
InString = true;
BeginOrContinueToken(ch);
}
}
else if (ch == ' ')
{
if (InString)
ContinueToken(ch);
else
EndTokenIfExists();
}
else
{
BeginOrContinueToken(ch);
}
Processed.Append(ch);
}
EndTokenIfExists();
return Tokens.ToArray();
}
private void BeginToken(char ch)
{
BeginToken();
CurrentToken.Append(ch);
}
private void BeginToken()
{
if (CurrentToken != null)
throw new Exception("cannot begin word in the middle of another word");
CurrentToken = new StringBuilder();
}
private void BeginOrContinueToken(char ch)
{
if (CurrentToken == null)
CurrentToken = new StringBuilder();
CurrentToken.Append(ch);
}
private void ContinueToken(char ch)
{
CurrentToken.Append(ch);
}
void EndToken()
{
var s = CurrentToken.ToString();
if(s.IsNotNullOrEmpty())
Tokens.Add(s);
CurrentToken = null;
}
private void EndTokenIfExists()
{
if (CurrentToken != null)
Tokens.Add(CurrentToken.ToString());
CurrentToken = null;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.