-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle exponents in FixedDecimal::from_str() #1265
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic looks good and the code is readable. I think Rust offers a number of nice APIs for iterating that would make it even cleaner, but nothing blocking, so up to Manish to include or reject my suggestions.
let mut dot_index = no_sign_str_len; | ||
let mut has_exponent = false; | ||
let mut dot_index = no_sign_str.len(); | ||
let mut exponent_index = no_sign_str.len(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
: would Option(index)
be a better encapsulation for index
and exponent
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still use exponent_index
even if it's an option, though. I can rename that to exponent_or_end_index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't expontent.is_none()
equivalent of no_sign_str.len()
then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mean that every time we use exponent_index
we want to use exponent.unwrap_or(no_sign_str.len())
anyway, so it's not worth it to store the Option
} | ||
dot_index = i; | ||
has_dot = true; | ||
if i == 0 || i == no_sign_str.len() - 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
: it seems like you could get rid of no_sign_str
if you just checked the dot: Option(index)
after the loop for if it is set to the length of the string.
if has_exponent && exponent_index == i - 1 { | ||
continue; | ||
} else { | ||
return Err(Error::Syntax); | ||
} | ||
} else if *c < b'0' || *c > b'9' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
: is that faster than is_ascii_digit
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a char
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils/fixed_decimal/src/decimal.rs
Outdated
@@ -570,7 +598,7 @@ impl FromStr for FixedDecimal { | |||
|
|||
// Constructing DecimalFixed.digits | |||
let mut v: SmallVec<[u8; 8]> = SmallVec::with_capacity(digits_str_len); | |||
for c in no_sign_str[leftmost_digit..rightmost_digit].iter() { | |||
for c in no_exponent_str[leftmost_digit..rightmost_digit].iter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
: filter_map
?
if has_exponent { | ||
let mut pow = 0; | ||
let mut pos_neg = 1; | ||
for digit in &no_sign_str[exponent_index + 1..] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
: reduce
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! (I still like the Option(index)
more, but leave it to the PR author discretion).
Yeah overall that code is existing code and I feel like it's just as complex with the Option, with the complexities shifting around |
I want to discuss scientific notation from an architecture perspective. Filed #1267. |
Needed for #166