67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
// /**
|
|
// * File: CharStream.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
namespace ln.types.stream
|
|
{
|
|
public class CharStream
|
|
{
|
|
public char Current => characters[position];
|
|
public int Position => position;
|
|
|
|
char[] characters;
|
|
int position;
|
|
|
|
public CharStream(IEnumerable<char> characters)
|
|
: this(characters.ToArray()) { }
|
|
|
|
public CharStream(char[] characters)
|
|
{
|
|
this.characters = characters;
|
|
}
|
|
|
|
public bool TryNext()
|
|
{
|
|
position++;
|
|
return (position < this.characters.Length);
|
|
}
|
|
public void MoveNext()
|
|
{
|
|
position++;
|
|
if (position >= this.characters.Length)
|
|
throw new IOException("Unexpected end of characters");
|
|
}
|
|
|
|
public void Skip(Func<char, bool> test)
|
|
{
|
|
if (position < this.characters.Length)
|
|
while (test(Current) && TryNext())
|
|
{
|
|
}
|
|
}
|
|
public char[] Read(Func<char, bool> test)
|
|
{
|
|
List<char> chars = new List<char>();
|
|
|
|
while (test(Current))
|
|
{
|
|
chars.Add(Current);
|
|
MoveNext();
|
|
}
|
|
|
|
return chars.ToArray();
|
|
}
|
|
|
|
|
|
}
|
|
}
|