| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace System.Text |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// An enumerator for retrieving <see cref="Rune"/> instances from a <see cref="StringBuilder"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public struct StringBuilderRuneEnumerator : IEnumerable<Rune>, IEnumerator<Rune> |
| | | 13 | | { |
| | | 14 | | private readonly StringBuilder _stringBuilder; |
| | | 15 | | private Rune _current; |
| | | 16 | | private int _nextIndex; |
| | | 17 | | |
| | | 18 | | internal StringBuilderRuneEnumerator(StringBuilder value) |
| | | 19 | | { |
| | 0 | 20 | | _stringBuilder = value; |
| | 0 | 21 | | _current = default; |
| | 0 | 22 | | _nextIndex = 0; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the <see cref="Rune"/> at the current position of the enumerator. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public readonly Rune Current => _current; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Returns the current enumerator instance. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <returns>The current enumerator instance.</returns> |
| | 0 | 34 | | public readonly StringBuilderRuneEnumerator GetEnumerator() => this; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Advances the enumerator to the next <see cref="Rune"/> of the builder. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <returns> |
| | | 40 | | /// <see langword="true"/> if the enumerator successfully advanced to the next item; |
| | | 41 | | /// <see langword="false"/> if the end of the builder has been reached. |
| | | 42 | | /// </returns> |
| | | 43 | | public bool MoveNext() |
| | | 44 | | { |
| | 0 | 45 | | if ((uint)_nextIndex >= _stringBuilder.Length) |
| | | 46 | | { |
| | | 47 | | // reached the end of the string |
| | 0 | 48 | | _current = default; |
| | 0 | 49 | | return false; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | if (!_stringBuilder.TryGetRuneAt(_nextIndex, out _current)) |
| | | 53 | | { |
| | | 54 | | // replace invalid sequences with U+FFFD |
| | 0 | 55 | | _current = Rune.ReplacementChar; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // In UTF-16 specifically, invalid sequences always have length 1, which is the same |
| | | 59 | | // length as the replacement character U+FFFD. This means that we can always bump the |
| | | 60 | | // next index by the current scalar's UTF-16 sequence length. This optimization is not |
| | | 61 | | // generally applicable; for example, enumerating scalars from UTF-8 cannot utilize |
| | | 62 | | // this same trick. |
| | | 63 | | |
| | 0 | 64 | | _nextIndex += _current.Utf16SequenceLength; |
| | 0 | 65 | | return true; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the <see cref="Rune"/> at the current position of the enumerator. |
| | | 70 | | /// </summary> |
| | 0 | 71 | | readonly object? IEnumerator.Current => _current; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Releases all resources used by the current <see cref="StringBuilderRuneEnumerator"/> instance. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <remarks> |
| | | 77 | | /// This method performs no operation and produces no side effects. |
| | | 78 | | /// </remarks> |
| | | 79 | | readonly void IDisposable.Dispose() |
| | | 80 | | { |
| | | 81 | | // no-op |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Returns the current enumerator instance. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <returns>The current enumerator instance.</returns> |
| | 0 | 88 | | readonly IEnumerator IEnumerable.GetEnumerator() => this; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Returns the current enumerator instance. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <returns>The current enumerator instance.</returns> |
| | 0 | 94 | | readonly IEnumerator<Rune> IEnumerable<Rune>.GetEnumerator() => this; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Resets the current <see cref="StringBuilderRuneEnumerator"/> instance to the beginning of the builder. |
| | | 98 | | /// </summary> |
| | | 99 | | void IEnumerator.Reset() |
| | | 100 | | { |
| | 0 | 101 | | _current = default; |
| | 0 | 102 | | _nextIndex = 0; |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |