|
| 1 | +//! Counting number of digits needed to represent a number. |
| 2 | +//! |
| 3 | +//! The [`num_integral_digits`] and [`num_fractional_digits`] functions |
| 4 | +//! count the number of digits needed to represent a number in decimal |
| 5 | +//! notation (like "123.456"). |
| 6 | +use std::convert::TryInto; |
| 7 | +use std::num::ParseIntError; |
| 8 | + |
| 9 | +use uucore::display::Quotable; |
| 10 | + |
| 11 | +/// The number of digits after the decimal point in a given number. |
| 12 | +/// |
| 13 | +/// The input `s` is a string representing a number, either an integer |
| 14 | +/// or a floating point number in either decimal notation or scientific |
| 15 | +/// notation. This function returns the number of digits after the |
| 16 | +/// decimal point needed to print the number in decimal notation. |
| 17 | +/// |
| 18 | +/// # Examples |
| 19 | +/// |
| 20 | +/// ```rust,ignore |
| 21 | +/// assert_eq!(num_fractional_digits("123.45e-1").unwrap(), 3); |
| 22 | +/// ``` |
| 23 | +pub fn num_fractional_digits(s: &str) -> Result<usize, ParseIntError> { |
| 24 | + match (s.find('.'), s.find('e')) { |
| 25 | + // For example, "123456". |
| 26 | + (None, None) => Ok(0), |
| 27 | + |
| 28 | + // For example, "123e456". |
| 29 | + (None, Some(j)) => { |
| 30 | + let exponent: i64 = s[j + 1..].parse()?; |
| 31 | + if exponent < 0 { |
| 32 | + Ok(-exponent as usize) |
| 33 | + } else { |
| 34 | + Ok(0) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // For example, "123.456". |
| 39 | + (Some(i), None) => Ok(s.len() - (i + 1)), |
| 40 | + |
| 41 | + // For example, "123.456e789". |
| 42 | + (Some(i), Some(j)) if i < j => { |
| 43 | + // Because of the match guard, this subtraction will not underflow. |
| 44 | + let num_digits_between_decimal_point_and_e = (j - (i + 1)) as i64; |
| 45 | + let exponent: i64 = s[j + 1..].parse()?; |
| 46 | + if num_digits_between_decimal_point_and_e < exponent { |
| 47 | + Ok(0) |
| 48 | + } else { |
| 49 | + Ok((num_digits_between_decimal_point_and_e - exponent) |
| 50 | + .try_into() |
| 51 | + .unwrap()) |
| 52 | + } |
| 53 | + } |
| 54 | + _ => crash!( |
| 55 | + 1, |
| 56 | + "invalid floating point argument: {}\n Try '{} --help' for more information.", |
| 57 | + s.quote(), |
| 58 | + uucore::execution_phrase() |
| 59 | + ), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// The number of digits before the decimal point in a given number. |
| 64 | +/// |
| 65 | +/// The input `s` is a string representing a number, either an integer |
| 66 | +/// or a floating point number in either decimal notation or scientific |
| 67 | +/// notation. This function returns the number of digits before the |
| 68 | +/// decimal point needed to print the number in decimal notation. |
| 69 | +/// |
| 70 | +/// # Examples |
| 71 | +/// |
| 72 | +/// ```rust,ignore |
| 73 | +/// assert_eq!(num_fractional_digits("123.45e-1").unwrap(), 2); |
| 74 | +/// ``` |
| 75 | +pub fn num_integral_digits(s: &str) -> Result<usize, ParseIntError> { |
| 76 | + match (s.find('.'), s.find('e')) { |
| 77 | + // For example, "123456". |
| 78 | + (None, None) => Ok(s.len()), |
| 79 | + |
| 80 | + // For example, "123e456". |
| 81 | + (None, Some(j)) => { |
| 82 | + let exponent: i64 = s[j + 1..].parse()?; |
| 83 | + let total = j as i64 + exponent; |
| 84 | + if total < 1 { |
| 85 | + Ok(1) |
| 86 | + } else { |
| 87 | + Ok(total.try_into().unwrap()) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // For example, "123.456". |
| 92 | + (Some(i), None) => Ok(i), |
| 93 | + |
| 94 | + // For example, "123.456e789". |
| 95 | + (Some(i), Some(j)) => { |
| 96 | + let exponent: i64 = s[j + 1..].parse()?; |
| 97 | + let minimum: usize = { |
| 98 | + let integral_part: f64 = crash_if_err!(1, s[..j].parse()); |
| 99 | + if integral_part == -0.0 && integral_part.is_sign_negative() { |
| 100 | + 2 |
| 101 | + } else { |
| 102 | + 1 |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + let total = i as i64 + exponent; |
| 107 | + if total < minimum as i64 { |
| 108 | + Ok(minimum) |
| 109 | + } else { |
| 110 | + Ok(total.try_into().unwrap()) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod tests { |
| 118 | + |
| 119 | + mod test_num_integral_digits { |
| 120 | + use crate::num_integral_digits; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_integer() { |
| 124 | + assert_eq!(num_integral_digits("123").unwrap(), 3); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_decimal() { |
| 129 | + assert_eq!(num_integral_digits("123.45").unwrap(), 3); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_scientific_no_decimal_positive_exponent() { |
| 134 | + assert_eq!(num_integral_digits("123e4").unwrap(), 3 + 4); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_scientific_with_decimal_positive_exponent() { |
| 139 | + assert_eq!(num_integral_digits("123.45e6").unwrap(), 3 + 6); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_scientific_no_decimal_negative_exponent() { |
| 144 | + assert_eq!(num_integral_digits("123e-4").unwrap(), 1); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_scientific_with_decimal_negative_exponent() { |
| 149 | + assert_eq!(num_integral_digits("123.45e-6").unwrap(), 1); |
| 150 | + assert_eq!(num_integral_digits("123.45e-1").unwrap(), 2); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + mod test_num_fractional_digits { |
| 155 | + use crate::num_fractional_digits; |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_integer() { |
| 159 | + assert_eq!(num_fractional_digits("123").unwrap(), 0); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_decimal() { |
| 164 | + assert_eq!(num_fractional_digits("123.45").unwrap(), 2); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_scientific_no_decimal_positive_exponent() { |
| 169 | + assert_eq!(num_fractional_digits("123e4").unwrap(), 0); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn test_scientific_with_decimal_positive_exponent() { |
| 174 | + assert_eq!(num_fractional_digits("123.45e6").unwrap(), 0); |
| 175 | + assert_eq!(num_fractional_digits("123.45e1").unwrap(), 1); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test_scientific_no_decimal_negative_exponent() { |
| 180 | + assert_eq!(num_fractional_digits("123e-4").unwrap(), 4); |
| 181 | + assert_eq!(num_fractional_digits("123e-1").unwrap(), 1); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn test_scientific_with_decimal_negative_exponent() { |
| 186 | + assert_eq!(num_fractional_digits("123.45e-6").unwrap(), 8); |
| 187 | + assert_eq!(num_fractional_digits("123.45e-1").unwrap(), 3); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments