| | | 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) |
| | | 21 | | { |
| | | 22 | | _sequenceReader = new SequenceReader<byte>(bitSequence); |
| | | 23 | | _index = 0; |
| | | 24 | | if (!_sequenceReader.TryRead(out _currentByte)) |
| | | 25 | | { |
| | | 26 | | throw new ArgumentException( |
| | | 27 | | "Cannot create a bit sequence reader over an empty byte sequence.", |
| | | 28 | | nameof(bitSequence)); |
| | | 29 | | } |
| | | 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() |
| | | 35 | | { |
| | | 36 | | if (_index == 8) |
| | | 37 | | { |
| | | 38 | | _index = 0; |
| | | 39 | | if (!_sequenceReader.TryRead(out _currentByte)) |
| | | 40 | | { |
| | | 41 | | throw new InvalidOperationException("Attempting to read past the end of the bit sequence."); |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | return (_currentByte & (1 << _index++)) != 0; |
| | | 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) |
| | 13564 | 62 | | { |
| | 13564 | 63 | | int byteIndex = _index >> 3; // right-shift by 3 positions, equivalent to divide by 8 |
| | 13564 | 64 | | Span<byte> span = _spanEnumerator.Current; |
| | | 65 | | |
| | 13564 | 66 | | if (byteIndex >= span.Length) |
| | 118 | 67 | | { |
| | 118 | 68 | | if (_spanEnumerator.MoveNext()) |
| | 114 | 69 | | { |
| | 114 | 70 | | span = _spanEnumerator.Current; |
| | 114 | 71 | | span.Clear(); |
| | | 72 | | |
| | 114 | 73 | | _index = 0; |
| | 114 | 74 | | byteIndex = 0; |
| | 114 | 75 | | } |
| | | 76 | | else |
| | 4 | 77 | | { |
| | 4 | 78 | | throw new InvalidOperationException("Cannot write past the end of the bit sequence."); |
| | | 79 | | } |
| | 114 | 80 | | } |
| | | 81 | | |
| | 13560 | 82 | | if (value) |
| | 6439 | 83 | | { |
| | | 84 | | // set bit |
| | 6439 | 85 | | span[byteIndex] = (byte)(span[byteIndex] | (1 << (_index & 0x7))); |
| | 6439 | 86 | | } |
| | | 87 | | // else keep it unset |
| | | 88 | | |
| | 13560 | 89 | | _index++; |
| | 13560 | 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) |
| | 1138 | 98 | | { |
| | 1138 | 99 | | _index = 0; |
| | 1138 | 100 | | _spanEnumerator = new SpanEnumerator(firstSpan, secondSpan, additionalMemory); |
| | 1138 | 101 | | _spanEnumerator.MoveNext(); |
| | | 102 | | |
| | | 103 | | // We fill the span with 0s, this way we only need to set bits, never unset them. |
| | 1138 | 104 | | _spanEnumerator.Current.Clear(); |
| | 1138 | 105 | | } |
| | | 106 | | } |