@@ -528,30 +528,20 @@ public static Bytes to_bytes(Int32 value, int length, string byteorder, bool sig
528528 return Bytes . Make ( res . ToArray ( ) ) ;
529529 }
530530
531- public static BigInteger from_bytes ( [ NotNull , BytesLike ] IList < byte > bytes , string byteorder , bool signed = false ) {
531+ public static BigInteger from_bytes ( CodeContext context , object bytes , [ NotNull ] string byteorder , bool signed = false ) {
532532 // TODO: signed should be a keyword only argument
533533 // TODO: return int when possible?
534+ // TODO: if called on a subclass of Extensible<BigInteger>, return that subclass
534535
535536 bool isLittle = byteorder == "little" ;
536537 if ( ! isLittle && byteorder != "big" ) throw PythonOps . ValueError ( "byteorder must be either 'little' or 'big'" ) ;
537538
538- return FromBytes ( bytes , isLittle , signed ) ;
539- }
540-
541- public static BigInteger from_bytes ( CodeContext context , object bytes , string byteorder , bool signed = false ) {
542- // TODO: signed should be a keyword only argument
543- // TODO: return int when possible?
544-
545- bool isLittle = byteorder == "little" ;
546- if ( ! isLittle && byteorder != "big" ) throw PythonOps . ValueError ( "byteorder must be either 'little' or 'big'" ) ;
547-
548- return FromBytes ( Bytes . FromObject ( context , bytes ) , isLittle , signed ) ;
549- }
550-
551- private static BigInteger FromBytes ( IList < byte > bytes , bool isLittle , bool signed ) {
552- if ( ! bytes . Any ( ) ) return 0 ;
539+ byte [ ] bytesArr = Bytes . FromObject ( context , bytes ) . UnsafeByteArray ;
540+ if ( bytesArr . Length == 0 ) return 0 ;
553541
554- byte [ ] bytesArr = bytes as byte [ ] ?? ( ( bytes is Bytes ) ? ( ( Bytes ) bytes ) . UnsafeByteArray : bytes . ToArray ( ) ) ;
542+ #if NETCOREAPP
543+ return new BigInteger ( bytesArr . AsSpan ( ) , isUnsigned : ! signed , isBigEndian : ! isLittle ) ;
544+ #else
555545
556546 if ( isLittle ) {
557547 bool msbSet = ( bytesArr [ bytesArr . Length - 1 ] & 0x80 ) == 0x80 ;
@@ -562,6 +552,7 @@ private static BigInteger FromBytes(IList<byte> bytes, bool isLittle, bool signe
562552 if ( ! msbSet ) return new BigInteger ( bytesArr . Reverse ( ) ) ;
563553 return new BigInteger ( bytesArr . Reverse ( ) . Concat ( Enumerable . Repeat < byte > ( signed ? ( byte ) 0xff : ( byte ) 0 , 1 ) ) . ToArray ( ) ) ;
564554 }
555+ #endif
565556 }
566557
567558 public static int __round__ ( int self ) {
0 commit comments