|
using System;
using System.Collections.Generic;
namespace NeoDataType.Documents
{
class Parser
{
string _text;
int _idx;
int _textLength;
const string blanks =" \n\r\t\v\f";
public Parser(string text)
{
_text = text;
_textLength = _text.Length;
_idx = 0;
}
public string Text
{
get { return _text; }
}
public int CurrentPosition
{
get { return _idx; }
set { _idx = value; }
}
public bool HasMoreText
{
get { return _idx < _textLength; }
}
public string FollowingText
{
get { return _text.Substring(_idx); }
}
public char FollowingChar
{
get { return _text[_idx]; }
}
public void SkipBlanks()
{
for (int i = _idx; i < _textLength; i++)
{
if (blanks.IndexOf(_text[i]) >= 0)
{
_idx++;
}
else
break;
}
}
public string GetStringUntil(char[] stopBeforeAnyOfChar)
{
string word;
//SkipBlanks();
int i = _text.IndexOfAny(stopBeforeAnyOfChar, _idx);
if (i >= 0)
{
word = _text.Substring(_idx, i - _idx);
_idx = i;
}
else
{
word = _text.Substring(_idx, _textLength - _idx);
_idx = _textLength;
}
return word;
}
public string GetStringUntil(string stopBeforeText)
{
string word;
//SkipBlanks();
int i = _text.IndexOf(stopBeforeText, _idx, StringComparison.Ordinal);
if (i >= 0)
{
word = _text.Substring(_idx, i - _idx);
_idx = i;
}
else
{
word = _text.Substring(_idx, _textLength - _idx);
_idx = _textLength;
}
return word;
}
public string GetStringBetween(string startAfterText, string stopBeforeText)
{
SkipNext(startAfterText);
return GetStringUntil(stopBeforeText);
}
//public string GetStringBetween(string excludeStartingText, string excludeEndingText, char[] ignoreChars)
//{
// // need some optimizations
// SkipNext(excludeStartingText);
// string s =GetStringUntil(excludeEndingText);
// for(int i = 0; i < ignoreChars.Length; i++)
// {
// s=s.Replace(ignoreChars[i].ToString(), "");
// }
// return s;
//}
public char GetChar()
{
char ch = _text[_idx];
_idx++;
return ch;
}
public string GetString(int length)
{
string word = _text.Substring(_idx, length);
_idx += word.Length;
return word;
}
public string[] GetStringsUntil(string stopBeforeText, string separator)
{
return GetStringUntil(stopBeforeText).Split(new string[] { separator }, StringSplitOptions.None);
}
//public static string[] Split(string text, string separator)
//{
// string[] strings = new string[255];
// int start = 0;
// int end;
// int separatorLength = separator.Length;
// int textLength = text.Length;
// int count = 0;
// do
// {
// end = text.IndexOf(separator, start);
// if (end < 0)
// end = textLength;
// strings[count] = text.Substring(start, end - start);
// start = end + separatorLength;
// count++;
// } while (end != textLength);
// return strings;
//}
public string[] GetStringsUntil(char[] stopBeforeAnyOfChars, string separator)
{
return GetStringUntil(stopBeforeAnyOfChars).Split(new string[] { separator }, StringSplitOptions.None);
}
public bool ContinuesWith(string continueText)
{
if (_idx >= _textLength)
return false;
return (_text.IndexOf(continueText, _idx, StringComparison.Ordinal) == _idx);
}
public bool ContinuesWith(char[] anyOfChars)
{
if (_idx >= _textLength)
return false;
return (_text.IndexOfAny(anyOfChars,_idx) == _idx);
}
public void SkipNext(string skipText)
{
_idx = _text.IndexOf(skipText, _idx, StringComparison.Ordinal);
if (_idx >= 0)
_idx += skipText.Length;
else
_idx = _textLength;
}
public void SkipNextBlock(char[] skipAllCharsBlock)
{
SkipNext(skipAllCharsBlock);
while (ContinuesWith(skipAllCharsBlock))
{
_idx++;
}
}
public void SkipNext(char[] skipAnyOfChars)
{
_idx = _text.IndexOfAny(skipAnyOfChars, _idx);
if (_idx < 0)
_idx = _textLength;
else
_idx++;
}
public void SkipNextChar()
{
_idx++;
}
public void GoToNext(char[] stopAtAnyOfChars)
{
_idx = _text.IndexOfAny(stopAtAnyOfChars, _idx);
if (_idx < 0)
_idx = _textLength;
}
public void GoToNext(string stopAtText)
{
_idx = _text.IndexOf(stopAtText, _idx, StringComparison.Ordinal);
if (_idx < 0)
_idx = _textLength;
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.