Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/uucore/src/lib/features/format/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

use crate::{
error::set_exit_code,
features::format::num_parser::{ParseError, ParsedNumber},
features::format::num_parser::{ExtendedParser, ExtendedParserError},
quoting_style::{Quotes, QuotingStyle, escape_name},
show_error, show_warning,
};
use os_display::Quotable;
use std::ffi::OsStr;

use super::ExtendedBigDecimal;

/// An argument for formatting
///
/// Each of these variants is only accepted by their respective directives. For
Expand All @@ -25,7 +27,7 @@ pub enum FormatArgument {
String(String),
UnsignedInt(u64),
SignedInt(i64),
Float(f64),
Float(ExtendedBigDecimal),
/// Special argument that gets coerced into the other variants
Unparsed(String),
}
Expand All @@ -34,7 +36,7 @@ pub trait ArgumentIter<'a>: Iterator<Item = &'a FormatArgument> {
fn get_char(&mut self) -> u8;
fn get_i64(&mut self) -> i64;
fn get_u64(&mut self) -> u64;
fn get_f64(&mut self) -> f64;
fn get_extended_big_decimal(&mut self) -> ExtendedBigDecimal;
fn get_str(&mut self) -> &'a str;
}

Expand All @@ -56,7 +58,7 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
};
match next {
FormatArgument::UnsignedInt(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_u64(s), s),
FormatArgument::Unparsed(s) => extract_value(u64::extended_parse(s), s),
_ => 0,
}
}
Expand All @@ -67,19 +69,19 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
};
match next {
FormatArgument::SignedInt(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_i64(s), s),
FormatArgument::Unparsed(s) => extract_value(i64::extended_parse(s), s),
_ => 0,
}
}

fn get_f64(&mut self) -> f64 {
fn get_extended_big_decimal(&mut self) -> ExtendedBigDecimal {
let Some(next) = self.next() else {
return 0.0;
return ExtendedBigDecimal::zero();
};
match next {
FormatArgument::Float(n) => *n,
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_f64(s), s),
_ => 0.0,
FormatArgument::Float(n) => n.clone(),
FormatArgument::Unparsed(s) => extract_value(ExtendedBigDecimal::extended_parse(s), s),
_ => ExtendedBigDecimal::zero(),
}
}

Expand All @@ -91,7 +93,7 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
}
}

fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T {
fn extract_value<T: Default>(p: Result<T, ExtendedParserError<'_, T>>, input: &str) -> T {
match p {
Ok(v) => v,
Err(e) => {
Expand All @@ -103,15 +105,15 @@ fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T
},
);
match e {
ParseError::Overflow => {
ExtendedParserError::Overflow => {
show_error!("{}: Numerical result out of range", input.quote());
Default::default()
}
ParseError::NotNumeric => {
ExtendedParserError::NotNumeric => {
show_error!("{}: expected a numeric value", input.quote());
Default::default()
}
ParseError::PartialMatch(v, rest) => {
ExtendedParserError::PartialMatch(v, rest) => {
let bytes = input.as_encoded_bytes();
if !bytes.is_empty() && bytes[0] == b'\'' {
show_warning!(
Expand Down
6 changes: 6 additions & 0 deletions src/uucore/src/lib/features/format/extendedbigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ impl Zero for ExtendedBigDecimal {
}
}

impl Default for ExtendedBigDecimal {
fn default() -> Self {
Self::zero()
}
}

impl Add for ExtendedBigDecimal {
type Output = Self;

Expand Down
Loading
Loading