Skip to content
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

Make ".".parse::<f32>() and ".".parse::<f64>() return Err #30681

Merged
merged 1 commit into from Jan 5, 2016
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
17 changes: 9 additions & 8 deletions src/libcore/num/dec2flt/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,28 @@ pub enum ParseResult<'a> {
/// Check if the input string is a valid floating point number and if so, locate the integral
/// part, the fractional part, and the exponent in it. Does not handle signs.
pub fn parse_decimal(s: &str) -> ParseResult {
if s.is_empty() {
return Invalid;
}

let s = s.as_bytes();
let (integral, s) = eat_digits(s);

match s.first() {
None => {
if integral.is_empty() {
return Invalid; // No digits at all
}
Valid(Decimal::new(integral, b"", 0))
}
None => Valid(Decimal::new(integral, b"", 0)),
Some(&b'e') | Some(&b'E') => {
if integral.is_empty() {
return Invalid; // No digits before 'e'
}

parse_exp(integral, b"", &s[1..])
}
Some(&b'.') => {
let (fractional, s) = eat_digits(&s[1..]);
if integral.is_empty() && fractional.is_empty() && s.is_empty() {
// For historic reasons "." is a valid input.
return Valid(Decimal::new(b"", b"", 0));
return Invalid;
}

match s.first() {
None => Valid(Decimal::new(integral, fractional, 0)),
Some(&b'e') | Some(&b'E') => parse_exp(integral, fractional, &s[1..]),
Expand Down
3 changes: 2 additions & 1 deletion src/libcoretest/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ fn fast_path_correct() {

#[test]
fn lonely_dot() {
assert_eq!(".".parse(), Ok(0.0));
assert!(".".parse::<f32>().is_err());
assert!(".".parse::<f64>().is_err());
}

#[test]
Expand Down