using System.Collections; using System.Collections.Generic; namespace System.Unicode { /// Supports a standard iteration of code points in a . public struct CodePointEnumerator : IEnumerator { private readonly string text; private int current; private int index; /// Initializes a new instance of the struct. /// The text whose code point should be enumerated. /// is . public CodePointEnumerator(string text) { if (text == null) throw new ArgumentNullException(nameof(text)); this.text = text; this.current = 0; this.index = -1; } /// Gets the element in the collection at the current position of the enumerator.. /// The element in the collection at the current position of the enumerator. public int Current => current; object IEnumerator.Current => current; void IDisposable.Dispose() { } /// Advances the enumerator to the next element of the collection. /// if the enumerator was successfully advanced to the next element; if the enumerator has passed the end of the collection. public bool MoveNext() { if (index < text.Length && (index += current > 0xFFFF ? 2 : 1) < text.Length) { current = char.ConvertToUtf32(text, index); return true; } else { current = 0; return false; } } void IEnumerator.Reset() { current = 0; index = -1; } } }