| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace ZeroC.Slice.Internal; |
| | 4 | |
|
| | 5 | | /// <summary>An enumerator over one or more <see cref="Span{T}" /> of bytes. Used by <see cref="BitSequenceWriter" />. |
| | 6 | | /// </summary> |
| | 7 | | internal ref struct SpanEnumerator |
| | 8 | | { |
| | 9 | | /// <summary>Gets the current span.</summary> |
| 14846 | 10 | | internal readonly Span<byte> Current => _position >= 0 ? _currentSpan : |
| 14846 | 11 | | throw new InvalidOperationException("The enumerator was not initialized."); |
| | 12 | |
|
| | 13 | | private Span<byte> _currentSpan; |
| | 14 | | private readonly Span<byte> _firstSpan; |
| | 15 | | private readonly Span<byte> _secondSpan; |
| | 16 | | private readonly IList<Memory<byte>>? _additionalMemory; |
| | 17 | | private int _position; |
| | 18 | |
|
| | 19 | | /// <summary>Moves to the next span.</summary> |
| | 20 | | /// <returns><see langword="true" /> when the operation was successful, and <see langword="false" /> when the |
| | 21 | | /// current span is the last span.</returns> |
| | 22 | | internal bool MoveNext() |
| 1314 | 23 | | { |
| 1314 | 24 | | switch (_position) |
| | 25 | | { |
| | 26 | | case -1: |
| 1154 | 27 | | _position = 0; |
| 1154 | 28 | | _currentSpan = _firstSpan; |
| 1154 | 29 | | return true; |
| | 30 | | case 0: |
| 38 | 31 | | if (_secondSpan.Length > 0) |
| 35 | 32 | | { |
| 35 | 33 | | _position = 1; |
| 35 | 34 | | _currentSpan = _secondSpan; |
| 35 | 35 | | return true; |
| | 36 | | } |
| | 37 | | else |
| 3 | 38 | | { |
| 3 | 39 | | return false; |
| | 40 | | } |
| | 41 | | default: |
| 122 | 42 | | if (_additionalMemory is not null && _additionalMemory.Count > _position - 1) |
| 109 | 43 | | { |
| 109 | 44 | | _position += 1; |
| 109 | 45 | | _currentSpan = _additionalMemory[_position - 2].Span; |
| 109 | 46 | | return true; |
| | 47 | | } |
| | 48 | | else |
| 13 | 49 | | { |
| 13 | 50 | | return false; |
| | 51 | | } |
| | 52 | | } |
| 1314 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary>Constructs a span enumerator.</summary> |
| | 56 | | internal SpanEnumerator( |
| | 57 | | Span<byte> firstSpan, |
| | 58 | | Span<byte> secondSpan = default, |
| | 59 | | IList<Memory<byte>>? additionalMemory = null) |
| 1154 | 60 | | { |
| 1154 | 61 | | _firstSpan = firstSpan; |
| 1154 | 62 | | _secondSpan = secondSpan; |
| 1154 | 63 | | _additionalMemory = additionalMemory; |
| | 64 | |
|
| 1154 | 65 | | _currentSpan = default; |
| 1154 | 66 | | _position = -1; |
| 1154 | 67 | | } |
| | 68 | | } |