// /** // * 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 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 test) { if (position < this.characters.Length) while (test(Current) && TryNext()) { } } public char[] Read(Func test) { List chars = new List(); while (test(Current)) { chars.Add(Current); MoveNext(); } return chars.ToArray(); } } }