Skip to content

Commit

Permalink
fix: parse "infinity" literal fully in float (#1673)
Browse files Browse the repository at this point in the history
`float` first parsed the "inf" literal and then "infinity", therefore
even though the latter was parsed correctly, the suffix "inity" was
returned as remaining input, which is not correct.

Co-authored-by: Geoffroy Couprie <contact@geoffroycouprie.com>
  • Loading branch information
boxdot and Geal authored Oct 21, 2023
1 parent c98ffeb commit 63def4e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/number/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,11 +1283,11 @@ where
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::complete::tag_no_case::<_, _, E>("inf")(i.clone())
crate::bytes::complete::tag_no_case::<_, _, E>("infinity")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::complete::tag_no_case::<_, _, E>("infinity")(i.clone())
crate::bytes::complete::tag_no_case::<_, _, E>("inf")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
))
Expand Down Expand Up @@ -1853,8 +1853,9 @@ mod tests {

let (_i, inf) = float::<_, ()>("inf").unwrap();
assert!(inf.is_infinite());
let (_i, inf) = float::<_, ()>("infinite").unwrap();
let (i, inf) = float::<_, ()>("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());
}

#[test]
Expand Down
9 changes: 5 additions & 4 deletions src/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,11 +1328,11 @@ where
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
))
Expand Down Expand Up @@ -1449,7 +1449,8 @@ mod tests {
let (_i, inf) = float::<_, ()>("inf").unwrap();
assert!(inf.is_infinite());
let (_i, inf) = float::<_, ()>("infinite").unwrap();
assert!(inf.is_infinite());*/
let (i, inf) = float::<_, ()>("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());*/
}
}
7 changes: 4 additions & 3 deletions src/number/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,11 +1250,11 @@ where
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
|i: T| {
crate::bytes::streaming::tag_no_case::<_, _, E>("infinity")(i.clone())
crate::bytes::streaming::tag_no_case::<_, _, E>("inf")(i.clone())
.map_err(|_| crate::Err::Error(E::from_error_kind(i, ErrorKind::Float)))
},
))
Expand Down Expand Up @@ -1928,8 +1928,9 @@ mod tests {

let (_i, inf) = float::<_, ()>("inf").unwrap();
assert!(inf.is_infinite());
let (_i, inf) = float::<_, ()>("infinite").unwrap();
let (i, inf) = float::<_, ()>("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());
}

#[test]
Expand Down

0 comments on commit 63def4e

Please sign in to comment.