Introduction
I use character ranges in my regular expression projects but they are obviously useful in many other situations. In fact, I'm somewhat surprised there isn't some sort of Range<T>
struct in .NET with language support behind it. The reason CharRange
isn't generic is because many of its operations are character specific.
Using this Mess
CharRange
works like many other value types in that it uses value equality semantics and is comparable and equatable. However, it also implements enumeration and indexing.
var test = "abcdlmnorstuvwxz";
var ranges = CharRange.GetRanges(test);
Console.Write("[");
foreach (var range in ranges)
Console.Write(range);
Console.WriteLine("]");
Console.Write("ranges chars: ");
foreach (char ch in CharRange.ExpandRanges(ranges))
Console.Write(ch);
Console.WriteLine();
Console.WriteLine("Packed string: " + CharRange.ToPackedString(ranges));
Console.Write("[");
foreach (var range in CharRange.NotRanges(ranges))
Console.Write(range);
Console.WriteLine("]");
var a2x = new CharRange('a', 'x');
Console.WriteLine("[{0}]: Length = {1}", a2x, a2x.Length);
Console.WriteLine("a2x[2] = " + a2x[2]);
Console.Write("a2x chars: ");
foreach (var ch in a2x)
Console.Write(ch);
Console.WriteLine();
And that's all, kids. All the members are doc commented as well.
History
- 9th January, 2020 - Initial submission