diff --git a/src/uu/seq/src/floatparse.rs b/src/uu/seq/src/hexadecimalfloat.rs similarity index 89% rename from src/uu/seq/src/floatparse.rs rename to src/uu/seq/src/hexadecimalfloat.rs index c00b6126b1..82d2a29a79 100644 --- a/src/uu/seq/src/floatparse.rs +++ b/src/uu/seq/src/hexadecimalfloat.rs @@ -24,12 +24,12 @@ const HEX_RADIX: u32 = 16; /// ```rust,ignore /// let input = "0x1.4p-2"; /// let expected = 0.3125; -/// match input.parse::().unwrap().number { +/// match input.parse_number::().unwrap().number { /// ExtendedBigDecimal::BigDecimal(bd) => assert_eq!(bd.to_f64().unwrap(),expected), /// _ => unreachable!() /// }; /// ``` -pub fn parse_hexadecimal_float(s: &str) -> Result { +pub fn parse_number(s: &str) -> Result { // Parse floating point parts let (sign, remain) = parse_sign_multiplier(s.trim())?; let remain = parse_hex_prefix(remain)?; @@ -80,7 +80,7 @@ pub fn parse_hexadecimal_float(s: &str) -> Result Option { +pub fn parse_precision(s: &str) -> Option { let hex_index = s.find(['x', 'X']); let point_index = s.find('.'); @@ -254,13 +254,13 @@ fn parse_exponent_part(s: &str) -> Result<(Option, &str), ParseNumberError> #[cfg(test)] mod tests { - use super::{detect_precision, parse_hexadecimal_float}; + use super::{parse_number, parse_precision}; use crate::{numberparse::ParseNumberError, ExtendedBigDecimal}; use bigdecimal::BigDecimal; use num_traits::ToPrimitive; fn parse_big_decimal(s: &str) -> Result { - match parse_hexadecimal_float(s)?.number { + match parse_number(s)?.number { ExtendedBigDecimal::BigDecimal(bd) => Ok(bd), _ => Err(ParseNumberError::Float), } @@ -358,47 +358,47 @@ mod tests { #[test] fn test_parse_precise_number_count_digits() { - let precise_num = parse_hexadecimal_float("0x1.2").unwrap(); // 1.125 decimal + let precise_num = parse_number("0x1.2").unwrap(); // 1.125 decimal assert_eq!(precise_num.num_integral_digits, 1); assert_eq!(precise_num.num_fractional_digits, 3); - let precise_num = parse_hexadecimal_float("-0x1.2").unwrap(); // -1.125 decimal + let precise_num = parse_number("-0x1.2").unwrap(); // -1.125 decimal assert_eq!(precise_num.num_integral_digits, 2); assert_eq!(precise_num.num_fractional_digits, 3); - let precise_num = parse_hexadecimal_float("0x123.8").unwrap(); // 291.5 decimal + let precise_num = parse_number("0x123.8").unwrap(); // 291.5 decimal assert_eq!(precise_num.num_integral_digits, 3); assert_eq!(precise_num.num_fractional_digits, 1); - let precise_num = parse_hexadecimal_float("-0x123.8").unwrap(); // -291.5 decimal + let precise_num = parse_number("-0x123.8").unwrap(); // -291.5 decimal assert_eq!(precise_num.num_integral_digits, 4); assert_eq!(precise_num.num_fractional_digits, 1); } #[test] fn test_detect_precision() { - assert_eq!(detect_precision("1"), Some(0)); - assert_eq!(detect_precision("0x1"), Some(0)); - assert_eq!(detect_precision("0x1.1"), None); - assert_eq!(detect_precision("0x1.1p2"), None); - assert_eq!(detect_precision("0x1.1p-2"), None); - assert_eq!(detect_precision(".1"), Some(1)); - assert_eq!(detect_precision("1.1"), Some(1)); - assert_eq!(detect_precision("1.12"), Some(2)); - assert_eq!(detect_precision("1.12345678"), Some(8)); - assert_eq!(detect_precision("1.12345678e-3"), Some(11)); - assert_eq!(detect_precision("1.1e-1"), Some(2)); - assert_eq!(detect_precision("1.1e-3"), Some(4)); + assert_eq!(parse_precision("1"), Some(0)); + assert_eq!(parse_precision("0x1"), Some(0)); + assert_eq!(parse_precision("0x1.1"), None); + assert_eq!(parse_precision("0x1.1p2"), None); + assert_eq!(parse_precision("0x1.1p-2"), None); + assert_eq!(parse_precision(".1"), Some(1)); + assert_eq!(parse_precision("1.1"), Some(1)); + assert_eq!(parse_precision("1.12"), Some(2)); + assert_eq!(parse_precision("1.12345678"), Some(8)); + assert_eq!(parse_precision("1.12345678e-3"), Some(11)); + assert_eq!(parse_precision("1.1e-1"), Some(2)); + assert_eq!(parse_precision("1.1e-3"), Some(4)); } #[test] fn test_detect_precision_invalid() { // Just to make sure it's not crash on incomplete values/bad format // Good enough for now. - assert_eq!(detect_precision("1."), Some(0)); - assert_eq!(detect_precision("1e"), Some(0)); - assert_eq!(detect_precision("1e-"), Some(0)); - assert_eq!(detect_precision("1e+"), Some(0)); - assert_eq!(detect_precision("1em"), Some(0)); + assert_eq!(parse_precision("1."), Some(0)); + assert_eq!(parse_precision("1e"), Some(0)); + assert_eq!(parse_precision("1e-"), Some(0)); + assert_eq!(parse_precision("1e+"), Some(0)); + assert_eq!(parse_precision("1em"), Some(0)); } } diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 00a99346c1..478622515c 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore extendedbigdecimal bigdecimal numberparse floatparse +// spell-checker:ignore extendedbigdecimal bigdecimal numberparse hexadecimalfloat //! Parsing numbers for use in `seq`. //! //! This module provides an implementation of [`FromStr`] for the @@ -16,7 +16,7 @@ use num_traits::Num; use num_traits::Zero; use crate::extendedbigdecimal::ExtendedBigDecimal; -use crate::floatparse; +use crate::hexadecimalfloat; use crate::number::PreciseNumber; /// An error returned when parsing a number fails. @@ -298,7 +298,7 @@ fn parse_decimal_and_exponent( /// ``` fn parse_hexadecimal(s: &str) -> Result { if s.find(['.', 'p', 'P']).is_some() { - floatparse::parse_hexadecimal_float(s) + hexadecimalfloat::parse_number(s) } else { parse_hexadecimal_integer(s) } diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index fd0f11aecb..0ee5101d7e 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) bigdecimal extendedbigdecimal numberparse floatparse +// spell-checker:ignore (ToDO) bigdecimal extendedbigdecimal numberparse hexadecimalfloat use std::ffi::OsString; use std::io::{stdout, ErrorKind, Write}; @@ -15,8 +15,9 @@ use uucore::{format_usage, help_about, help_usage}; mod error; mod extendedbigdecimal; +mod hexadecimalfloat; + // public to allow fuzzing -mod floatparse; #[cfg(fuzzing)] pub mod number; #[cfg(not(fuzzing))] @@ -114,7 +115,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let (first, first_precision) = if numbers.len() > 1 { match numbers[0].parse() { - Ok(num) => (num, floatparse::detect_precision(numbers[0])), + Ok(num) => (num, hexadecimalfloat::parse_precision(numbers[0])), Err(e) => return Err(SeqError::ParseError(numbers[0].to_string(), e).into()), } } else { @@ -122,7 +123,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let (increment, increment_precision) = if numbers.len() > 2 { match numbers[1].parse() { - Ok(num) => (num, floatparse::detect_precision(numbers[1])), + Ok(num) => (num, hexadecimalfloat::parse_precision(numbers[1])), Err(e) => return Err(SeqError::ParseError(numbers[1].to_string(), e).into()), } } else { @@ -137,7 +138,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // `uu_app()`. let n: usize = numbers.len(); match numbers[n - 1].parse() { - Ok(num) => (num, floatparse::detect_precision(numbers[n - 1])), + Ok(num) => (num, hexadecimalfloat::parse_precision(numbers[n - 1])), Err(e) => return Err(SeqError::ParseError(numbers[n - 1].to_string(), e).into()), } };