| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using ZeroC.Slice.Internal; |
| | 5 | |
|
| | 6 | | namespace ZeroC.Slice; |
| | 7 | |
|
| | 8 | | /// <summary>Provides a method for reading a bit sequence.</summary> |
| | 9 | | /// <remarks>This struct is typically returned by <see cref="SliceDecoder.GetBitSequenceReader(int)" /> |
| | 10 | | /// to read the bit sequence associated with a Slice type.</remarks> |
| | 11 | | /// <seealso href="https://docs.icerpc.dev/slice2/encoding/encoding-only-constructs#bit-sequence"/> |
| | 12 | | public ref struct BitSequenceReader |
| | 13 | | { |
| | 14 | | private byte _currentByte; |
| | 15 | | private int _index; // between 0 and 7 |
| | 16 | | private SequenceReader<byte> _sequenceReader; |
| | 17 | |
|
| | 18 | | /// <summary>Constructs a bit sequence reader over a <see cref="ReadOnlySequence{T}" />.</summary> |
| | 19 | | /// <param name="bitSequence">The read-only sequence.</param> |
| | 20 | | public BitSequenceReader(ReadOnlySequence<byte> bitSequence) |
| 1124 | 21 | | { |
| 1124 | 22 | | _sequenceReader = new SequenceReader<byte>(bitSequence); |
| 1124 | 23 | | _index = 0; |
| 1124 | 24 | | if (!_sequenceReader.TryRead(out _currentByte)) |
| 0 | 25 | | { |
| 0 | 26 | | throw new ArgumentException( |
| 0 | 27 | | "Cannot create a bit sequence reader over an empty byte sequence.", |
| 0 | 28 | | nameof(bitSequence)); |
| | 29 | | } |
| 1124 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary>Reads the next bit.</summary> |
| | 33 | | /// <returns><see langword="true" /> when the next bit is set; otherwise, <see langword="false" />.</returns> |
| | 34 | | public bool Read() |
| 8814 | 35 | | { |
| 8814 | 36 | | if (_index == 8) |
| 938 | 37 | | { |
| 938 | 38 | | _index = 0; |
| 938 | 39 | | if (!_sequenceReader.TryRead(out _currentByte)) |
| 0 | 40 | | { |
| 0 | 41 | | throw new InvalidOperationException("Attempting to read past the end of the bit sequence."); |
| | 42 | | } |
| 938 | 43 | | } |
| 8814 | 44 | | return (_currentByte & (1 << _index++)) != 0; |
| 8814 | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary>Provides a method for writing a bit sequence.</summary> |
| | 49 | | /// <remarks>This struct is returned by <seealso cref="SliceEncoder.GetBitSequenceWriter(int)" /> |
| | 50 | | /// to write the bit sequence associated with a Slice type.</remarks> |
| | 51 | | /// <seealso href="https://docs.icerpc.dev/slice2/encoding/encoding-only-constructs#bit-sequence"/> |
| | 52 | | public ref struct BitSequenceWriter |
| | 53 | | { |
| | 54 | | private int _index; // the bit index in _spanEnumerator.Current |
| | 55 | |
|
| | 56 | | private SpanEnumerator _spanEnumerator; |
| | 57 | |
|
| | 58 | | /// <summary>Writes the bit at the current position in the underlying bit sequence and moves to the next |
| | 59 | | /// position.</summary> |
| | 60 | | /// <param name="value"><see langword="true" /> to set the bit and <see langword="false" /> to unset it.</param> |
| | 61 | | public void Write(bool value) |
| | 62 | | { |
| | 63 | | int byteIndex = _index >> 3; // right-shift by 3 positions, equivalent to divide by 8 |
| | 64 | | Span<byte> span = _spanEnumerator.Current; |
| | 65 | |
|
| | 66 | | if (byteIndex >= span.Length) |
| | 67 | | { |
| | 68 | | if (_spanEnumerator.MoveNext()) |
| | 69 | | { |
| | 70 | | span = _spanEnumerator.Current; |
| | 71 | | span.Clear(); |
| | 72 | |
|
| | 73 | | _index = 0; |
| | 74 | | byteIndex = 0; |
| | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| | 78 | | throw new InvalidOperationException("Cannot write past the end of the bit sequence."); |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | if (value) |
| | 83 | | { |
| | 84 | | // set bit |
| | 85 | | span[byteIndex] = (byte)(span[byteIndex] | (1 << (_index & 0x7))); |
| | 86 | | } |
| | 87 | | // else keep it unset |
| | 88 | |
|
| | 89 | | _index++; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary>Constructs a bit sequence writer over a bit sequence represented by a <see cref="SpanEnumerator" />. |
| | 93 | | /// </summary> |
| | 94 | | internal BitSequenceWriter( |
| | 95 | | Span<byte> firstSpan, |
| | 96 | | Span<byte> secondSpan = default, |
| | 97 | | IList<Memory<byte>>? additionalMemory = null) |
| | 98 | | { |
| | 99 | | _index = 0; |
| | 100 | | _spanEnumerator = new SpanEnumerator(firstSpan, secondSpan, additionalMemory); |
| | 101 | | _spanEnumerator.MoveNext(); |
| | 102 | |
|
| | 103 | | // We fill the span with 0s, this way we only need to set bits, never unset them. |
| | 104 | | _spanEnumerator.Current.Clear(); |
| | 105 | | } |
| | 106 | | } |