| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | using static ZeroC.Slice.Internal.Slice1Definitions; |
| | | 9 | | |
| | | 10 | | namespace ZeroC.Slice; |
| | | 11 | | |
| | | 12 | | /// <summary>Provides methods to encode data with Slice.</summary> |
| | | 13 | | public ref partial struct SliceEncoder |
| | | 14 | | { |
| | | 15 | | /// <summary>Gets the number of bytes encoded by this encoder into the underlying buffer writer.</summary> |
| | 1420656 | 16 | | public int EncodedByteCount { get; private set; } |
| | | 17 | | |
| | | 18 | | /// <summary>Gets the Slice encoding of this encoder.</summary> |
| | 327193 | 19 | | public SliceEncoding Encoding { get; } |
| | | 20 | | |
| | | 21 | | internal const long VarInt62MinValue = -2_305_843_009_213_693_952; // -2^61 |
| | | 22 | | internal const long VarInt62MaxValue = 2_305_843_009_213_693_951; // 2^61 - 1 |
| | | 23 | | internal const ulong VarUInt62MinValue = 0; |
| | | 24 | | internal const ulong VarUInt62MaxValue = 4_611_686_018_427_387_903; // 2^62 - 1 |
| | | 25 | | |
| | 8 | 26 | | private static readonly UTF8Encoding _utf8 = |
| | 8 | 27 | | new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); // no BOM |
| | | 28 | | |
| | | 29 | | private readonly IBufferWriter<byte> _bufferWriter; |
| | | 30 | | |
| | | 31 | | private ClassContext _classContext; |
| | | 32 | | |
| | | 33 | | private Encoder? _utf8Encoder; // initialized lazily |
| | | 34 | | |
| | | 35 | | /// <summary>Encodes an int as a Slice int32 into a span of 4 bytes.</summary> |
| | | 36 | | /// <param name="value">The value to encode.</param> |
| | | 37 | | /// <param name="into">The destination byte buffer, which must be 4 bytes long.</param> |
| | | 38 | | public static void EncodeInt32(int value, Span<byte> into) |
| | 5612 | 39 | | { |
| | 5612 | 40 | | Debug.Assert(into.Length == 4); |
| | 5612 | 41 | | MemoryMarshal.Write(into, in value); |
| | 5612 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Encodes a ulong as a Slice varuint62 into a span of bytes using a fixed number of bytes.</summary> |
| | | 45 | | /// <param name="value">The value to encode.</param> |
| | | 46 | | /// <param name="into">The destination byte buffer, which must be 1, 2, 4 or 8 bytes long.</param> |
| | | 47 | | public static void EncodeVarUInt62(ulong value, Span<byte> into) |
| | 176174 | 48 | | { |
| | 176174 | 49 | | int sizeLength = into.Length; |
| | 176174 | 50 | | Debug.Assert(sizeLength == 1 || sizeLength == 2 || sizeLength == 4 || sizeLength == 8); |
| | | 51 | | |
| | 176174 | 52 | | (uint encodedSizeExponent, long maxSize) = sizeLength switch |
| | 176174 | 53 | | { |
| | 138810 | 54 | | 1 => (0x00u, 63), // 2^6 - 1 |
| | 6699 | 55 | | 2 => (0x01u, 16_383), // 2^14 - 1 |
| | 30663 | 56 | | 4 => (0x02u, 1_073_741_823), // 2^30 - 1 |
| | 2 | 57 | | _ => (0x03u, (long)VarUInt62MaxValue) |
| | 176174 | 58 | | }; |
| | | 59 | | |
| | 176174 | 60 | | if (value > (ulong)maxSize) |
| | 4 | 61 | | { |
| | 4 | 62 | | throw new ArgumentOutOfRangeException( |
| | 4 | 63 | | nameof(value), |
| | 4 | 64 | | $"The value '{value}' cannot be encoded on {sizeLength} bytes."); |
| | | 65 | | } |
| | | 66 | | |
| | 176170 | 67 | | Span<byte> ulongBuf = stackalloc byte[8]; |
| | 176170 | 68 | | value <<= 2; |
| | | 69 | | |
| | 176170 | 70 | | value |= encodedSizeExponent; |
| | 176170 | 71 | | MemoryMarshal.Write(ulongBuf, in value); |
| | 176170 | 72 | | ulongBuf[0..sizeLength].CopyTo(into); |
| | 176170 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary>Computes the minimum number of bytes required to encode a long value using the Slice encoding's |
| | | 76 | | /// variable-size encoded representation.</summary> |
| | | 77 | | /// <param name="value">The long value.</param> |
| | | 78 | | /// <returns>The minimum number of bytes required to encode <paramref name="value" />. Can be 1, 2, 4 or 8. |
| | | 79 | | /// </returns> |
| | 0 | 80 | | public static int GetVarInt62EncodedSize(long value) => 1 << GetVarInt62EncodedSizeExponent(value); |
| | | 81 | | |
| | | 82 | | /// <summary>Computes the minimum number of bytes required to encode a ulong value using the Slice encoding's |
| | | 83 | | /// variable-size encoded representation.</summary> |
| | | 84 | | /// <param name="value">The ulong value.</param> |
| | | 85 | | /// <returns>The minimum number of bytes required to encode <paramref name="value" />. Can be 1, 2, 4 or 8. |
| | | 86 | | /// </returns> |
| | 144999 | 87 | | public static int GetVarUInt62EncodedSize(ulong value) => 1 << GetVarUInt62EncodedSizeExponent(value); |
| | | 88 | | |
| | | 89 | | /// <summary>Constructs a Slice encoder.</summary> |
| | | 90 | | /// <param name="bufferWriter">A buffer writer that writes to byte buffers. See important remarks below.</param> |
| | | 91 | | /// <param name="encoding">The Slice encoding.</param> |
| | | 92 | | /// <param name="classFormat">The class format (Slice1 only).</param> |
| | | 93 | | /// <remarks>Warning: the Slice encoding requires rewriting buffers, and many buffer writers do not support this |
| | | 94 | | /// behavior. It is safe to use a pipe writer or a buffer writer that writes to a single fixed-size buffer (without |
| | | 95 | | /// reallocation).</remarks> |
| | | 96 | | public SliceEncoder(IBufferWriter<byte> bufferWriter, SliceEncoding encoding, ClassFormat classFormat = default) |
| | 37209 | 97 | | : this() |
| | 37209 | 98 | | { |
| | 37209 | 99 | | Encoding = encoding; |
| | 37209 | 100 | | _bufferWriter = bufferWriter; |
| | 37209 | 101 | | _classContext = new ClassContext(classFormat); |
| | 37209 | 102 | | } |
| | | 103 | | |
| | | 104 | | // Encode methods for basic types |
| | | 105 | | |
| | | 106 | | /// <summary>Encodes a bool into a Slice bool.</summary> |
| | | 107 | | /// <param name="v">The boolean to encode.</param> |
| | 3252 | 108 | | public void EncodeBool(bool v) => EncodeUInt8(v ? (byte)1 : (byte)0); |
| | | 109 | | |
| | | 110 | | /// <summary>Encodes a float into a Slice float32.</summary> |
| | | 111 | | /// <param name="v">The float to encode.</param> |
| | 0 | 112 | | public void EncodeFloat32(float v) => EncodeFixedSizeNumeric(v); |
| | | 113 | | |
| | | 114 | | /// <summary>Encodes a double into a Slice float64.</summary> |
| | | 115 | | /// <param name="v">The double to encode.</param> |
| | 0 | 116 | | public void EncodeFloat64(double v) => EncodeFixedSizeNumeric(v); |
| | | 117 | | |
| | | 118 | | /// <summary>Encodes an sbyte into a Slice int8.</summary> |
| | | 119 | | /// <param name="v">The sbyte to encode.</param> |
| | 2 | 120 | | public void EncodeInt8(sbyte v) => EncodeUInt8((byte)v); |
| | | 121 | | |
| | | 122 | | /// <summary>Encodes a short into a Slice int16.</summary> |
| | | 123 | | /// <param name="v">The short to encode.</param> |
| | 2125 | 124 | | public void EncodeInt16(short v) => EncodeFixedSizeNumeric(v); |
| | | 125 | | |
| | | 126 | | /// <summary>Encodes an int into a Slice int32.</summary> |
| | | 127 | | /// <param name="v">The int to encode.</param> |
| | 150578 | 128 | | public void EncodeInt32(int v) => EncodeFixedSizeNumeric(v); |
| | | 129 | | |
| | | 130 | | /// <summary>Encodes a long into a Slice int64.</summary> |
| | | 131 | | /// <param name="v">The long to encode.</param> |
| | 113 | 132 | | public void EncodeInt64(long v) => EncodeFixedSizeNumeric(v); |
| | | 133 | | |
| | | 134 | | /// <summary>Encodes a size on variable number of bytes.</summary> |
| | | 135 | | /// <param name="value">The size to encode.</param> |
| | | 136 | | public void EncodeSize(int value) |
| | 40568 | 137 | | { |
| | 40568 | 138 | | if (value < 0) |
| | 2 | 139 | | { |
| | 2 | 140 | | throw new ArgumentException( |
| | 2 | 141 | | $"The {nameof(value)} argument must be greater than 0.", |
| | 2 | 142 | | nameof(value)); |
| | | 143 | | } |
| | | 144 | | |
| | 40566 | 145 | | if (Encoding == SliceEncoding.Slice1) |
| | 25796 | 146 | | { |
| | 25796 | 147 | | if (value < 255) |
| | 25777 | 148 | | { |
| | 25777 | 149 | | EncodeUInt8((byte)value); |
| | 25777 | 150 | | } |
| | | 151 | | else |
| | 19 | 152 | | { |
| | 19 | 153 | | EncodeUInt8(255); |
| | 19 | 154 | | EncodeInt32(value); |
| | 19 | 155 | | } |
| | 25796 | 156 | | } |
| | | 157 | | else |
| | 14770 | 158 | | { |
| | 14770 | 159 | | EncodeVarUInt62((ulong)value); |
| | 14770 | 160 | | } |
| | 40566 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary>Encodes a string into a Slice string.</summary> |
| | | 164 | | /// <param name="v">The string to encode.</param> |
| | | 165 | | public void EncodeString(string v) |
| | 153450 | 166 | | { |
| | 153450 | 167 | | if (v.Length == 0) |
| | 10883 | 168 | | { |
| | 10883 | 169 | | EncodeSize(0); |
| | 10883 | 170 | | } |
| | | 171 | | else |
| | 142567 | 172 | | { |
| | 142567 | 173 | | int maxSize = _utf8.GetMaxByteCount(v.Length); |
| | 142567 | 174 | | int sizeLength = GetSizeLength(maxSize); |
| | 142567 | 175 | | Span<byte> sizePlaceholder = GetPlaceholderSpan(sizeLength); |
| | | 176 | | |
| | 142567 | 177 | | Span<byte> currentSpan = _bufferWriter.GetSpan(); |
| | 142567 | 178 | | if (currentSpan.Length >= maxSize) |
| | 141419 | 179 | | { |
| | | 180 | | // Encode directly into currentSpan |
| | 141419 | 181 | | int size = _utf8.GetBytes(v, currentSpan); |
| | 141419 | 182 | | EncodeSizeIntoPlaceholder(Encoding, size, sizePlaceholder); |
| | 141419 | 183 | | Advance(size); |
| | 141419 | 184 | | } |
| | | 185 | | else |
| | 1148 | 186 | | { |
| | | 187 | | // Encode piecemeal using _utf8Encoder |
| | 1148 | 188 | | if (_utf8Encoder is null) |
| | 65 | 189 | | { |
| | 65 | 190 | | _utf8Encoder = _utf8.GetEncoder(); |
| | 65 | 191 | | } |
| | | 192 | | else |
| | 1083 | 193 | | { |
| | 1083 | 194 | | _utf8Encoder.Reset(); |
| | 1083 | 195 | | } |
| | | 196 | | |
| | 1148 | 197 | | ReadOnlySpan<char> chars = v.AsSpan(); |
| | 1148 | 198 | | _utf8Encoder.Convert(chars, _bufferWriter, flush: true, out long bytesUsed, out bool completed); |
| | | 199 | | |
| | 1148 | 200 | | Debug.Assert(completed); // completed is always true when flush is true |
| | 1148 | 201 | | int size = checked((int)bytesUsed); |
| | 1148 | 202 | | EncodedByteCount += size; |
| | 1148 | 203 | | EncodeSizeIntoPlaceholder(Encoding, size, sizePlaceholder); |
| | 1148 | 204 | | } |
| | 142567 | 205 | | } |
| | | 206 | | |
| | | 207 | | static void EncodeSizeIntoPlaceholder(SliceEncoding encoding, int size, Span<byte> into) |
| | 142567 | 208 | | { |
| | 142567 | 209 | | if (encoding == SliceEncoding.Slice1) |
| | 3830 | 210 | | { |
| | 3830 | 211 | | if (into.Length == 1) |
| | 3812 | 212 | | { |
| | 3812 | 213 | | Debug.Assert(size < 255); |
| | 3812 | 214 | | into[0] = (byte)size; |
| | 3812 | 215 | | } |
| | | 216 | | else |
| | 18 | 217 | | { |
| | 18 | 218 | | Debug.Assert(into.Length == 5); |
| | 18 | 219 | | into[0] = 255; |
| | 18 | 220 | | EncodeInt32(size, into[1..]); |
| | 18 | 221 | | } |
| | 3830 | 222 | | } |
| | | 223 | | else |
| | 138737 | 224 | | { |
| | 138737 | 225 | | EncodeVarUInt62((ulong)size, into); |
| | 138737 | 226 | | } |
| | 142567 | 227 | | } |
| | 153450 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary>Encodes a byte into a Slice uint8.</summary> |
| | | 231 | | /// <param name="v">The byte to encode.</param> |
| | | 232 | | public void EncodeUInt8(byte v) |
| | 75029 | 233 | | { |
| | 75029 | 234 | | Span<byte> span = _bufferWriter.GetSpan(); |
| | 75029 | 235 | | span[0] = v; |
| | 75029 | 236 | | Advance(1); |
| | 75029 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary>Encodes a ushort into a Slice uint16.</summary> |
| | | 240 | | /// <param name="v">The ushort to encode.</param> |
| | 2 | 241 | | public void EncodeUInt16(ushort v) => EncodeFixedSizeNumeric(v); |
| | | 242 | | |
| | | 243 | | /// <summary>Encodes a uint into a Slice uint32.</summary> |
| | | 244 | | /// <param name="v">The uint to encode.</param> |
| | 22 | 245 | | public void EncodeUInt32(uint v) => EncodeFixedSizeNumeric(v); |
| | | 246 | | |
| | | 247 | | /// <summary>Encodes a ulong into a Slice uint64.</summary> |
| | | 248 | | /// <param name="v">The ulong to encode.</param> |
| | 0 | 249 | | public void EncodeUInt64(ulong v) => EncodeFixedSizeNumeric(v); |
| | | 250 | | |
| | | 251 | | /// <summary>Encodes an int into a Slice varint32.</summary> |
| | | 252 | | /// <param name="v">The int to encode.</param> |
| | 300 | 253 | | public void EncodeVarInt32(int v) => EncodeVarInt62(v); |
| | | 254 | | |
| | | 255 | | /// <summary>Encodes a long into a Slice varint62, with the minimum number of bytes required |
| | | 256 | | /// by the encoding.</summary> |
| | | 257 | | /// <param name="v">The long to encode. It must be in the range [-2^61..2^61 - 1].</param> |
| | | 258 | | public void EncodeVarInt62(long v) |
| | 321 | 259 | | { |
| | 321 | 260 | | int encodedSizeExponent = GetVarInt62EncodedSizeExponent(v); |
| | 319 | 261 | | v <<= 2; |
| | 319 | 262 | | v |= (uint)encodedSizeExponent; |
| | | 263 | | |
| | 319 | 264 | | Span<byte> data = _bufferWriter.GetSpan(sizeof(long)); |
| | 319 | 265 | | MemoryMarshal.Write(data, in v); |
| | 319 | 266 | | Advance(1 << encodedSizeExponent); |
| | 319 | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary>Encodes a uint into a Slice varuint32.</summary> |
| | | 270 | | /// <param name="v">The uint to encode.</param> |
| | 0 | 271 | | public void EncodeVarUInt32(uint v) => EncodeVarUInt62(v); |
| | | 272 | | |
| | | 273 | | /// <summary>Encodes a ulong into a Slice varuint62, with the minimum number of bytes |
| | | 274 | | /// required by the encoding.</summary> |
| | | 275 | | /// <param name="v">The ulong to encode. It must be in the range [0..2^62 - 1].</param> |
| | | 276 | | public void EncodeVarUInt62(ulong v) |
| | 50091 | 277 | | { |
| | 50091 | 278 | | int encodedSizeExponent = GetVarUInt62EncodedSizeExponent(v); |
| | 50090 | 279 | | v <<= 2; |
| | 50090 | 280 | | v |= (uint)encodedSizeExponent; |
| | | 281 | | |
| | 50090 | 282 | | Span<byte> data = _bufferWriter.GetSpan(sizeof(ulong)); |
| | 50090 | 283 | | MemoryMarshal.Write(data, in v); |
| | 50090 | 284 | | Advance(1 << encodedSizeExponent); |
| | 50090 | 285 | | } |
| | | 286 | | |
| | | 287 | | // Other methods |
| | | 288 | | |
| | | 289 | | /// <summary>Encodes a non-null Slice2 encoded tagged value. The number of bytes needed to encode the value is |
| | | 290 | | /// not known before encoding this value (Slice2 only).</summary> |
| | | 291 | | /// <typeparam name="T">The type of the value being encoded.</typeparam> |
| | | 292 | | /// <param name="tag">The tag.</param> |
| | | 293 | | /// <param name="v">The value to encode.</param> |
| | | 294 | | /// <param name="encodeAction">The delegate that encodes the value after the tag header.</param> |
| | | 295 | | public void EncodeTagged<T>(int tag, T v, EncodeAction<T> encodeAction) where T : notnull |
| | 24 | 296 | | { |
| | 24 | 297 | | if (Encoding == SliceEncoding.Slice1) |
| | 0 | 298 | | { |
| | 0 | 299 | | throw new InvalidOperationException("Slice1 encoded tags must be encoded with tag formats."); |
| | | 300 | | } |
| | | 301 | | |
| | 24 | 302 | | EncodeVarInt32(tag); // the key |
| | 24 | 303 | | Span<byte> sizePlaceholder = GetPlaceholderSpan(4); |
| | 24 | 304 | | int startPos = EncodedByteCount; |
| | 24 | 305 | | encodeAction(ref this, v); |
| | 24 | 306 | | EncodeVarUInt62((ulong)(EncodedByteCount - startPos), sizePlaceholder); |
| | 24 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary>Encodes a non-null encoded tagged value. The number of bytes needed to encode the value is |
| | | 310 | | /// known before encoding the value. With Slice1 encoding this method always use the VSize tag format.</summary> |
| | | 311 | | /// <typeparam name="T">The type of the value being encoded.</typeparam> |
| | | 312 | | /// <param name="tag">The tag.</param> |
| | | 313 | | /// <param name="size">The number of bytes needed to encode the value.</param> |
| | | 314 | | /// <param name="v">The value to encode.</param> |
| | | 315 | | /// <param name="encodeAction">The delegate that encodes the value after the tag header.</param> |
| | | 316 | | public void EncodeTagged<T>(int tag, int size, T v, EncodeAction<T> encodeAction) where T : notnull |
| | 41 | 317 | | { |
| | 41 | 318 | | if (size <= 0) |
| | 0 | 319 | | { |
| | 0 | 320 | | throw new ArgumentException("Invalid size value, size must be greater than zero.", nameof(size)); |
| | | 321 | | } |
| | | 322 | | |
| | 41 | 323 | | if (Encoding == SliceEncoding.Slice1) |
| | 16 | 324 | | { |
| | 16 | 325 | | EncodeTaggedFieldHeader(tag, TagFormat.VSize); |
| | 16 | 326 | | } |
| | | 327 | | else |
| | 25 | 328 | | { |
| | 25 | 329 | | EncodeVarInt32(tag); |
| | 25 | 330 | | } |
| | | 331 | | |
| | 41 | 332 | | EncodeSize(size); |
| | 41 | 333 | | int startPos = EncodedByteCount; |
| | 41 | 334 | | encodeAction(ref this, v); |
| | | 335 | | |
| | 41 | 336 | | int actualSize = EncodedByteCount - startPos; |
| | 41 | 337 | | if (actualSize != size) |
| | 0 | 338 | | { |
| | 0 | 339 | | throw new ArgumentException( |
| | 0 | 340 | | $"The value of size ({size}) does not match encoded size ({actualSize}).", |
| | 0 | 341 | | nameof(size)); |
| | | 342 | | } |
| | 41 | 343 | | } |
| | | 344 | | |
| | | 345 | | /// <summary>Encodes a non-null Slice1 encoded tagged value. The number of bytes needed to encode the value is |
| | | 346 | | /// not known before encoding this value.</summary> |
| | | 347 | | /// <typeparam name="T">The type of the value being encoded.</typeparam> |
| | | 348 | | /// <param name="tag">The tag. Must be either FSize or OptimizedVSize.</param> |
| | | 349 | | /// <param name="tagFormat">The tag format.</param> |
| | | 350 | | /// <param name="v">The value to encode.</param> |
| | | 351 | | /// <param name="encodeAction">The delegate that encodes the value after the tag header.</param> |
| | | 352 | | /// <exception cref="ArgumentException">Thrown if <paramref name="tagFormat" /> is VSize.</exception> |
| | | 353 | | public void EncodeTagged<T>( |
| | | 354 | | int tag, |
| | | 355 | | TagFormat tagFormat, |
| | | 356 | | T v, |
| | | 357 | | EncodeAction<T> encodeAction) where T : notnull |
| | 75 | 358 | | { |
| | 75 | 359 | | if (Encoding != SliceEncoding.Slice1) |
| | 0 | 360 | | { |
| | 0 | 361 | | throw new InvalidOperationException("Tag formats can only be used with the Slice1 encoding."); |
| | | 362 | | } |
| | | 363 | | |
| | 75 | 364 | | switch (tagFormat) |
| | | 365 | | { |
| | | 366 | | case TagFormat.F1: |
| | | 367 | | case TagFormat.F2: |
| | | 368 | | case TagFormat.F4: |
| | | 369 | | case TagFormat.F8: |
| | | 370 | | case TagFormat.Size: |
| | 51 | 371 | | EncodeTaggedFieldHeader(tag, tagFormat); |
| | 51 | 372 | | encodeAction(ref this, v); |
| | 51 | 373 | | break; |
| | | 374 | | case TagFormat.FSize: |
| | 6 | 375 | | EncodeTaggedFieldHeader(tag, tagFormat); |
| | 6 | 376 | | Span<byte> placeholder = GetPlaceholderSpan(4); |
| | 6 | 377 | | int startPos = EncodedByteCount; |
| | 6 | 378 | | encodeAction(ref this, v); |
| | | 379 | | // We don't include the size-length in the size we encode. |
| | 6 | 380 | | EncodeInt32(EncodedByteCount - startPos, placeholder); |
| | 6 | 381 | | break; |
| | | 382 | | |
| | | 383 | | case TagFormat.OptimizedVSize: |
| | | 384 | | // Used to encode string, and sequences of non optional elements with 1 byte min wire size, |
| | | 385 | | // in this case OptimizedVSize is always used to optimize out the size. |
| | 18 | 386 | | EncodeTaggedFieldHeader(tag, TagFormat.VSize); |
| | 18 | 387 | | encodeAction(ref this, v); |
| | 18 | 388 | | break; |
| | | 389 | | |
| | | 390 | | default: |
| | 0 | 391 | | throw new ArgumentException($"Invalid tag format value: '{tagFormat}'.", nameof(tagFormat)); |
| | | 392 | | } |
| | 75 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary>Allocates a new bit sequence in the underlying buffer(s) and returns a writer for this bit |
| | | 396 | | /// sequence.</summary> |
| | | 397 | | /// <param name="bitSequenceSize">The minimum number of bits in the bit sequence.</param> |
| | | 398 | | /// <returns>The bit sequence writer.</returns> |
| | | 399 | | public BitSequenceWriter GetBitSequenceWriter(int bitSequenceSize) |
| | 1126 | 400 | | { |
| | 1126 | 401 | | if (Encoding == SliceEncoding.Slice1) |
| | 0 | 402 | | { |
| | 0 | 403 | | throw new InvalidOperationException("The bit sequence writer cannot be used with the Slice1 encoding."); |
| | | 404 | | } |
| | | 405 | | |
| | 1126 | 406 | | if (bitSequenceSize <= 0) |
| | 0 | 407 | | { |
| | 0 | 408 | | throw new ArgumentOutOfRangeException( |
| | 0 | 409 | | nameof(bitSequenceSize), |
| | 0 | 410 | | $"The {nameof(bitSequenceSize)} argument must be greater than 0."); |
| | | 411 | | } |
| | | 412 | | |
| | 1126 | 413 | | int remaining = GetBitSequenceByteCount(bitSequenceSize); |
| | | 414 | | |
| | 1126 | 415 | | Span<byte> firstSpan = _bufferWriter.GetSpan(); |
| | 1126 | 416 | | Span<byte> secondSpan = default; |
| | | 417 | | |
| | | 418 | | // We only create this additionalMemory list in the rare situation where 2 spans are not sufficient. |
| | 1126 | 419 | | List<Memory<byte>>? additionalMemory = null; |
| | | 420 | | |
| | 1126 | 421 | | if (firstSpan.Length >= remaining) |
| | 1114 | 422 | | { |
| | 1114 | 423 | | firstSpan = firstSpan[0..remaining]; |
| | 1114 | 424 | | Advance(remaining); |
| | 1114 | 425 | | } |
| | | 426 | | else |
| | 12 | 427 | | { |
| | 12 | 428 | | Advance(firstSpan.Length); |
| | 12 | 429 | | remaining -= firstSpan.Length; |
| | | 430 | | |
| | 12 | 431 | | secondSpan = _bufferWriter.GetSpan(); |
| | 12 | 432 | | if (secondSpan.Length >= remaining) |
| | 6 | 433 | | { |
| | 6 | 434 | | secondSpan = secondSpan[0..remaining]; |
| | 6 | 435 | | Advance(remaining); |
| | 6 | 436 | | } |
| | | 437 | | else |
| | 6 | 438 | | { |
| | 6 | 439 | | Advance(secondSpan.Length); |
| | 6 | 440 | | remaining -= secondSpan.Length; |
| | 6 | 441 | | additionalMemory = new List<Memory<byte>>(); |
| | | 442 | | |
| | | 443 | | do |
| | 78 | 444 | | { |
| | 78 | 445 | | Memory<byte> memory = _bufferWriter.GetMemory(); |
| | 78 | 446 | | if (memory.Length >= remaining) |
| | 6 | 447 | | { |
| | 6 | 448 | | additionalMemory.Add(memory[0..remaining]); |
| | 6 | 449 | | Advance(remaining); |
| | 6 | 450 | | remaining = 0; |
| | 6 | 451 | | } |
| | | 452 | | else |
| | 72 | 453 | | { |
| | 72 | 454 | | additionalMemory.Add(memory); |
| | 72 | 455 | | Advance(memory.Length); |
| | 72 | 456 | | remaining -= memory.Length; |
| | 72 | 457 | | } |
| | 78 | 458 | | } |
| | 78 | 459 | | while (remaining > 0); |
| | 6 | 460 | | } |
| | 12 | 461 | | } |
| | | 462 | | |
| | 1126 | 463 | | return new BitSequenceWriter(firstSpan, secondSpan, additionalMemory); |
| | 1126 | 464 | | } |
| | | 465 | | |
| | | 466 | | /// <summary>Gets a placeholder to be filled-in later.</summary> |
| | | 467 | | /// <param name="size">The size of the placeholder, typically a small number like 4.</param> |
| | | 468 | | /// <returns>A buffer of length <paramref name="size" />.</returns> |
| | | 469 | | /// <remarks>We make the assumption the underlying buffer writer allows rewriting memory it provided even after |
| | | 470 | | /// successive calls to GetMemory/GetSpan and Advance.</remarks> |
| | | 471 | | public Span<byte> GetPlaceholderSpan(int size) |
| | 179106 | 472 | | { |
| | 179106 | 473 | | Debug.Assert(size > 0); |
| | 179106 | 474 | | Span<byte> placeholder = _bufferWriter.GetSpan(size)[0..size]; |
| | 179106 | 475 | | Advance(size); |
| | 179106 | 476 | | return placeholder; |
| | 179106 | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary>Computes the minimum number of bytes needed to encode a variable-length size.</summary> |
| | | 480 | | /// <param name="size">The size.</param> |
| | | 481 | | /// <returns>The minimum number of bytes.</returns> |
| | 142578 | 482 | | public readonly int GetSizeLength(int size) => Encoding == SliceEncoding.Slice1 ? |
| | 142578 | 483 | | (size < 255 ? 1 : 5) : GetVarUInt62EncodedSize(checked((ulong)size)); |
| | | 484 | | |
| | | 485 | | /// <summary>Copies a span of bytes to the underlying buffer writer.</summary> |
| | | 486 | | /// <param name="span">The span to copy.</param> |
| | | 487 | | public void WriteByteSpan(ReadOnlySpan<byte> span) |
| | 11851 | 488 | | { |
| | 11851 | 489 | | _bufferWriter.Write(span); |
| | 11851 | 490 | | EncodedByteCount += span.Length; |
| | 11851 | 491 | | } |
| | | 492 | | |
| | 2220 | 493 | | internal static int GetBitSequenceByteCount(int bitCount) => (bitCount >> 3) + ((bitCount & 0x07) != 0 ? 1 : 0); |
| | | 494 | | |
| | | 495 | | /// <summary>Encodes a fixed-size numeric value.</summary> |
| | | 496 | | /// <param name="v">The numeric value to encode.</param> |
| | | 497 | | internal void EncodeFixedSizeNumeric<T>(T v) where T : struct |
| | 153352 | 498 | | { |
| | 153352 | 499 | | int elementSize = Unsafe.SizeOf<T>(); |
| | 153352 | 500 | | Span<byte> data = _bufferWriter.GetSpan(elementSize)[0..elementSize]; |
| | 153352 | 501 | | MemoryMarshal.Write(data, in v); |
| | 153352 | 502 | | Advance(elementSize); |
| | 153352 | 503 | | } |
| | | 504 | | |
| | | 505 | | /// <summary>Gets a placeholder to be filled-in later.</summary> |
| | | 506 | | /// <param name="size">The size of the placeholder, typically a small number like 4.</param> |
| | | 507 | | /// <returns>A buffer of length <paramref name="size" />.</returns> |
| | | 508 | | /// <remarks>We make the assumption the underlying buffer writer allows rewriting memory it provided even after |
| | | 509 | | /// successive calls to GetMemory/GetSpan and Advance.</remarks> |
| | | 510 | | internal Memory<byte> GetPlaceholderMemory(int size) |
| | 643 | 511 | | { |
| | 643 | 512 | | Debug.Assert(size > 0); |
| | 643 | 513 | | Memory<byte> placeholder = _bufferWriter.GetMemory(size)[0..size]; |
| | 643 | 514 | | Advance(size); |
| | 643 | 515 | | return placeholder; |
| | 643 | 516 | | } |
| | | 517 | | |
| | | 518 | | /// <summary>Gets the minimum number of bytes needed to encode a long value with the varint62 encoding as an |
| | | 519 | | /// exponent of 2.</summary> |
| | | 520 | | /// <param name="value">The value to encode.</param> |
| | | 521 | | /// <returns>N where 2^N is the number of bytes needed to encode value with Slice's varint62 encoding.</returns> |
| | | 522 | | private static int GetVarInt62EncodedSizeExponent(long value) |
| | 321 | 523 | | { |
| | 321 | 524 | | if (value < VarInt62MinValue || value > VarInt62MaxValue) |
| | 2 | 525 | | { |
| | 2 | 526 | | throw new ArgumentOutOfRangeException(nameof(value), $"The value '{value}' is out of the varint62 range."); |
| | | 527 | | } |
| | | 528 | | |
| | 319 | 529 | | return (value << 2) switch |
| | 319 | 530 | | { |
| | 620 | 531 | | long b when b >= sbyte.MinValue && b <= sbyte.MaxValue => 0, |
| | 20 | 532 | | long s when s >= short.MinValue && s <= short.MaxValue => 1, |
| | 26 | 533 | | long i when i >= int.MinValue && i <= int.MaxValue => 2, |
| | 6 | 534 | | _ => 3 |
| | 319 | 535 | | }; |
| | 319 | 536 | | } |
| | | 537 | | |
| | | 538 | | /// <summary>Gets the minimum number of bytes needed to encode a ulong value with the varuint62 encoding as an |
| | | 539 | | /// exponent of 2.</summary> |
| | | 540 | | /// <param name="value">The value to encode.</param> |
| | | 541 | | /// <returns>N where 2^N is the number of bytes needed to encode value with Slice's varuint62 encoding.</returns> |
| | | 542 | | private static int GetVarUInt62EncodedSizeExponent(ulong value) |
| | 195090 | 543 | | { |
| | 195090 | 544 | | if (value > VarUInt62MaxValue) |
| | 1 | 545 | | { |
| | 1 | 546 | | throw new ArgumentOutOfRangeException(nameof(value), $"The value '{value}' is out of the varuint62 range."); |
| | | 547 | | } |
| | | 548 | | |
| | 195089 | 549 | | return (value << 2) switch |
| | 195089 | 550 | | { |
| | 374087 | 551 | | ulong b when b <= byte.MaxValue => 0, |
| | 26295 | 552 | | ulong s when s <= ushort.MaxValue => 1, |
| | 11756 | 553 | | ulong i when i <= uint.MaxValue => 2, |
| | 18 | 554 | | _ => 3 |
| | 195089 | 555 | | }; |
| | 195089 | 556 | | } |
| | | 557 | | |
| | | 558 | | private void Advance(int count) |
| | 601174 | 559 | | { |
| | 601174 | 560 | | _bufferWriter.Advance(count); |
| | 601174 | 561 | | EncodedByteCount += count; |
| | 601174 | 562 | | } |
| | | 563 | | |
| | | 564 | | /// <summary>Encodes the header for a tagged field. Slice1 only.</summary> |
| | | 565 | | /// <param name="tag">The numeric tag associated with the field.</param> |
| | | 566 | | /// <param name="format">The tag format.</param> |
| | | 567 | | private void EncodeTaggedFieldHeader(int tag, TagFormat format) |
| | 91 | 568 | | { |
| | 91 | 569 | | Debug.Assert(Encoding == SliceEncoding.Slice1); |
| | 91 | 570 | | Debug.Assert(format != TagFormat.OptimizedVSize); // OptimizedVSize cannot be encoded |
| | | 571 | | |
| | 91 | 572 | | int v = (int)format; |
| | 91 | 573 | | if (tag < 30) |
| | 83 | 574 | | { |
| | 83 | 575 | | v |= tag << 3; |
| | 83 | 576 | | EncodeUInt8((byte)v); |
| | 83 | 577 | | } |
| | | 578 | | else |
| | 8 | 579 | | { |
| | 8 | 580 | | v |= 0x0F0; // tag = 30 |
| | 8 | 581 | | EncodeUInt8((byte)v); |
| | 8 | 582 | | EncodeSize(tag); |
| | 8 | 583 | | } |
| | | 584 | | |
| | 91 | 585 | | if (_classContext.Current.InstanceType != InstanceType.None) |
| | 63 | 586 | | { |
| | 63 | 587 | | _classContext.Current.SliceFlags |= SliceFlags.HasTaggedFields; |
| | 63 | 588 | | } |
| | 91 | 589 | | } |
| | | 590 | | } |