Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 254ecc0

Browse files
committed
Use real AsReadOnlySpan rather than workaround
1 parent ed36542 commit 254ecc0

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

src/System.Runtime.Numerics/src/System.Runtime.Numerics.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
<Reference Include="System.Diagnostics.Contracts" />
3535
<Reference Include="System.Diagnostics.Debug" />
3636
<Reference Include="System.Diagnostics.Tools" />
37+
<Reference Include="System.Memory" />
3738
<Reference Include="System.Resources.ResourceManager" />
3839
<Reference Include="System.Runtime" />
3940
<Reference Include="System.Runtime.Extensions" />
4041
</ItemGroup>
4142
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
42-
</Project>
43+
</Project>

src/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ internal static bool TryParseBigInteger(string value, NumberStyles style, Number
330330
return false;
331331
}
332332

333-
return TryParseBigInteger(AsReadOnlySpan(value), style, info, out result);
333+
return TryParseBigInteger(value.AsReadOnlySpan(), style, info, out result);
334334
}
335335

336336
internal static bool TryParseBigInteger(ReadOnlySpan<char> value, NumberStyles style, NumberFormatInfo info, out BigInteger result)
@@ -371,18 +371,7 @@ internal static BigInteger ParseBigInteger(string value, NumberStyles style, Num
371371
throw new ArgumentNullException(nameof(value));
372372
}
373373

374-
return ParseBigInteger(AsReadOnlySpan(value), style, info);
375-
}
376-
377-
// TODO #22688: Remove this and replace it with the real AsReadOnlySpan extension
378-
// method from System.Memory once the System.Memory package is marked stable
379-
// and the package validation system allows us to take a dependency on it.
380-
private static unsafe ReadOnlySpan<char> AsReadOnlySpan(string s)
381-
{
382-
fixed (char* c = s)
383-
{
384-
return new ReadOnlySpan<char>(c, s.Length);
385-
}
374+
return ParseBigInteger(value.AsReadOnlySpan(), style, info);
386375
}
387376

388377
internal static BigInteger ParseBigInteger(ReadOnlySpan<char> value, NumberStyles style, NumberFormatInfo info)
@@ -517,19 +506,10 @@ internal static char ParseFormatSpecifier(string format, out int digits)
517506
private static string FormatBigIntegerToHex(BigInteger value, char format, int digits, NumberFormatInfo info)
518507
{
519508
// Get the bytes that make up the BigInteger.
520-
int byteCount = value.GetByteCount();
521-
Span<byte> bits = stackalloc byte[0]; // TODO: Remove the initialization once C# compiler is fixed to allow it.
522-
if (byteCount <= 128) // arbitrary limit to switch from stack to heap
523-
{
524-
bits = stackalloc byte[byteCount];
525-
bool formatted = value.TryWriteBytes(bits, out int bytesWritten);
526-
Debug.Assert(formatted);
527-
Debug.Assert(bytesWritten == byteCount);
528-
}
529-
else
530-
{
531-
bits = value.ToByteArray();
532-
}
509+
Span<byte> bits = stackalloc byte[128]; // arbitrary limit to switch from stack to heap
510+
bits = value.TryWriteBytes(bits, out int bytesWritten) ?
511+
bits.Slice(0, bytesWritten) :
512+
value.ToByteArray();
533513

534514
StringBuilder sb = new StringBuilder();
535515
string fmt = null;

0 commit comments

Comments
 (0)