| | | 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 | | /// Enumerates the lines of a <see cref="ReadOnlySpan{Char}"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// To get an instance of this type, use <see cref="MemoryExtensions.EnumerateLines(ReadOnlySpan{char})"/>. |
| | | 14 | | /// </remarks> |
| | | 15 | | public ref struct SpanLineEnumerator : IEnumerator<ReadOnlySpan<char>> |
| | | 16 | | { |
| | | 17 | | private ReadOnlySpan<char> _remaining; |
| | | 18 | | private ReadOnlySpan<char> _current; |
| | | 19 | | private bool _isEnumeratorActive; |
| | | 20 | | |
| | | 21 | | internal SpanLineEnumerator(ReadOnlySpan<char> buffer) |
| | | 22 | | { |
| | 0 | 23 | | _remaining = buffer; |
| | 0 | 24 | | _current = default; |
| | 0 | 25 | | _isEnumeratorActive = true; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the line at the current position of the enumerator. |
| | | 30 | | /// </summary> |
| | 0 | 31 | | public ReadOnlySpan<char> Current => _current; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns this instance as an enumerator. |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public SpanLineEnumerator GetEnumerator() => this; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Advances the enumerator to the next line of the span. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <returns> |
| | | 42 | | /// True if the enumerator successfully advanced to the next line; false if |
| | | 43 | | /// the enumerator has advanced past the end of the span. |
| | | 44 | | /// </returns> |
| | | 45 | | public bool MoveNext() |
| | | 46 | | { |
| | 0 | 47 | | if (!_isEnumeratorActive) |
| | | 48 | | { |
| | 0 | 49 | | _current = default; |
| | 0 | 50 | | return false; // EOF previously reached or enumerator was never initialized |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | ReadOnlySpan<char> remaining = _remaining; |
| | | 54 | | |
| | 0 | 55 | | int idx = remaining.IndexOfAny(string.SearchValuesStorage.NewLineChars); |
| | | 56 | | |
| | 0 | 57 | | if ((uint)idx < (uint)remaining.Length) |
| | | 58 | | { |
| | 0 | 59 | | int stride = 1; |
| | | 60 | | |
| | 0 | 61 | | if (remaining[idx] == '\r' && (uint)(idx + 1) < (uint)remaining.Length && remaining[idx + 1] == '\n') |
| | | 62 | | { |
| | 0 | 63 | | stride = 2; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | _current = remaining.Slice(0, idx); |
| | 0 | 67 | | _remaining = remaining.Slice(idx + stride); |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | | 71 | | // We've reached EOF, but we still need to return 'true' for this final |
| | | 72 | | // iteration so that the caller can query the Current property once more. |
| | | 73 | | |
| | 0 | 74 | | _current = remaining; |
| | 0 | 75 | | _remaining = default; |
| | 0 | 76 | | _isEnumeratorActive = false; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | 0 | 83 | | object IEnumerator.Current => throw new NotSupportedException(); |
| | | 84 | | |
| | | 85 | | /// <inheritdoc /> |
| | 0 | 86 | | void IEnumerator.Reset() => throw new NotSupportedException(); |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | 0 | 89 | | void IDisposable.Dispose() { } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |