1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use core::{
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
str::FromStr,
};
use alloc::borrow::Cow;
use num_bigint::BigInt;
use num_rational::Ratio;
use num_traits::Signed;
use num_traits::{One, Pow, Zero};
use crate::measure::provider::si_prefix::{Base, SiPrefix};
// TODO: add test cases for IcuRatio.
// TODO: Make a decicion on whether to keep the `IcuRatio` public or not.
/// A ratio type that uses `BigInt` as the underlying type.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct IcuRatio(Ratio<BigInt>);
/// Represents an error when the ratio string is invalid and cannot be parsed.
#[derive(Debug, PartialEq)]
pub enum RatioFromStrError {
/// Represents an error when the ratio string is divided by zero.
DivisionByZero,
/// Represents an error when the ratio string contains multiple slashes.
/// For example, "1/2/3".
MultipleSlashes,
/// Represents an error when the ratio string contains non-numeric characters in fractions.
/// For example, "1/2A".
NonNumericCharactersInFractions,
/// Represents an error when the ratio string contains multiple scientific notations.
/// For example, "1.5E6E6".
MultipleScientificNotations,
/// Represents an error when the ratio string contains multiple decimal points.
/// For example, "1.5.6".
MultipleDecimalPoints,
/// Represents an error when the exponent part of the ratio string is not an integer.
/// For example, "1.5E6.5".
ExponentPartIsNotAnInteger,
/// Represents an error when the ratio string is dificient in some other way.
ParsingBigIntError(num_bigint::ParseBigIntError),
}
impl IcuRatio {
/// Creates a new `IcuRatio` from the given numerator and denominator.
pub fn from_big_ints(numerator: BigInt, denominator: BigInt) -> Self {
Self(Ratio::new(numerator, denominator))
}
/// Returns the current [`IcuRatio`] as a [`Ratio`] of [`BigInt`].
pub fn get_ratio(self) -> Ratio<BigInt> {
self.0
}
/// Returns the reciprocal of the ratio.
/// For example, the reciprocal of 2/3 is 3/2.
pub(crate) fn recip(&self) -> Self {
Self(self.0.recip())
}
/// Returns the absolute value of the ratio.
pub fn abs(&self) -> Self {
Self(self.0.abs())
}
/// Returns a Ratio with value of 2.
pub fn two() -> Self {
Self(Ratio::from_integer(2.into()))
}
/// Returns a Ratio with value of 10.
pub fn ten() -> Self {
Self(Ratio::from_integer(10.into()))
}
/// Returns true if the ratio is negative.
pub fn is_negative(&self) -> bool {
self.0.is_negative()
}
}
impl Mul for IcuRatio {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
Self(self.0 * rhs.0)
}
}
impl Mul<&IcuRatio> for &IcuRatio {
type Output = IcuRatio;
fn mul(self, rhs: &IcuRatio) -> IcuRatio {
IcuRatio(&self.0 * &rhs.0)
}
}
impl MulAssign<IcuRatio> for IcuRatio {
fn mul_assign(&mut self, rhs: IcuRatio) {
self.0 *= rhs.0;
}
}
impl MulAssign<&SiPrefix> for IcuRatio {
fn mul_assign(&mut self, rhs: &SiPrefix) {
match rhs.base {
Base::Decimal => {
*self *= IcuRatio::ten().pow(rhs.power as i32);
}
Base::Binary => {
*self *= IcuRatio::two().pow(rhs.power as i32);
}
}
}
}
impl Div for IcuRatio {
type Output = Self;
fn div(self, rhs: Self) -> Self {
Self(self.0 / rhs.0)
}
}
impl DivAssign for IcuRatio {
fn div_assign(&mut self, rhs: Self) {
self.0 /= rhs.0;
}
}
impl Add for IcuRatio {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl Add<&IcuRatio> for &IcuRatio {
type Output = IcuRatio;
fn add(self, rhs: &IcuRatio) -> IcuRatio {
IcuRatio(&self.0 + &rhs.0)
}
}
impl AddAssign for IcuRatio {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl Sub for IcuRatio {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl SubAssign for IcuRatio {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl Pow<i32> for IcuRatio {
type Output = Self;
fn pow(self, exp: i32) -> Self {
Self(self.0.pow(exp))
}
}
impl Zero for IcuRatio {
fn zero() -> Self {
Self(Ratio::zero())
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
impl One for IcuRatio {
fn one() -> Self {
Self(Ratio::one())
}
fn is_one(&self) -> bool {
self.0.is_one()
}
}
impl From<Ratio<BigInt>> for IcuRatio {
fn from(ratio: Ratio<BigInt>) -> Self {
Self(ratio)
}
}
impl From<u32> for IcuRatio {
fn from(value: u32) -> Self {
Self(Ratio::from_integer(value.into()))
}
}
impl FromStr for IcuRatio {
type Err = RatioFromStrError;
/// Converts a string representation of a ratio into an `IcuRatio`.
/// Supported string formats include:
/// ```
/// use core::str::FromStr;
/// use icu::experimental::units::ratio::IcuRatio;
/// use num_bigint::BigInt;
/// use num_rational::Ratio;
/// use num_traits::identities::Zero;
///
/// // Fractional notation
/// let ratio = IcuRatio::from_str("1/2").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::new(BigInt::from(1), BigInt::from(2)))
/// );
///
/// // Decimal notation
/// let ratio = IcuRatio::from_str("1.5").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::new(BigInt::from(3), BigInt::from(2)))
/// );
///
/// // Scientific notation
/// let ratio = IcuRatio::from_str("1.5E6").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::from_integer(BigInt::from(1500000)))
/// );
///
/// // Scientific notation with negative exponent
/// let ratio = IcuRatio::from_str("1.5E-6").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::new(BigInt::from(15), BigInt::from(10000000)))
/// );
///
/// // Scientific notation with commas
/// let ratio = IcuRatio::from_str("1,500E6").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::from_integer(BigInt::from(1500000000)))
/// );
///
/// // Integer notation
/// let ratio = IcuRatio::from_str("1").unwrap();
/// assert_eq!(ratio, IcuRatio::from(Ratio::from_integer(BigInt::from(1))));
///
/// // Fractional notation with exponent
/// let ratio = IcuRatio::from_str("1/2E5").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::from_integer(BigInt::from(50000)))
/// );
///
/// // Negative numbers in fractional notation
/// let ratio = IcuRatio::from_str("-1/2").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::new(BigInt::from(-1), BigInt::from(2)))
/// );
///
/// // Negative numbers in decimal notation
/// let ratio = IcuRatio::from_str("-1.5").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::new(BigInt::from(-3), BigInt::from(2)))
/// );
///
/// // Negative numbers in scientific notation
/// let ratio = IcuRatio::from_str("-1.5E6").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::from_integer(BigInt::from(-1500000)))
/// );
///
/// // Negative numbers in scientific notation with commas
/// let ratio = IcuRatio::from_str("-1,500E6").unwrap();
/// assert_eq!(
/// ratio,
/// IcuRatio::from(Ratio::from_integer(BigInt::from(-1500000000)))
/// );
///
/// // Corner cases
///
/// // Empty string
/// let ratio = IcuRatio::from_str("").unwrap();
/// assert_eq!(ratio, IcuRatio::zero());
///
/// // Single dot
/// let ratio = IcuRatio::from_str(".").unwrap();
/// assert_eq!(ratio, IcuRatio::zero());
///
/// // Single minus
/// let ratio = IcuRatio::from_str("-").unwrap();
/// assert_eq!(ratio, IcuRatio::zero());
///
/// // Single plus
/// let ratio = IcuRatio::from_str("+").unwrap();
/// assert_eq!(ratio, IcuRatio::zero());
///
/// // Only zeros after dot
/// let ratio = IcuRatio::from_str(".000").unwrap();
/// assert_eq!(ratio, IcuRatio::zero());
///
/// // Error cases
/// // Division by zero
/// assert!(IcuRatio::from_str("1/0").is_err());
///
/// // Multiple slashes
/// assert!(IcuRatio::from_str("1/2/3").is_err());
///
/// // Non-numeric characters in fractions
/// assert!(IcuRatio::from_str("1/2A").is_err());
///
/// // Multiple scientific notations
/// assert!(IcuRatio::from_str("1.5E6E6").is_err());
///
/// // Multiple decimal points
/// assert!(IcuRatio::from_str("1.5.6").is_err());
///
/// // Exponent part is not an integer
/// assert!(IcuRatio::from_str("1.5E6.5").is_err());
/// ```
/// NOTE:
/// You can add as many commas as you want in the string, they will be disregarded.
fn from_str(number_str: &str) -> Result<Self, Self::Err> {
/// This function interprets numeric strings and converts them into an `IcuRatio` object, supporting:
/// - Simple fractions: e.g., "1/2".
/// - Decimals: e.g., "1.5".
/// - Mixed fractions: e.g., "1.5/6", "1.4/5.6".
///
/// Note: Scientific notation is not supported.
fn parse_fraction(decimal: &str) -> Result<IcuRatio, RatioFromStrError> {
let mut components = decimal.split('/');
let numerator = components.next();
let denominator = components.next();
if components.next().is_some() {
return Err(RatioFromStrError::MultipleSlashes);
}
let numerator = match numerator {
Some(numerator) => parse_decimal(numerator)?,
None => IcuRatio::zero(),
};
let denominator = match denominator {
Some(denominator) => parse_decimal(denominator)?,
None => IcuRatio::one(),
};
if denominator.is_zero() {
return Err(RatioFromStrError::DivisionByZero);
}
Ok(numerator / denominator)
}
/// Parses a decimal number from a string input.
///
/// Accepts input in various decimal formats, including:
/// - Whole numbers: "1"
/// - Decimal numbers: "1.5"
///
/// An empty string input is interpreted as "0".
///
/// Note: Fractional inputs are not supported in this context.
fn parse_decimal(decimal: &str) -> Result<IcuRatio, RatioFromStrError> {
let is_negative = decimal.starts_with('-');
let mut dot_parts = decimal.split('.');
let integer_part = dot_parts.next();
let decimal_part = dot_parts.next();
if dot_parts.next().is_some() {
return Err(RatioFromStrError::MultipleDecimalPoints);
}
let integer_part = match integer_part {
None | Some("") => IcuRatio::zero(),
Some("-") | Some("+") => IcuRatio::zero(),
Some(integer_part) => IcuRatio(
BigInt::from_str(integer_part)
.map_err(RatioFromStrError::ParsingBigIntError)?
.into(),
),
};
let decimal_part = match decimal_part {
None | Some("") => return Ok(integer_part),
Some(decimal_part) => {
let decimal_power = decimal_part.len() as i32;
let decimal_part = IcuRatio(
BigInt::from_str(decimal_part)
.map_err(RatioFromStrError::ParsingBigIntError)?
.into(),
);
decimal_part / IcuRatio::ten().pow(decimal_power) // Divide by 10^decimal_power
}
};
Ok(if is_negative {
integer_part - decimal_part
} else {
integer_part + decimal_part
})
}
let number_str = if number_str.contains(',') {
Cow::Owned(number_str.replace(',', ""))
} else {
Cow::Borrowed(number_str)
};
if number_str.is_empty() {
return Ok(IcuRatio::zero());
}
let mut parts = number_str.split(['e', 'E']);
let significand = parts.next();
let exponent = parts.next();
if parts.next().is_some() {
return Err(RatioFromStrError::MultipleScientificNotations);
}
let significand = match significand {
Some(significand) => parse_fraction(significand)?,
None => IcuRatio::one(),
};
let exponent = match exponent {
Some(exponent) => {
let exponent = exponent
.parse::<i32>()
.map_err(|_| RatioFromStrError::ExponentPartIsNotAnInteger)?;
IcuRatio::ten().pow(exponent)
}
None => IcuRatio::one(),
};
Ok(significand * exponent)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_icu_ratio_from_str() {
let test_cases: &[(&str, Result<IcuRatio, RatioFromStrError>)] = &[
("1/2", Ok(IcuRatio::from_big_ints(1.into(), 2.into()))),
("1.5", Ok(IcuRatio::from_big_ints(3.into(), 2.into()))),
(
"-1.5",
Ok(IcuRatio::from_big_ints(BigInt::from(-3), 2.into())),
),
(
"-.05",
Ok(IcuRatio::from_big_ints(BigInt::from(-5), 100.into())),
),
("+.05", Ok(IcuRatio::from_big_ints(5.into(), 100.into()))),
("+1.5", Ok(IcuRatio::from_big_ints(15.into(), 10.into()))),
(
"1.5E6",
Ok(IcuRatio::from_big_ints(1500000.into(), 1.into())),
),
(
"1.5E-6",
Ok(IcuRatio::from_big_ints(15.into(), 10000000.into())),
),
(
"1,500E6",
Ok(IcuRatio::from_big_ints(1500000000.into(), 1.into())),
),
(
"1.0005",
Ok(IcuRatio::from_big_ints(10005.into(), 10000.into())),
),
(".0005", Ok(IcuRatio::from_big_ints(5.into(), 10000.into()))),
("1", Ok(IcuRatio::from_big_ints(1.into(), 1.into()))),
("", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
(".", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
("-", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
("+", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
(".000", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
(
"-1/2E5",
Ok(IcuRatio::from_big_ints(BigInt::from(-50000), 1.into())),
),
("1/2E5", Ok(IcuRatio::from_big_ints(50000.into(), 1.into()))),
// commas are neglected
(",,,,", Ok(IcuRatio::from_big_ints(0.into(), 1.into()))),
("1/0", Err(RatioFromStrError::DivisionByZero)),
("1/2/3", Err(RatioFromStrError::MultipleSlashes)),
// TODO: fix how to test the error returned as another error type.
// ("1/2A", Err(RatioFromStrError::ParsingBigIntError()),
];
for (input, expected) in test_cases.iter() {
let actual = &IcuRatio::from_str(input);
assert_eq!(actual, expected, "Values do not match for input: {}", input);
}
}
}