Skip to content

Commit 194d9e4

Browse files
EgorBoCopilot
andcommitted
Avoid BigInteger polyfill, use private methods instead
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 41eee75 commit 194d9e4

2 files changed

Lines changed: 27 additions & 38 deletions

File tree

src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Polyfills.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Buffers.Binary;
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Numerics;
87
using System.Runtime.CompilerServices;
98
using System.Runtime.InteropServices;
109

@@ -20,42 +19,6 @@ internal static class DateTimeOffsetPolyfills
2019
}
2120
}
2221

23-
internal static class BigIntegerPolyfills
24-
{
25-
extension(BigInteger self)
26-
{
27-
public byte[] ToByteArray(bool isUnsigned, bool isBigEndian)
28-
{
29-
byte[] littleEndianBytes = self.ToByteArray();
30-
31-
if (littleEndianBytes.Length == 1)
32-
return littleEndianBytes;
33-
34-
Span<byte> bytesAsSpan = littleEndianBytes;
35-
36-
if (isBigEndian)
37-
bytesAsSpan.Reverse();
38-
39-
if (isUnsigned)
40-
{
41-
int start = 0;
42-
for (int i = 0; i < bytesAsSpan.Length; i++)
43-
{
44-
if (bytesAsSpan[i] == 0x00)
45-
start++;
46-
else
47-
break;
48-
}
49-
50-
if (start > 0)
51-
return bytesAsSpan.Slice(start).ToArray();
52-
}
53-
54-
return littleEndianBytes;
55-
}
56-
}
57-
}
58-
5922
internal static class DecimalPolyfills
6023
{
6124
extension(decimal)

src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Writer/CborWriter.Tag.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void WriteBigInteger(BigInteger value)
9393
{
9494
bool isUnsigned = value.Sign >= 0;
9595
BigInteger unsignedValue = isUnsigned ? value : -1 - value;
96-
byte[] unsignedBigEndianEncoding = unsignedValue.ToByteArray(isUnsigned: true, isBigEndian: true);
96+
byte[] unsignedBigEndianEncoding = CreateUnsignedBigEndianBytes(unsignedValue);
9797

9898
WriteTag(isUnsigned ? CborTag.UnsignedBigNum : CborTag.NegativeBigNum);
9999
WriteByteString(unsignedBigEndianEncoding);
@@ -207,5 +207,31 @@ public static decimal Reconstruct(decimal mantissa, long exponent)
207207
}
208208
}
209209
}
210+
211+
private static byte[] CreateUnsignedBigEndianBytes(BigInteger value)
212+
{
213+
#if NET
214+
return value.ToByteArray(isUnsigned: true, isBigEndian: true);
215+
#else
216+
byte[] littleEndianBytes = value.ToByteArray();
217+
218+
if (littleEndianBytes.Length == 1)
219+
return littleEndianBytes;
220+
221+
Span<byte> bytesAsSpan = littleEndianBytes;
222+
bytesAsSpan.Reverse();
223+
224+
int start = 0;
225+
for (int i = 0; i < bytesAsSpan.Length; i++)
226+
{
227+
if (bytesAsSpan[i] == 0x00)
228+
start++;
229+
else
230+
break;
231+
}
232+
233+
return start == 0 ? littleEndianBytes : bytesAsSpan.Slice(start).ToArray();
234+
#endif
235+
}
210236
}
211237
}

0 commit comments

Comments
 (0)