MCPcopy Create free account
hub / github.com/apache/fory / Write

Method Write

csharp/src/Fory/DecimalSerializer.cs:101–123  ·  view source on GitHub ↗
(ByteWriter buffer, int scale, BigInteger unscaled)

Source from the content-addressed store, hash-verified

99 private static readonly BigInteger LongMax = long.MaxValue;
100
101 public static void Write(ByteWriter buffer, int scale, BigInteger unscaled)
102 {
103 buffer.WriteVarInt32(scale);
104 if (CanUseSmallEncoding(unscaled))
105 {
106 long smallValue = (long)unscaled;
107 ulong zigzag = EncodeZigZag64(smallValue);
108 buffer.WriteVarUInt64(zigzag << 1);
109 return;
110 }
111
112 BigInteger magnitude = BigInteger.Abs(unscaled);
113 if (magnitude.IsZero)
114 {
115 throw new InvalidDataException("zero must use the small decimal encoding");
116 }
117
118 byte[] magnitudeBytes = magnitude.ToByteArray(isUnsigned: true, isBigEndian: false);
119 ulong meta = ((ulong)magnitudeBytes.Length << 1) | (unscaled.Sign < 0 ? 1UL : 0UL);
120 ulong header = (meta << 1) | 1UL;
121 buffer.WriteVarUInt64(header);
122 buffer.WriteBytes(magnitudeBytes);
123 }
124
125 public static (int Scale, BigInteger Unscaled) Read(ByteReader buffer)
126 {

Callers

nothing calls this directly

Calls 4

WriteVarInt32Method · 0.80
WriteVarUInt64Method · 0.80
AbsMethod · 0.80
WriteBytesMethod · 0.45

Tested by

no test coverage detected