Skip to content

Commit ace92dc

Browse files
authored
Merge pull request #7556 from drinkcat/parse-bigdecimal
uucore: format: num_parser: Use ExtendedBigDecimal
2 parents 1fe7642 + 30c89af commit ace92dc

File tree

5 files changed

+518
-256
lines changed

5 files changed

+518
-256
lines changed

src/uucore/src/lib/features/format/argument.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
use crate::{
77
error::set_exit_code,
8-
features::format::num_parser::{ParseError, ParsedNumber},
8+
features::format::num_parser::{ExtendedParser, ExtendedParserError},
99
quoting_style::{Quotes, QuotingStyle, escape_name},
1010
show_error, show_warning,
1111
};
1212
use os_display::Quotable;
1313
use std::ffi::OsStr;
1414

15+
use super::ExtendedBigDecimal;
16+
1517
/// An argument for formatting
1618
///
1719
/// Each of these variants is only accepted by their respective directives. For
@@ -25,7 +27,7 @@ pub enum FormatArgument {
2527
String(String),
2628
UnsignedInt(u64),
2729
SignedInt(i64),
28-
Float(f64),
30+
Float(ExtendedBigDecimal),
2931
/// Special argument that gets coerced into the other variants
3032
Unparsed(String),
3133
}
@@ -34,7 +36,7 @@ pub trait ArgumentIter<'a>: Iterator<Item = &'a FormatArgument> {
3436
fn get_char(&mut self) -> u8;
3537
fn get_i64(&mut self) -> i64;
3638
fn get_u64(&mut self) -> u64;
37-
fn get_f64(&mut self) -> f64;
39+
fn get_extended_big_decimal(&mut self) -> ExtendedBigDecimal;
3840
fn get_str(&mut self) -> &'a str;
3941
}
4042

@@ -56,7 +58,7 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
5658
};
5759
match next {
5860
FormatArgument::UnsignedInt(n) => *n,
59-
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_u64(s), s),
61+
FormatArgument::Unparsed(s) => extract_value(u64::extended_parse(s), s),
6062
_ => 0,
6163
}
6264
}
@@ -67,19 +69,19 @@ impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
6769
};
6870
match next {
6971
FormatArgument::SignedInt(n) => *n,
70-
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_i64(s), s),
72+
FormatArgument::Unparsed(s) => extract_value(i64::extended_parse(s), s),
7173
_ => 0,
7274
}
7375
}
7476

75-
fn get_f64(&mut self) -> f64 {
77+
fn get_extended_big_decimal(&mut self) -> ExtendedBigDecimal {
7678
let Some(next) = self.next() else {
77-
return 0.0;
79+
return ExtendedBigDecimal::zero();
7880
};
7981
match next {
80-
FormatArgument::Float(n) => *n,
81-
FormatArgument::Unparsed(s) => extract_value(ParsedNumber::parse_f64(s), s),
82-
_ => 0.0,
82+
FormatArgument::Float(n) => n.clone(),
83+
FormatArgument::Unparsed(s) => extract_value(ExtendedBigDecimal::extended_parse(s), s),
84+
_ => ExtendedBigDecimal::zero(),
8385
}
8486
}
8587

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

94-
fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T {
96+
fn extract_value<T: Default>(p: Result<T, ExtendedParserError<'_, T>>, input: &str) -> T {
9597
match p {
9698
Ok(v) => v,
9799
Err(e) => {
@@ -103,15 +105,15 @@ fn extract_value<T: Default>(p: Result<T, ParseError<'_, T>>, input: &str) -> T
103105
},
104106
);
105107
match e {
106-
ParseError::Overflow => {
108+
ExtendedParserError::Overflow => {
107109
show_error!("{}: Numerical result out of range", input.quote());
108110
Default::default()
109111
}
110-
ParseError::NotNumeric => {
112+
ExtendedParserError::NotNumeric => {
111113
show_error!("{}: expected a numeric value", input.quote());
112114
Default::default()
113115
}
114-
ParseError::PartialMatch(v, rest) => {
116+
ExtendedParserError::PartialMatch(v, rest) => {
115117
let bytes = input.as_encoded_bytes();
116118
if !bytes.is_empty() && bytes[0] == b'\'' {
117119
show_warning!(

src/uucore/src/lib/features/format/extendedbigdecimal.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ impl Zero for ExtendedBigDecimal {
141141
}
142142
}
143143

144+
impl Default for ExtendedBigDecimal {
145+
fn default() -> Self {
146+
Self::zero()
147+
}
148+
}
149+
144150
impl Add for ExtendedBigDecimal {
145151
type Output = Self;
146152

0 commit comments

Comments
 (0)