Read a TYPE_LONG arbitrary-precision integer (base-2^15 digits).
(rdr: &mut R)
| 987 | |
| 988 | /// Read a TYPE_LONG arbitrary-precision integer (base-2^15 digits). |
| 989 | pub 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). |
| 1018 | pub fn read_float_str<R: Read>(rdr: &mut R) -> Result<f64> { |
no test coverage detected