Switch exponent bounds checking back to using powi

This commit is contained in:
Erick Tryzelaar
2015-06-08 07:07:09 -07:00
parent 7dc1a64f03
commit 8f67e9c048
+20 -21
View File
@@ -1,4 +1,5 @@
use std::char;
use std::i32;
use std::io;
use std::str;
@@ -13,6 +14,15 @@ pub struct Deserializer<Iter: Iterator<Item=io::Result<u8>>> {
str_buf: Vec<u8>,
}
macro_rules! try_or_invalid {
($self_:expr, $e:expr) => {
match $e {
Some(v) => v,
None => { return Err($self_.error(ErrorCode::InvalidNumber)); }
}
}
}
impl<Iter> Deserializer<Iter>
where Iter: Iterator<Item=io::Result<u8>>,
{
@@ -198,16 +208,8 @@ impl<Iter> Deserializer<Iter>
while !self.eof() {
match self.ch_or_null() {
c @ b'0' ... b'9' => {
macro_rules! try_or_invalid {
($e: expr) => {
match $e {
Some(v) => v,
None => { return Err(self.error(ErrorCode::InvalidNumber)); }
}
}
}
accum = try_or_invalid!(accum.checked_mul(10));
accum = try_or_invalid!(accum.checked_add((c as u64) - ('0' as u64)));
accum = try_or_invalid!(self, accum.checked_mul(10));
accum = try_or_invalid!(self, accum.checked_add((c as u64) - ('0' as u64)));
try!(self.bump());
}
@@ -267,16 +269,8 @@ impl<Iter> Deserializer<Iter>
while !self.eof() {
match self.ch_or_null() {
c @ b'0' ... b'9' => {
macro_rules! try_or_invalid {
($e: expr) => {
match $e {
Some(v) => v,
None => { return Err(self.error(ErrorCode::InvalidNumber)); }
}
}
}
exp = try_or_invalid!(exp.checked_mul(10));
exp = try_or_invalid!(exp.checked_add((c as u64) - (b'0' as u64)));
exp = try_or_invalid!(self, exp.checked_mul(10));
exp = try_or_invalid!(self, exp.checked_add((c as u64) - (b'0' as u64)));
try!(self.bump());
}
@@ -284,7 +278,12 @@ impl<Iter> Deserializer<Iter>
}
}
let exp: f64 = 10_f64.powf(exp as f64);
let exp = if exp <= i32::MAX as u64 {
10_f64.powi(exp as i32)
} else {
return Err(self.error(ErrorCode::InvalidNumber));
};
if neg_exp {
res /= exp;
} else {