Updated on 2020-02-02
Add UTF-32 support and easy foreach streaming to your apps
My scanners/tokenizers all take IEnumerable
The code is straightforward to use allowing for a couple of gotchas which I'll cover.
// Lifetime is automatically managed
var fr = new FileReaderEnumerable(@"..\..\Program.cs");
// file open on enumeration start
foreach (var ch in fr)
Console.Write(ch);
// file close when done
Console.WriteLine();
Console.WriteLine();
var ur = new UrlReaderEnumerable(@"http://www.google.com");
var i = 0;
// url fetch on enumeration start
foreach (var ch in ur)
{
if(79==i)
{
Console.Write("...");
break;
}
Console.Write(ch);
++i;
}
// url close on done
Console.WriteLine();
Console.WriteLine();
// put in a string with a 21-bit unicode value
var test = "This is a test \U0010FFEE";
var uni = new Utf32Enumerable(test);
foreach(var uch in uni)
{
// console will mangle, but
// do it anyway
var str = char.ConvertFromUtf32(uch);
Console.Write(str);
}
Console.WriteLine();
Console.WriteLine();
var reader = new StringReader("This is a demo of TextReaderEnumerable");
foreach (char ch in TextReaderEnumerable.FromReader(reader))
Console.Write(ch);
Console.WriteLine();
Console.WriteLine();
The gotchas here are this: TextReaderEnumerable must be created via FromReader(), unlike the others which are created via a constructor. The other gotcha with it is that it cannot be reset and it can only be enumerated once (no seeking). Attempting to enumerate it a second time will throw. Furthermore, it's important to call Dispose() on the IEnumerator<> instances if you are using them manually. foreach does this for you automatically. Finally, when using ConsoleReaderEnumerable, it doesn't know when to stop short of you typing Ctrl-Z into the console. It's usually used for file piping.
The nice thing about these interfaces is it's super easy to load them into List
Have fun!