37 lines
888 B
C#
37 lines
888 B
C#
|
|
namespace GParse;
|
|
|
|
public class DelimitedTextParser(string delimiter = ",") : ITextParser
|
|
{
|
|
public IEnumerable<string> Parse(ITextInputProvider textInputProvider) => Parse(textInputProvider.GetText());
|
|
|
|
public IEnumerable<string> Parse(string text)
|
|
{
|
|
int size = text.Length;
|
|
int delimiterSize = delimiter.Length;
|
|
|
|
int tokenStartIndex = 0;
|
|
|
|
for (int index = 0; index < size; index++)
|
|
{
|
|
if (index + delimiterSize <= text.Length)
|
|
if (text.Substring(index, delimiterSize) == delimiter)
|
|
{
|
|
string token = text.Substring(tokenStartIndex, index - tokenStartIndex);
|
|
yield return token;
|
|
|
|
tokenStartIndex = index + delimiterSize;
|
|
}
|
|
}
|
|
|
|
// Output last token.
|
|
if (tokenStartIndex <= size - 1)
|
|
{
|
|
string lastToken = text.Substring(tokenStartIndex, size - tokenStartIndex);
|
|
yield return lastToken;
|
|
}
|
|
else
|
|
yield return string.Empty;
|
|
}
|
|
}
|