Skip to content

Commit

Permalink
seq:update names
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-sh committed Jan 4, 2025
1 parent ed1a842 commit 16afa0f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
52 changes: 26 additions & 26 deletions src/uu/seq/src/floatparse.rs → src/uu/seq/src/hexadecimalfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const HEX_RADIX: u32 = 16;
/// ```rust,ignore
/// let input = "0x1.4p-2";
/// let expected = 0.3125;
/// match input.parse::<PreciseNumber>().unwrap().number {
/// match input.parse_number::<PreciseNumber>().unwrap().number {
/// ExtendedBigDecimal::BigDecimal(bd) => assert_eq!(bd.to_f64().unwrap(),expected),
/// _ => unreachable!()
/// };
/// ```
pub fn parse_hexadecimal_float(s: &str) -> Result<PreciseNumber, ParseNumberError> {
pub fn parse_number(s: &str) -> Result<PreciseNumber, ParseNumberError> {
// Parse floating point parts
let (sign, remain) = parse_sign_multiplier(s.trim())?;
let remain = parse_hex_prefix(remain)?;
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn parse_hexadecimal_float(s: &str) -> Result<PreciseNumber, ParseNumberErro

// Detect number precision similar to GNU coreutils. Refer to scan_arg in seq.c. There are still
// some differences from the GNU version, but this should be sufficient to test the idea.
pub fn detect_precision(s: &str) -> Option<usize> {
pub fn parse_precision(s: &str) -> Option<usize> {
let hex_index = s.find(['x', 'X']);
let point_index = s.find('.');

Expand Down Expand Up @@ -254,13 +254,13 @@ fn parse_exponent_part(s: &str) -> Result<(Option<i32>, &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<BigDecimal, ParseNumberError> {
match parse_hexadecimal_float(s)?.number {
match parse_number(s)?.number {
ExtendedBigDecimal::BigDecimal(bd) => Ok(bd),
_ => Err(ParseNumberError::Float),
}
Expand Down Expand Up @@ -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));
}
}
6 changes: 3 additions & 3 deletions src/uu/seq/src/numberparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -298,7 +298,7 @@ fn parse_decimal_and_exponent(
/// ```
fn parse_hexadecimal(s: &str) -> Result<PreciseNumber, ParseNumberError> {
if s.find(['.', 'p', 'P']).is_some() {
floatparse::parse_hexadecimal_float(s)
hexadecimalfloat::parse_number(s)
} else {
parse_hexadecimal_integer(s)
}
Expand Down
11 changes: 6 additions & 5 deletions src/uu/seq/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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))]
Expand Down Expand Up @@ -114,15 +115,15 @@ 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 {
(PreciseNumber::one(), Some(0))
};
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 {
Expand All @@ -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()),
}
};
Expand Down

0 comments on commit 16afa0f

Please sign in to comment.