using System.Collections.Generic; using System.Linq; namespace Ink { /// /// A class representing a character range. Allows for lazy-loading a corresponding character set. /// public sealed class CharacterRange { public static CharacterRange Define(char start, char end, IEnumerable excludes = null) { return new CharacterRange (start, end, excludes); } /// /// Returns a character set instance corresponding to the character range /// represented by the current instance. /// /// /// The internal character set is created once and cached in memory. /// /// The char set. public CharacterSet ToCharacterSet () { if (_correspondingCharSet.Count == 0) { for (char c = _start; c <= _end; c++) { if (!_excludes.Contains (c)) { _correspondingCharSet.Add (c); } } } return _correspondingCharSet; } public char start { get { return _start; } } public char end { get { return _end; } } CharacterRange (char start, char end, IEnumerable excludes) { _start = start; _end = end; _excludes = excludes == null ? new HashSet() : new HashSet (excludes); } char _start; char _end; ICollection _excludes; CharacterSet _correspondingCharSet = new CharacterSet(); } }