MCPcopy Index your code
hub / github.com/RustPython/RustPython / read_pylong

Function read_pylong

crates/compiler-core/src/marshal.rs:989–1015  ·  view source on GitHub ↗

Read a TYPE_LONG arbitrary-precision integer (base-2^15 digits).

(rdr: &mut R)

Source from the content-addressed store, hash-verified

987
988/// Read a TYPE_LONG arbitrary-precision integer (base-2^15 digits).
989pub fn read_pylong<R: Read>(rdr: &mut R) -> Result<BigInt> {
990 const MARSHAL_SHIFT: u32 = 15;
991 const MARSHAL_BASE: u32 = 1 << MARSHAL_SHIFT;
992 let n = read_i32(rdr)?;
993 if n == 0 {
994 return Ok(BigInt::from(0));
995 }
996 let negative = n < 0;
997 let num_digits = n.unsigned_abs() as usize;
998 let mut accum = BigInt::from(0);
999 let mut last_digit = 0u32;
1000 for i in 0..num_digits {
1001 let d = rdr.read_u16()? as u32;
1002 if d >= MARSHAL_BASE {
1003 return Err(MarshalError::InvalidBytecode);
1004 }
1005 last_digit = d;
1006 accum += BigInt::from(d) << (i as u32 * MARSHAL_SHIFT);
1007 }
1008 if num_digits > 0 && last_digit == 0 {
1009 return Err(MarshalError::InvalidBytecode);
1010 }
1011 if negative {
1012 accum = -accum;
1013 }
1014 Ok(accum)
1015}
1016
1017/// Read a text-encoded float (1-byte length + ASCII).
1018pub fn read_float_str<R: Read>(rdr: &mut R) -> Result<f64> {

Callers 1

deserialize_value_typedFunction · 0.85

Calls 3

read_i32Function · 0.85
read_u16Method · 0.80
ErrClass · 0.50

Tested by

no test coverage detected