Skip to content

Commit 9e7680a

Browse files
committed
Wire up non-float_roundtrip parsing in long number case
1 parent b8d4153 commit 9e7680a

1 file changed

Lines changed: 60 additions & 3 deletions

File tree

‎src/de.rs‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
422422

423423
fn parse_number(&mut self, positive: bool, significand: u64) -> Result<ParserNumber> {
424424
Ok(match tri!(self.peek_or_null()) {
425-
b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand))),
425+
b'.' => ParserNumber::F64(tri!(self.parse_decimal(positive, significand, 0))),
426426
b'e' | b'E' => ParserNumber::F64(tri!(self.parse_exponent(positive, significand, 0))),
427427
_ => {
428428
if positive {
@@ -441,10 +441,14 @@ impl<'de, R: Read<'de>> Deserializer<R> {
441441
})
442442
}
443443

444-
fn parse_decimal(&mut self, positive: bool, mut significand: u64) -> Result<f64> {
444+
fn parse_decimal(
445+
&mut self,
446+
positive: bool,
447+
mut significand: u64,
448+
mut exponent: i32,
449+
) -> Result<f64> {
445450
self.eat_char();
446451

447-
let mut exponent: i32 = 0;
448452
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
449453
let digit = (c - b'0') as u64;
450454

@@ -578,7 +582,9 @@ impl<'de, R: Read<'de>> Deserializer<R> {
578582
Ok(if positive { f } else { -f })
579583
}
580584

585+
#[cfg(feature = "float_roundtrip")]
581586
#[cold]
587+
#[inline(never)]
582588
fn parse_long_integer(&mut self, positive: bool, partial_significand: u64) -> Result<f64> {
583589
// To deserialize floats we'll first push the integer and fraction
584590
// parts, both as byte strings, into the scratch buffer and then feed
@@ -616,6 +622,33 @@ impl<'de, R: Read<'de>> Deserializer<R> {
616622
}
617623
}
618624

625+
#[cfg(not(feature = "float_roundtrip"))]
626+
#[cold]
627+
#[inline(never)]
628+
fn parse_long_integer(&mut self, positive: bool, significand: u64) -> Result<f64> {
629+
let mut exponent = 0;
630+
loop {
631+
match tri!(self.peek_or_null()) {
632+
b'0'..=b'9' => {
633+
self.eat_char();
634+
// This could overflow... if your integer is gigabytes long.
635+
// Ignore that possibility.
636+
exponent += 1;
637+
}
638+
b'.' => {
639+
return self.parse_decimal(positive, significand, exponent);
640+
}
641+
b'e' | b'E' => {
642+
return self.parse_exponent(positive, significand, exponent);
643+
}
644+
_ => {
645+
return self.f64_from_parts(positive, significand, exponent);
646+
}
647+
}
648+
}
649+
}
650+
651+
#[cfg(feature = "float_roundtrip")]
619652
#[cold]
620653
fn parse_long_decimal(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
621654
let mut at_least_one_digit = integer_end < self.scratch.len();
@@ -638,6 +671,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
638671
}
639672
}
640673

674+
#[cfg(feature = "float_roundtrip")]
641675
fn parse_long_exponent(&mut self, positive: bool, integer_end: usize) -> Result<f64> {
642676
self.eat_char();
643677

@@ -687,6 +721,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
687721

688722
// This cold code should not be inlined into the middle of the hot
689723
// decimal-parsing loop above.
724+
#[cfg(feature = "float_roundtrip")]
690725
#[cold]
691726
#[inline(never)]
692727
fn parse_decimal_overflow(
@@ -707,6 +742,27 @@ impl<'de, R: Read<'de>> Deserializer<R> {
707742
self.parse_long_decimal(positive, integer_end)
708743
}
709744

745+
#[cfg(not(feature = "float_roundtrip"))]
746+
#[cold]
747+
#[inline(never)]
748+
fn parse_decimal_overflow(
749+
&mut self,
750+
positive: bool,
751+
significand: u64,
752+
exponent: i32,
753+
) -> Result<f64> {
754+
// The next multiply/add would overflow, so just ignore all further
755+
// digits.
756+
while let b'0'..=b'9' = tri!(self.peek_or_null()) {
757+
self.eat_char();
758+
}
759+
760+
match tri!(self.peek_or_null()) {
761+
b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
762+
_ => self.f64_from_parts(positive, significand, exponent),
763+
}
764+
}
765+
710766
// This cold code should not be inlined into the middle of the hot
711767
// exponent-parsing loop above.
712768
#[cold]
@@ -728,6 +784,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
728784
Ok(if positive { 0.0 } else { -0.0 })
729785
}
730786

787+
#[cfg(feature = "float_roundtrip")]
731788
fn f64_long_from_parts(
732789
&mut self,
733790
positive: bool,

0 commit comments

Comments
 (0)