| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::fmt; |
| 6 |
use core::str::FromStr; |
| 7 |
|
| 8 |
use crate::Decimal; |
| 9 |
use crate::FixedInteger; |
| 10 |
use crate::ParseError; |
| 11 |
|
| 12 |
/// A struct containing a [`Decimal`] significand together with an exponent, representing a |
| 13 |
/// number written in scientific notation, such as 1.729×10³. |
| 14 |
/// |
| 15 |
/// This structure represents any 0s shown in the significand and exponent, |
| 16 |
/// and an optional sign for both the significand and the exponent. |
| 17 |
#[derive(Debug, Clone, PartialEq)] |
| 18 |
pub struct ScientificDecimal { |
| 19 |
significand: Decimal, |
| 20 |
exponent: FixedInteger, |
| 21 |
} |
| 22 |
|
| 23 |
impl ScientificDecimal { |
| 24 |
/// Creates a [`ScientificDecimal`] from parts. |
| 25 |
pub fn from(significand: Decimal, exponent: FixedInteger) -> Self { |
| 26 |
ScientificDecimal { |
| 27 |
significand, |
| 28 |
exponent, |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/// Render the [`ScientificDecimal`] as a string of ASCII digits with a possible decimal point, |
| 34 |
/// followed by the letter 'e', and the exponent. |
| 35 |
/// |
| 36 |
/// # Examples |
| 37 |
/// |
| 38 |
/// ``` |
| 39 |
/// # use fixed_decimal::Decimal; |
| 40 |
/// # use fixed_decimal::FixedInteger; |
| 41 |
/// # use fixed_decimal::ScientificDecimal; |
| 42 |
/// # use std::str::FromStr; |
| 43 |
/// # use writeable::assert_writeable_eq; |
| 44 |
/// # |
| 45 |
/// assert_writeable_eq!( |
| 46 |
/// ScientificDecimal::from( |
| 47 |
/// { |
| 48 |
/// let mut dec = Decimal::from(1729u32); |
| 49 |
/// dec.multiply_pow10(-3); |
| 50 |
/// dec |
| 51 |
/// }, |
| 52 |
/// FixedInteger::from(3) |
| 53 |
/// ), |
| 54 |
/// "1.729e3" |
| 55 |
/// ); |
| 56 |
/// assert_writeable_eq!( |
| 57 |
/// ScientificDecimal::from( |
| 58 |
/// Decimal::from_str("+1.729").unwrap(), |
| 59 |
/// FixedInteger::from_str("+03").unwrap() |
| 60 |
/// ), |
| 61 |
/// "+1.729e+03" |
| 62 |
/// ); |
| 63 |
/// ``` |
| 64 |
impl writeable::Writeable for ScientificDecimal { |
| 65 |
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { |
| 66 |
self.significand.write_to(sink)?; |
| 67 |
sink.write_char('e')?; |
| 68 |
self.exponent.write_to(sink) |
| 69 |
} |
| 70 |
|
| 71 |
fn writeable_length_hint(&self) -> writeable::LengthHint { |
| 72 |
self.significand.writeable_length_hint() + 1 + self.exponent.writeable_length_hint() |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
writeable::impl_display_with_writeable!(ScientificDecimal); |
| 77 |
|
| 78 |
impl ScientificDecimal { |
| 79 |
/// Parses a [`ScientificDecimal`]. |
| 80 |
#[inline] |
| 81 |
pub fn try_from_str(s: &str) -> Result<Self, ParseError> { |
| 82 |
Self::try_from_utf8(s.as_bytes()) |
| 83 |
} |
| 84 |
|
| 85 |
/// See [`Self::try_from_str`] |
| 86 |
pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> { |
| 87 |
// Fixed_Decimal::try_from supports scientific notation; ensure that |
| 88 |
// we don’t accept something like 1e1E1. Splitting on 'e' ensures that |
| 89 |
// we disallow 1e1e1. |
| 90 |
if code_units.contains(&b'E') { |
| 91 |
return Err(ParseError::Syntax); |
| 92 |
} |
| 93 |
let mut parts = code_units.split(|&c| c == b'e'); |
| 94 |
let significand = parts.next().ok_or(ParseError::Syntax)?; |
| 95 |
let exponent = parts.next().ok_or(ParseError::Syntax)?; |
| 96 |
if parts.next().is_some() { |
| 97 |
return Err(ParseError::Syntax); |
| 98 |
} |
| 99 |
Ok(ScientificDecimal::from( |
| 100 |
Decimal::try_from_utf8(significand)?, |
| 101 |
FixedInteger::try_from_utf8(exponent)?, |
| 102 |
)) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl FromStr for ScientificDecimal { |
| 107 |
type Err = ParseError; |
| 108 |
#[inline] |
| 109 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 110 |
Self::try_from_str(s) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_scientific_syntax_error() { |
| 116 |
#[derive(Debug)] |
| 117 |
struct TestCase { |
| 118 |
pub input_str: &'static str, |
| 119 |
pub expected_err: Option<ParseError>, |
| 120 |
} |
| 121 |
let cases = [ |
| 122 |
TestCase { |
| 123 |
input_str: "5", |
| 124 |
expected_err: Some(ParseError::Syntax), |
| 125 |
}, |
| 126 |
TestCase { |
| 127 |
input_str: "-123c4", |
| 128 |
expected_err: Some(ParseError::Syntax), |
| 129 |
}, |
| 130 |
TestCase { |
| 131 |
input_str: "-123e", |
| 132 |
expected_err: Some(ParseError::Syntax), |
| 133 |
}, |
| 134 |
TestCase { |
| 135 |
input_str: "1e10", |
| 136 |
expected_err: None, |
| 137 |
}, |
| 138 |
TestCase { |
| 139 |
input_str: "1e1e1", |
| 140 |
expected_err: Some(ParseError::Syntax), |
| 141 |
}, |
| 142 |
TestCase { |
| 143 |
input_str: "1e1E1", |
| 144 |
expected_err: Some(ParseError::Syntax), |
| 145 |
}, |
| 146 |
TestCase { |
| 147 |
input_str: "1E1e1", |
| 148 |
expected_err: Some(ParseError::Syntax), |
| 149 |
}, |
| 150 |
TestCase { |
| 151 |
input_str: "-1e+01", |
| 152 |
expected_err: None, |
| 153 |
}, |
| 154 |
TestCase { |
| 155 |
input_str: "-1e+1.0", |
| 156 |
expected_err: Some(ParseError::Limit), |
| 157 |
}, |
| 158 |
TestCase { |
| 159 |
input_str: "-1e+-1", |
| 160 |
expected_err: Some(ParseError::Syntax), |
| 161 |
}, |
| 162 |
TestCase { |
| 163 |
input_str: "123E4", |
| 164 |
expected_err: Some(ParseError::Syntax), |
| 165 |
}, |
| 166 |
]; |
| 167 |
for cas in &cases { |
| 168 |
match ScientificDecimal::from_str(cas.input_str) { |
| 169 |
Ok(dec) => { |
| 170 |
assert_eq!(cas.expected_err, None, "{cas:?}"); |
| 171 |
assert_eq!(cas.input_str, dec.to_string(), "{cas:?}"); |
| 172 |
} |
| 173 |
Err(err) => { |
| 174 |
assert_eq!(cas.expected_err, Some(err), "{cas:?}"); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |