/rust/registry/src/index.crates.io-6f17d22bba15001f/num-bigint-0.4.6/src/bigint/convert.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::Sign::{self, Minus, NoSign, Plus}; |
2 | | use super::{BigInt, ToBigInt}; |
3 | | |
4 | | use crate::TryFromBigIntError; |
5 | | use crate::{BigUint, ParseBigIntError, ToBigUint}; |
6 | | |
7 | | use alloc::vec::Vec; |
8 | | use core::cmp::Ordering::{Equal, Greater, Less}; |
9 | | use core::convert::TryFrom; |
10 | | use core::str::{self, FromStr}; |
11 | | use num_traits::{FromPrimitive, Num, One, ToPrimitive, Zero}; |
12 | | |
13 | | impl FromStr for BigInt { |
14 | | type Err = ParseBigIntError; |
15 | | |
16 | | #[inline] |
17 | 0 | fn from_str(s: &str) -> Result<BigInt, ParseBigIntError> { |
18 | 0 | BigInt::from_str_radix(s, 10) |
19 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::str::traits::FromStr>::from_str Unexecuted instantiation: <num_bigint::bigint::BigInt as core::str::traits::FromStr>::from_str |
20 | | } |
21 | | |
22 | | impl Num for BigInt { |
23 | | type FromStrRadixErr = ParseBigIntError; |
24 | | |
25 | | /// Creates and initializes a [`BigInt`]. |
26 | | #[inline] |
27 | 30.1k | fn from_str_radix(mut s: &str, radix: u32) -> Result<BigInt, ParseBigIntError> { |
28 | 30.1k | let sign = if let Some(tail) = s.strip_prefix('-') { |
29 | 23.3k | if !tail.starts_with('+') { |
30 | 23.3k | s = tail |
31 | 0 | } |
32 | 23.3k | Minus |
33 | | } else { |
34 | 6.83k | Plus |
35 | | }; |
36 | 30.1k | let bu = BigUint::from_str_radix(s, radix)?; |
37 | 29.9k | Ok(BigInt::from_biguint(sign, bu)) |
38 | 30.1k | } <num_bigint::bigint::BigInt as num_traits::Num>::from_str_radix Line | Count | Source | 27 | 30.1k | fn from_str_radix(mut s: &str, radix: u32) -> Result<BigInt, ParseBigIntError> { | 28 | 30.1k | let sign = if let Some(tail) = s.strip_prefix('-') { | 29 | 23.3k | if !tail.starts_with('+') { | 30 | 23.3k | s = tail | 31 | 0 | } | 32 | 23.3k | Minus | 33 | | } else { | 34 | 6.83k | Plus | 35 | | }; | 36 | 30.1k | let bu = BigUint::from_str_radix(s, radix)?; | 37 | 29.9k | Ok(BigInt::from_biguint(sign, bu)) | 38 | 30.1k | } |
Unexecuted instantiation: <num_bigint::bigint::BigInt as num_traits::Num>::from_str_radix |
39 | | } |
40 | | |
41 | | impl ToPrimitive for BigInt { |
42 | | #[inline] |
43 | 0 | fn to_i64(&self) -> Option<i64> { |
44 | 0 | match self.sign { |
45 | 0 | Plus => self.data.to_i64(), |
46 | 0 | NoSign => Some(0), |
47 | | Minus => { |
48 | 0 | let n = self.data.to_u64()?; |
49 | 0 | let m: u64 = 1 << 63; |
50 | 0 | match n.cmp(&m) { |
51 | 0 | Less => Some(-(n as i64)), |
52 | 0 | Equal => Some(i64::MIN), |
53 | 0 | Greater => None, |
54 | | } |
55 | | } |
56 | | } |
57 | 0 | } |
58 | | |
59 | | #[inline] |
60 | 0 | fn to_i128(&self) -> Option<i128> { |
61 | 0 | match self.sign { |
62 | 0 | Plus => self.data.to_i128(), |
63 | 0 | NoSign => Some(0), |
64 | | Minus => { |
65 | 0 | let n = self.data.to_u128()?; |
66 | 0 | let m: u128 = 1 << 127; |
67 | 0 | match n.cmp(&m) { |
68 | 0 | Less => Some(-(n as i128)), |
69 | 0 | Equal => Some(i128::MIN), |
70 | 0 | Greater => None, |
71 | | } |
72 | | } |
73 | | } |
74 | 0 | } |
75 | | |
76 | | #[inline] |
77 | 0 | fn to_u64(&self) -> Option<u64> { |
78 | 0 | match self.sign { |
79 | 0 | Plus => self.data.to_u64(), |
80 | 0 | NoSign => Some(0), |
81 | 0 | Minus => None, |
82 | | } |
83 | 0 | } |
84 | | |
85 | | #[inline] |
86 | 0 | fn to_u128(&self) -> Option<u128> { |
87 | 0 | match self.sign { |
88 | 0 | Plus => self.data.to_u128(), |
89 | 0 | NoSign => Some(0), |
90 | 0 | Minus => None, |
91 | | } |
92 | 0 | } |
93 | | |
94 | | #[inline] |
95 | 0 | fn to_f32(&self) -> Option<f32> { |
96 | 0 | let n = self.data.to_f32()?; |
97 | 0 | Some(if self.sign == Minus { -n } else { n }) |
98 | 0 | } |
99 | | |
100 | | #[inline] |
101 | 0 | fn to_f64(&self) -> Option<f64> { |
102 | 0 | let n = self.data.to_f64()?; |
103 | 0 | Some(if self.sign == Minus { -n } else { n }) |
104 | 0 | } |
105 | | } |
106 | | |
107 | | macro_rules! impl_try_from_bigint { |
108 | | ($T:ty, $to_ty:path) => { |
109 | | impl TryFrom<&BigInt> for $T { |
110 | | type Error = TryFromBigIntError<()>; |
111 | | |
112 | | #[inline] |
113 | 0 | fn try_from(value: &BigInt) -> Result<$T, TryFromBigIntError<()>> { |
114 | 0 | $to_ty(value).ok_or(TryFromBigIntError::new(())) |
115 | 0 | } Unexecuted instantiation: <u8 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u128 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<&num_bigint::bigint::BigInt>>::try_from |
116 | | } |
117 | | |
118 | | impl TryFrom<BigInt> for $T { |
119 | | type Error = TryFromBigIntError<BigInt>; |
120 | | |
121 | | #[inline] |
122 | 0 | fn try_from(value: BigInt) -> Result<$T, TryFromBigIntError<BigInt>> { |
123 | 0 | <$T>::try_from(&value).map_err(|_| TryFromBigIntError::new(value)) Unexecuted instantiation: <u8 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <u16 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <u32 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <u64 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <usize as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <u128 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <i8 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <i16 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <i32 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <i64 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <isize as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} Unexecuted instantiation: <i128 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from::{closure#0} |
124 | 0 | } Unexecuted instantiation: <u8 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <u128 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<num_bigint::bigint::BigInt>>::try_from |
125 | | } |
126 | | }; |
127 | | } |
128 | | |
129 | | impl_try_from_bigint!(u8, ToPrimitive::to_u8); |
130 | | impl_try_from_bigint!(u16, ToPrimitive::to_u16); |
131 | | impl_try_from_bigint!(u32, ToPrimitive::to_u32); |
132 | | impl_try_from_bigint!(u64, ToPrimitive::to_u64); |
133 | | impl_try_from_bigint!(usize, ToPrimitive::to_usize); |
134 | | impl_try_from_bigint!(u128, ToPrimitive::to_u128); |
135 | | |
136 | | impl_try_from_bigint!(i8, ToPrimitive::to_i8); |
137 | | impl_try_from_bigint!(i16, ToPrimitive::to_i16); |
138 | | impl_try_from_bigint!(i32, ToPrimitive::to_i32); |
139 | | impl_try_from_bigint!(i64, ToPrimitive::to_i64); |
140 | | impl_try_from_bigint!(isize, ToPrimitive::to_isize); |
141 | | impl_try_from_bigint!(i128, ToPrimitive::to_i128); |
142 | | |
143 | | impl FromPrimitive for BigInt { |
144 | | #[inline] |
145 | 0 | fn from_i64(n: i64) -> Option<BigInt> { |
146 | 0 | Some(BigInt::from(n)) |
147 | 0 | } |
148 | | |
149 | | #[inline] |
150 | 0 | fn from_i128(n: i128) -> Option<BigInt> { |
151 | 0 | Some(BigInt::from(n)) |
152 | 0 | } |
153 | | |
154 | | #[inline] |
155 | 0 | fn from_u64(n: u64) -> Option<BigInt> { |
156 | 0 | Some(BigInt::from(n)) |
157 | 0 | } |
158 | | |
159 | | #[inline] |
160 | 0 | fn from_u128(n: u128) -> Option<BigInt> { |
161 | 0 | Some(BigInt::from(n)) |
162 | 0 | } |
163 | | |
164 | | #[inline] |
165 | 0 | fn from_f64(n: f64) -> Option<BigInt> { |
166 | 0 | if n >= 0.0 { |
167 | 0 | BigUint::from_f64(n).map(BigInt::from) |
168 | | } else { |
169 | 0 | let x = BigUint::from_f64(-n)?; |
170 | 0 | Some(-BigInt::from(x)) |
171 | | } |
172 | 0 | } |
173 | | } |
174 | | |
175 | | impl From<i64> for BigInt { |
176 | | #[inline] |
177 | 0 | fn from(n: i64) -> Self { |
178 | 0 | if n >= 0 { |
179 | 0 | BigInt::from(n as u64) |
180 | | } else { |
181 | 0 | let u = u64::MAX - (n as u64) + 1; |
182 | 0 | BigInt { |
183 | 0 | sign: Minus, |
184 | 0 | data: BigUint::from(u), |
185 | 0 | } |
186 | | } |
187 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i64>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i64>>::from |
188 | | } |
189 | | |
190 | | impl From<i128> for BigInt { |
191 | | #[inline] |
192 | 0 | fn from(n: i128) -> Self { |
193 | 0 | if n >= 0 { |
194 | 0 | BigInt::from(n as u128) |
195 | | } else { |
196 | 0 | let u = u128::MAX - (n as u128) + 1; |
197 | 0 | BigInt { |
198 | 0 | sign: Minus, |
199 | 0 | data: BigUint::from(u), |
200 | 0 | } |
201 | | } |
202 | 0 | } |
203 | | } |
204 | | |
205 | | macro_rules! impl_bigint_from_int { |
206 | | ($T:ty) => { |
207 | | impl From<$T> for BigInt { |
208 | | #[inline] |
209 | 0 | fn from(n: $T) -> Self { |
210 | 0 | BigInt::from(n as i64) |
211 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<i32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<isize>>::from |
212 | | } |
213 | | }; |
214 | | } |
215 | | |
216 | | impl_bigint_from_int!(i8); |
217 | | impl_bigint_from_int!(i16); |
218 | | impl_bigint_from_int!(i32); |
219 | | impl_bigint_from_int!(isize); |
220 | | |
221 | | impl From<u64> for BigInt { |
222 | | #[inline] |
223 | 0 | fn from(n: u64) -> Self { |
224 | 0 | if n > 0 { |
225 | 0 | BigInt { |
226 | 0 | sign: Plus, |
227 | 0 | data: BigUint::from(n), |
228 | 0 | } |
229 | | } else { |
230 | 0 | Self::ZERO |
231 | | } |
232 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u64>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u64>>::from |
233 | | } |
234 | | |
235 | | impl From<u128> for BigInt { |
236 | | #[inline] |
237 | 0 | fn from(n: u128) -> Self { |
238 | 0 | if n > 0 { |
239 | 0 | BigInt { |
240 | 0 | sign: Plus, |
241 | 0 | data: BigUint::from(n), |
242 | 0 | } |
243 | | } else { |
244 | 0 | Self::ZERO |
245 | | } |
246 | 0 | } |
247 | | } |
248 | | |
249 | | macro_rules! impl_bigint_from_uint { |
250 | | ($T:ty) => { |
251 | | impl From<$T> for BigInt { |
252 | | #[inline] |
253 | 0 | fn from(n: $T) -> Self { |
254 | 0 | BigInt::from(n as u64) |
255 | 0 | } Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u32>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u8>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<u16>>::from Unexecuted instantiation: <num_bigint::bigint::BigInt as core::convert::From<usize>>::from |
256 | | } |
257 | | }; |
258 | | } |
259 | | |
260 | | impl_bigint_from_uint!(u8); |
261 | | impl_bigint_from_uint!(u16); |
262 | | impl_bigint_from_uint!(u32); |
263 | | impl_bigint_from_uint!(usize); |
264 | | |
265 | | impl From<BigUint> for BigInt { |
266 | | #[inline] |
267 | 0 | fn from(n: BigUint) -> Self { |
268 | 0 | if n.is_zero() { |
269 | 0 | Self::ZERO |
270 | | } else { |
271 | 0 | BigInt { |
272 | 0 | sign: Plus, |
273 | 0 | data: n, |
274 | 0 | } |
275 | | } |
276 | 0 | } |
277 | | } |
278 | | |
279 | | impl ToBigInt for BigInt { |
280 | | #[inline] |
281 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
282 | 0 | Some(self.clone()) |
283 | 0 | } |
284 | | } |
285 | | |
286 | | impl ToBigInt for BigUint { |
287 | | #[inline] |
288 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
289 | 0 | if self.is_zero() { |
290 | 0 | Some(BigInt::ZERO) |
291 | | } else { |
292 | 0 | Some(BigInt { |
293 | 0 | sign: Plus, |
294 | 0 | data: self.clone(), |
295 | 0 | }) |
296 | | } |
297 | 0 | } |
298 | | } |
299 | | |
300 | | impl ToBigUint for BigInt { |
301 | | #[inline] |
302 | 0 | fn to_biguint(&self) -> Option<BigUint> { |
303 | 0 | match self.sign() { |
304 | 0 | Plus => Some(self.data.clone()), |
305 | 0 | NoSign => Some(BigUint::ZERO), |
306 | 0 | Minus => None, |
307 | | } |
308 | 0 | } |
309 | | } |
310 | | |
311 | | impl TryFrom<&BigInt> for BigUint { |
312 | | type Error = TryFromBigIntError<()>; |
313 | | |
314 | | #[inline] |
315 | 0 | fn try_from(value: &BigInt) -> Result<BigUint, TryFromBigIntError<()>> { |
316 | 0 | value |
317 | 0 | .to_biguint() |
318 | 0 | .ok_or_else(|| TryFromBigIntError::new(())) |
319 | 0 | } |
320 | | } |
321 | | |
322 | | impl TryFrom<BigInt> for BigUint { |
323 | | type Error = TryFromBigIntError<BigInt>; |
324 | | |
325 | | #[inline] |
326 | 0 | fn try_from(value: BigInt) -> Result<BigUint, TryFromBigIntError<BigInt>> { |
327 | 0 | if value.sign() == Sign::Minus { |
328 | 0 | Err(TryFromBigIntError::new(value)) |
329 | | } else { |
330 | 0 | Ok(value.data) |
331 | | } |
332 | 0 | } |
333 | | } |
334 | | |
335 | | macro_rules! impl_to_bigint { |
336 | | ($T:ty, $from_ty:path) => { |
337 | | impl ToBigInt for $T { |
338 | | #[inline] |
339 | 0 | fn to_bigint(&self) -> Option<BigInt> { |
340 | 0 | $from_ty(*self) |
341 | 0 | } Unexecuted instantiation: <isize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <i128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <usize as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u8 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u16 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u64 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <u128 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f32 as num_bigint::bigint::ToBigInt>::to_bigint Unexecuted instantiation: <f64 as num_bigint::bigint::ToBigInt>::to_bigint |
342 | | } |
343 | | }; |
344 | | } |
345 | | |
346 | | impl_to_bigint!(isize, FromPrimitive::from_isize); |
347 | | impl_to_bigint!(i8, FromPrimitive::from_i8); |
348 | | impl_to_bigint!(i16, FromPrimitive::from_i16); |
349 | | impl_to_bigint!(i32, FromPrimitive::from_i32); |
350 | | impl_to_bigint!(i64, FromPrimitive::from_i64); |
351 | | impl_to_bigint!(i128, FromPrimitive::from_i128); |
352 | | |
353 | | impl_to_bigint!(usize, FromPrimitive::from_usize); |
354 | | impl_to_bigint!(u8, FromPrimitive::from_u8); |
355 | | impl_to_bigint!(u16, FromPrimitive::from_u16); |
356 | | impl_to_bigint!(u32, FromPrimitive::from_u32); |
357 | | impl_to_bigint!(u64, FromPrimitive::from_u64); |
358 | | impl_to_bigint!(u128, FromPrimitive::from_u128); |
359 | | |
360 | | impl_to_bigint!(f32, FromPrimitive::from_f32); |
361 | | impl_to_bigint!(f64, FromPrimitive::from_f64); |
362 | | |
363 | | impl From<bool> for BigInt { |
364 | 0 | fn from(x: bool) -> Self { |
365 | 0 | if x { |
366 | 0 | One::one() |
367 | | } else { |
368 | 0 | Self::ZERO |
369 | | } |
370 | 0 | } |
371 | | } |
372 | | |
373 | | #[inline] |
374 | 0 | pub(super) fn from_signed_bytes_be(digits: &[u8]) -> BigInt { |
375 | 0 | let sign = match digits.first() { |
376 | 0 | Some(v) if *v > 0x7f => Sign::Minus, |
377 | 0 | Some(_) => Sign::Plus, |
378 | 0 | None => return BigInt::ZERO, |
379 | | }; |
380 | | |
381 | 0 | if sign == Sign::Minus { |
382 | | // two's-complement the content to retrieve the magnitude |
383 | 0 | let mut digits = Vec::from(digits); |
384 | 0 | twos_complement_be(&mut digits); |
385 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_be(&digits)) |
386 | | } else { |
387 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_be(digits)) |
388 | | } |
389 | 0 | } |
390 | | |
391 | | #[inline] |
392 | 0 | pub(super) fn from_signed_bytes_le(digits: &[u8]) -> BigInt { |
393 | 0 | let sign = match digits.last() { |
394 | 0 | Some(v) if *v > 0x7f => Sign::Minus, |
395 | 0 | Some(_) => Sign::Plus, |
396 | 0 | None => return BigInt::ZERO, |
397 | | }; |
398 | | |
399 | 0 | if sign == Sign::Minus { |
400 | | // two's-complement the content to retrieve the magnitude |
401 | 0 | let mut digits = Vec::from(digits); |
402 | 0 | twos_complement_le(&mut digits); |
403 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_le(&digits)) |
404 | | } else { |
405 | 0 | BigInt::from_biguint(sign, BigUint::from_bytes_le(digits)) |
406 | | } |
407 | 0 | } |
408 | | |
409 | | #[inline] |
410 | 0 | pub(super) fn to_signed_bytes_be(x: &BigInt) -> Vec<u8> { |
411 | 0 | let mut bytes = x.data.to_bytes_be(); |
412 | 0 | let first_byte = bytes.first().cloned().unwrap_or(0); |
413 | 0 | if first_byte > 0x7f |
414 | 0 | && !(first_byte == 0x80 && bytes.iter().skip(1).all(Zero::is_zero) && x.sign == Sign::Minus) |
415 | 0 | { |
416 | 0 | // msb used by magnitude, extend by 1 byte |
417 | 0 | bytes.insert(0, 0); |
418 | 0 | } |
419 | 0 | if x.sign == Sign::Minus { |
420 | 0 | twos_complement_be(&mut bytes); |
421 | 0 | } |
422 | 0 | bytes |
423 | 0 | } |
424 | | |
425 | | #[inline] |
426 | 0 | pub(super) fn to_signed_bytes_le(x: &BigInt) -> Vec<u8> { |
427 | 0 | let mut bytes = x.data.to_bytes_le(); |
428 | 0 | let last_byte = bytes.last().cloned().unwrap_or(0); |
429 | 0 | if last_byte > 0x7f |
430 | 0 | && !(last_byte == 0x80 |
431 | 0 | && bytes.iter().rev().skip(1).all(Zero::is_zero) |
432 | 0 | && x.sign == Sign::Minus) |
433 | 0 | { |
434 | 0 | // msb used by magnitude, extend by 1 byte |
435 | 0 | bytes.push(0); |
436 | 0 | } |
437 | 0 | if x.sign == Sign::Minus { |
438 | 0 | twos_complement_le(&mut bytes); |
439 | 0 | } |
440 | 0 | bytes |
441 | 0 | } |
442 | | |
443 | | /// Perform in-place two's complement of the given binary representation, |
444 | | /// in little-endian byte order. |
445 | | #[inline] |
446 | 0 | fn twos_complement_le(digits: &mut [u8]) { |
447 | 0 | twos_complement(digits) |
448 | 0 | } |
449 | | |
450 | | /// Perform in-place two's complement of the given binary representation |
451 | | /// in big-endian byte order. |
452 | | #[inline] |
453 | 0 | fn twos_complement_be(digits: &mut [u8]) { |
454 | 0 | twos_complement(digits.iter_mut().rev()) |
455 | 0 | } |
456 | | |
457 | | /// Perform in-place two's complement of the given digit iterator |
458 | | /// starting from the least significant byte. |
459 | | #[inline] |
460 | 0 | fn twos_complement<'a, I>(digits: I) |
461 | 0 | where |
462 | 0 | I: IntoIterator<Item = &'a mut u8>, |
463 | 0 | { |
464 | 0 | let mut carry = true; |
465 | 0 | for d in digits { |
466 | 0 | *d = !*d; |
467 | 0 | if carry { |
468 | 0 | *d = d.wrapping_add(1); |
469 | 0 | carry = d.is_zero(); |
470 | 0 | } |
471 | | } |
472 | 0 | } Unexecuted instantiation: num_bigint::bigint::convert::twos_complement::<core::iter::adapters::rev::Rev<core::slice::iter::IterMut<u8>>> Unexecuted instantiation: num_bigint::bigint::convert::twos_complement::<&mut [u8]> |