@@ -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