Skip to content

Commit

Permalink
Merge pull request #786 from ruifengx/master
Browse files Browse the repository at this point in the history
Make arbitrary_precision preserve the exact string representation
  • Loading branch information
dtolnay authored Jul 29, 2021
2 parents c9193d4 + 2152324 commit c38fc65
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
fn scan_number(&mut self, buf: &mut String) -> Result<()> {
match tri!(self.peek_or_null()) {
b'.' => self.scan_decimal(buf),
b'e' | b'E' => self.scan_exponent(buf),
c @ (b'e' | b'E') => self.scan_exponent(c as char, buf),
_ => Ok(()),
}
}
Expand All @@ -924,19 +924,20 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

match tri!(self.peek_or_null()) {
b'e' | b'E' => self.scan_exponent(buf),
c @ (b'e' | b'E') => self.scan_exponent(c as char, buf),
_ => Ok(()),
}
}

#[cfg(feature = "arbitrary_precision")]
fn scan_exponent(&mut self, buf: &mut String) -> Result<()> {
fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push('e');
buf.push(e);

match tri!(self.peek_or_null()) {
b'+' => {
self.eat_char();
buf.push('+');
}
b'-' => {
self.eat_char();
Expand Down
6 changes: 6 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,14 @@ fn test_parse_number() {
#[cfg(feature = "arbitrary_precision")]
test_parse_ok(vec![
("1e999", Number::from_string_unchecked("1e999".to_owned())),
("1e+999", Number::from_string_unchecked("1e+999".to_owned())),
("-1e999", Number::from_string_unchecked("-1e999".to_owned())),
("1e-999", Number::from_string_unchecked("1e-999".to_owned())),
("1E999", Number::from_string_unchecked("1E999".to_owned())),
("1E+999", Number::from_string_unchecked("1E+999".to_owned())),
("-1E999", Number::from_string_unchecked("-1E999".to_owned())),
("1E-999", Number::from_string_unchecked("1E-999".to_owned())),
("1E+000", Number::from_string_unchecked("1E+000".to_owned())),
(
"2.3e999",
Number::from_string_unchecked("2.3e999".to_owned()),
Expand Down

0 comments on commit c38fc65

Please sign in to comment.