use alloc::vec::Vec; use core::cmp::Ordering::{Equal, Greater, Less}; use core::convert::TryFrom; use core::str::{self, FromStr}; use num_traits::{FromPrimitive, Num, One, ToPrimitive, Zero};
impl FromStr for BigInt { type Err = ParseBigIntError;
impl From<bool> for BigInt { fn from(x: bool) -> Self { if x {
One::one()
} else { Self::ZERO
}
}
}
#[inline] pub(super) fn from_signed_bytes_be(digits: &[u8]) -> BigInt { let sign = match digits.first() {
Some(v) if *v > 0x7f => Sign::Minus,
Some(_) => Sign::Plus,
None => return BigInt::ZERO,
};
if sign == Sign::Minus { // two's-complement the content to retrieve the magnitude letmut digits = Vec::from(digits);
twos_complement_be(&mut digits);
BigInt::from_biguint(sign, BigUint::from_bytes_be(&digits))
} else {
BigInt::from_biguint(sign, BigUint::from_bytes_be(digits))
}
}
#[inline] pub(super) fn from_signed_bytes_le(digits: &[u8]) -> BigInt { let sign = match digits.last() {
Some(v) if *v > 0x7f => Sign::Minus,
Some(_) => Sign::Plus,
None => return BigInt::ZERO,
};
if sign == Sign::Minus { // two's-complement the content to retrieve the magnitude letmut digits = Vec::from(digits);
twos_complement_le(&mut digits);
BigInt::from_biguint(sign, BigUint::from_bytes_le(&digits))
} else {
BigInt::from_biguint(sign, BigUint::from_bytes_le(digits))
}
}
#[inline] pub(super) fn to_signed_bytes_be(x: &BigInt) -> Vec<u8> { letmut bytes = x.data.to_bytes_be(); let first_byte = bytes.first().cloned().unwrap_or(0); if first_byte > 0x7f
&& !(first_byte == 0x80 && bytes.iter().skip(1).all(Zero::is_zero) && x.sign == Sign::Minus)
{ // msb used by magnitude, extend by 1 byte
bytes.insert(0, 0);
} if x.sign == Sign::Minus {
twos_complement_be(&mut bytes);
}
bytes
}
#[inline] pub(super) fn to_signed_bytes_le(x: &BigInt) -> Vec<u8> { letmut bytes = x.data.to_bytes_le(); let last_byte = bytes.last().cloned().unwrap_or(0); if last_byte > 0x7f
&& !(last_byte == 0x80
&& bytes.iter().rev().skip(1).all(Zero::is_zero)
&& x.sign == Sign::Minus)
{ // msb used by magnitude, extend by 1 byte
bytes.push(0);
} if x.sign == Sign::Minus {
twos_complement_le(&mut bytes);
}
bytes
}
/// Perform in-place two's complement of the given binary representation, /// in little-endian byte order. #[inline] fn twos_complement_le(digits: &mut [u8]) {
twos_complement(digits)
}
/// Perform in-place two's complement of the given binary representation /// in big-endian byte order. #[inline] fn twos_complement_be(digits: &mut [u8]) {
twos_complement(digits.iter_mut().rev())
}
/// Perform in-place two's complement of the given digit iterator /// starting from the least significant byte. #[inline] fn twos_complement<'a, I>(digits: I) where
I: IntoIterator<Item = &'a mut u8>,
{ letmut carry = true; for d in digits {
*d = !*d; if carry {
*d = d.wrapping_add(1);
carry = d.is_zero();
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.