|
| 1 | +//! Code for num_traits |
| 2 | +
|
| 3 | +use num_traits::{Num, FromPrimitive, ToPrimitive, AsPrimitive}; |
| 4 | +use num_bigint::{BigInt, Sign, ToBigInt}; |
| 5 | + |
| 6 | +use stdlib::str::FromStr; |
| 7 | +use stdlib::string::{String, ToString}; |
| 8 | +use stdlib::convert::TryFrom; |
| 9 | +use stdlib::ops::Neg; |
| 10 | + |
| 11 | +use crate::BigDecimal; |
| 12 | +use crate::ParseBigDecimalError; |
| 13 | + |
| 14 | +#[cfg(not(feature = "std"))] |
| 15 | +// f64::powi is only available in std, no_std must use libm |
| 16 | +fn powi(x: f64, n: f64) -> f64 { |
| 17 | + libm::pow(x, n) |
| 18 | +} |
| 19 | + |
| 20 | +#[cfg(feature = "std")] |
| 21 | +fn powi(x: f64, n: i32) -> f64 { |
| 22 | + x.powi(n) |
| 23 | +} |
| 24 | + |
| 25 | +impl Num for BigDecimal { |
| 26 | + type FromStrRadixErr = ParseBigDecimalError; |
| 27 | + |
| 28 | + /// Creates and initializes a BigDecimal. |
| 29 | + #[inline] |
| 30 | + fn from_str_radix(s: &str, radix: u32) -> Result<BigDecimal, ParseBigDecimalError> { |
| 31 | + if radix != 10 { |
| 32 | + return Err(ParseBigDecimalError::Other(String::from( |
| 33 | + "The radix for decimal MUST be 10", |
| 34 | + ))); |
| 35 | + } |
| 36 | + |
| 37 | + let exp_separator: &[_] = &['e', 'E']; |
| 38 | + |
| 39 | + // split slice into base and exponent parts |
| 40 | + let (base_part, exponent_value) = match s.find(exp_separator) { |
| 41 | + // exponent defaults to 0 if (e|E) not found |
| 42 | + None => (s, 0), |
| 43 | + |
| 44 | + // split and parse exponent field |
| 45 | + Some(loc) => { |
| 46 | + // slice up to `loc` and 1 after to skip the 'e' char |
| 47 | + let (base, exp) = (&s[..loc], &s[loc + 1..]); |
| 48 | + |
| 49 | + // special consideration for rust 1.0.0 which would not |
| 50 | + // parse a leading '+' |
| 51 | + let exp = match exp.chars().next() { |
| 52 | + Some('+') => &exp[1..], |
| 53 | + _ => exp, |
| 54 | + }; |
| 55 | + |
| 56 | + (base, i64::from_str(exp)?) |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + // TEMPORARY: Test for emptiness - remove once BigInt supports similar error |
| 61 | + if base_part.is_empty() { |
| 62 | + return Err(ParseBigDecimalError::Empty); |
| 63 | + } |
| 64 | + |
| 65 | + // split decimal into a digit string and decimal-point offset |
| 66 | + let (digits, decimal_offset): (String, _) = match base_part.find('.') { |
| 67 | + // No dot! pass directly to BigInt |
| 68 | + None => (base_part.to_string(), 0), |
| 69 | + |
| 70 | + // decimal point found - necessary copy into new string buffer |
| 71 | + Some(loc) => { |
| 72 | + // split into leading and trailing digits |
| 73 | + let (lead, trail) = (&base_part[..loc], &base_part[loc + 1..]); |
| 74 | + |
| 75 | + // copy all leading characters into 'digits' string |
| 76 | + let mut digits = String::from(lead); |
| 77 | + |
| 78 | + // copy all trailing characters after '.' into the digits string |
| 79 | + digits.push_str(trail); |
| 80 | + |
| 81 | + // count number of trailing digits |
| 82 | + let trail_digits = trail.chars().filter(|c| *c != '_').count(); |
| 83 | + |
| 84 | + (digits, trail_digits as i64) |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + let scale = decimal_offset - exponent_value; |
| 89 | + let big_int = BigInt::from_str_radix(&digits, radix)?; |
| 90 | + |
| 91 | + Ok(BigDecimal::new(big_int, scale)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl ToPrimitive for BigDecimal { |
| 96 | + fn to_i64(&self) -> Option<i64> { |
| 97 | + match self.sign() { |
| 98 | + Sign::Minus | Sign::Plus => self.with_scale(0).int_val.to_i64(), |
| 99 | + Sign::NoSign => Some(0), |
| 100 | + } |
| 101 | + } |
| 102 | + fn to_i128(&self) -> Option<i128> { |
| 103 | + match self.sign() { |
| 104 | + Sign::Minus | Sign::Plus => self.with_scale(0).int_val.to_i128(), |
| 105 | + Sign::NoSign => Some(0), |
| 106 | + } |
| 107 | + } |
| 108 | + fn to_u64(&self) -> Option<u64> { |
| 109 | + match self.sign() { |
| 110 | + Sign::Plus => self.with_scale(0).int_val.to_u64(), |
| 111 | + Sign::NoSign => Some(0), |
| 112 | + Sign::Minus => None, |
| 113 | + } |
| 114 | + } |
| 115 | + fn to_u128(&self) -> Option<u128> { |
| 116 | + match self.sign() { |
| 117 | + Sign::Plus => self.with_scale(0).int_val.to_u128(), |
| 118 | + Sign::NoSign => Some(0), |
| 119 | + Sign::Minus => None, |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn to_f64(&self) -> Option<f64> { |
| 124 | + self.int_val.to_f64().map(|x| x * powi(10f64, self.scale.neg().as_())) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +impl FromPrimitive for BigDecimal { |
| 130 | + #[inline] |
| 131 | + fn from_i64(n: i64) -> Option<Self> { |
| 132 | + Some(BigDecimal::from(n)) |
| 133 | + } |
| 134 | + |
| 135 | + #[inline] |
| 136 | + fn from_u64(n: u64) -> Option<Self> { |
| 137 | + Some(BigDecimal::from(n)) |
| 138 | + } |
| 139 | + |
| 140 | + #[inline] |
| 141 | + fn from_i128(n: i128) -> Option<Self> { |
| 142 | + Some(BigDecimal::from(n)) |
| 143 | + } |
| 144 | + |
| 145 | + #[inline] |
| 146 | + fn from_u128(n: u128) -> Option<Self> { |
| 147 | + Some(BigDecimal::from(n)) |
| 148 | + } |
| 149 | + |
| 150 | + #[inline] |
| 151 | + fn from_f32(n: f32) -> Option<Self> { |
| 152 | + BigDecimal::try_from(n).ok() |
| 153 | + } |
| 154 | + |
| 155 | + #[inline] |
| 156 | + fn from_f64(n: f64) -> Option<Self> { |
| 157 | + BigDecimal::try_from(n).ok() |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl ToBigInt for BigDecimal { |
| 162 | + fn to_bigint(&self) -> Option<BigInt> { |
| 163 | + Some(self.with_scale(0).int_val) |
| 164 | + } |
| 165 | +} |
0 commit comments