@@ -12,6 +12,12 @@ use crate::safer_unchecked::GetSaferUnchecked;
1212use crate :: StaticNode ;
1313use crate :: { Deserializer , ErrorType , Result } ;
1414
15+ macro_rules! err {
16+ ( $idx: ident, $num: expr) => {
17+ return Err ( Error :: new_c( $idx, $num as char , ErrorType :: InvalidNumber ) )
18+ } ;
19+ }
20+
1521macro_rules! get {
1622 ( $buf: ident, $idx: expr) => {
1723 unsafe { * $buf. get_kinda_unchecked( $idx as usize ) }
@@ -23,11 +29,7 @@ macro_rules! check_overflow {
2329 if $overflowed {
2430 #[ cfg( not( feature = "big-int-as-float" ) ) ]
2531 {
26- return Err ( Error :: new_c(
27- $idx,
28- get!( $buf, $idx) as char ,
29- ErrorType :: InvalidNumber ,
30- ) ) ;
32+ err!( $idx, get!( $buf, $idx) ) ;
3133 }
3234 #[ cfg( feature = "big-int-as-float" ) ]
3335 {
@@ -61,31 +63,19 @@ impl<'de> Deserializer<'de> {
6163 if negative {
6264 idx += 1 ;
6365 if !is_integer ( get ! ( buf, idx) ) {
64- return Err ( Error :: new_c (
65- idx,
66- get ! ( buf, idx) as char ,
67- ErrorType :: InvalidNumber ,
68- ) ) ;
66+ err ! ( idx, get!( buf, idx) ) ;
6967 }
7068 }
7169 let mut start = idx;
7270 let mut num: u64 = 0 ;
7371 if get ! ( buf, idx) == b'0' {
7472 idx += 1 ;
7573 if is_not_structural_or_whitespace_or_exponent_or_decimal ( get ! ( buf, idx) ) {
76- return Err ( Error :: new_c (
77- idx,
78- get ! ( buf, idx) as char ,
79- ErrorType :: InvalidNumber ,
80- ) ) ;
74+ err ! ( idx, get!( buf, idx) ) ;
8175 }
8276 } else {
8377 if !is_integer ( get ! ( buf, idx) ) {
84- return Err ( Error :: new_c (
85- idx,
86- get ! ( buf, idx) as char ,
87- ErrorType :: InvalidNumber ,
88- ) ) ;
78+ err ! ( idx, get!( buf, idx) ) ;
8979 }
9080 num = u64:: from ( get ! ( buf, idx) - b'0' ) ;
9181 idx += 1 ;
@@ -108,11 +98,7 @@ impl<'de> Deserializer<'de> {
10898 . wrapping_add ( u64:: from ( get ! ( buf, idx) - b'0' ) ) ;
10999 idx += 1 ;
110100 } else {
111- return Err ( Error :: new_c (
112- idx,
113- get ! ( buf, idx) as char ,
114- ErrorType :: InvalidNumber ,
115- ) ) ;
101+ err ! ( idx, get!( buf, idx) ) ;
116102 }
117103
118104 #[ cfg( feature = "swar-number-parsing" ) ]
@@ -155,11 +141,7 @@ impl<'de> Deserializer<'de> {
155141 }
156142 }
157143 if !is_integer ( get ! ( buf, idx) ) {
158- return Err ( Error :: new_c (
159- idx,
160- get ! ( buf, idx) as char ,
161- ErrorType :: InvalidNumber ,
162- ) ) ;
144+ err ! ( idx, get!( buf, idx) ) ;
163145 }
164146 let mut exp_number = i64:: from ( get ! ( buf, idx) - b'0' ) ;
165147 idx += 1 ;
@@ -173,11 +155,7 @@ impl<'de> Deserializer<'de> {
173155 }
174156 while is_integer ( get ! ( buf, idx) ) {
175157 if exp_number > 0x0001_0000_0000 {
176- return Err ( Error :: new_c (
177- idx,
178- get ! ( buf, idx) as char ,
179- ErrorType :: InvalidNumber ,
180- ) ) ;
158+ err ! ( idx, get!( buf, idx) ) ;
181159 }
182160 exp_number = 10 * exp_number + i64:: from ( get ! ( buf, idx) - b'0' ) ;
183161 idx += 1 ;
@@ -202,11 +180,7 @@ impl<'de> Deserializer<'de> {
202180 }
203181 }
204182 if is_structural_or_whitespace ( get ! ( buf, idx) ) == 0 {
205- return Err ( Error :: new_c (
206- idx,
207- get ! ( buf, idx) as char ,
208- ErrorType :: InvalidNumber ,
209- ) ) ;
183+ err ! ( idx, get!( buf, idx) ) ;
210184 }
211185 Ok ( f64_from_parts (
212186 !negative,
@@ -224,13 +198,11 @@ impl<'de> Deserializer<'de> {
224198 ErrorType :: InvalidNumber ,
225199 ) )
226200 } else {
227- let res = if negative {
201+ Ok ( if negative {
228202 StaticNode :: I64 ( unsafe { static_cast_i64 ! ( num. wrapping_neg( ) ) } )
229- // -(num as i64)
230203 } else {
231204 StaticNode :: U64 ( num)
232- } ;
233- Ok ( res)
205+ } )
234206 }
235207 }
236208}
@@ -362,11 +334,9 @@ fn f64_from_parts(
362334 } else {
363335 f *= get ! ( POW10 , exponent) ;
364336 }
365- let res = StaticNode :: F64 ( if positive { f } else { -f } ) ;
366- Ok ( res)
337+ Ok ( StaticNode :: F64 ( if positive { f } else { -f } ) )
367338 } else if significand == 0 {
368- let res = StaticNode :: F64 ( if positive { 0.0 } else { -0.0 } ) ;
369- Ok ( res)
339+ Ok ( StaticNode :: F64 ( if positive { 0.0 } else { -0.0 } ) )
370340 } else if ( -325 ..=308 ) . contains ( & exponent) {
371341 let ( factor_mantissa, factor_exponent) = get ! ( POW10_COMPONENTS , exponent + 325 ) ;
372342 let mut leading_zeroes = u64:: from ( significand. leading_zeros ( ) ) ;
@@ -421,8 +391,7 @@ fn f64_from_parts(
421391 ErrorType :: InvalidNumber ,
422392 ) )
423393 } else {
424- let res = StaticNode :: F64 ( res) ;
425- Ok ( res)
394+ Ok ( StaticNode :: F64 ( res) )
426395 }
427396 } else {
428397 let res = f64_from_parts_slow ( slice, offset) ?;
@@ -438,21 +407,12 @@ fn f64_from_parts_slow(slice: &[u8], offset: usize) -> Result<StaticNode> {
438407 match unsafe { std:: str:: from_utf8_unchecked ( slice) . parse :: < f64 > ( ) } {
439408 Ok ( val) => {
440409 if val. is_infinite ( ) {
441- return Err ( Error :: new_c (
442- offset,
443- get ! ( slice, 0 ) as char ,
444- ErrorType :: InvalidNumber ,
445- ) ) ;
410+ err ! ( offset, get!( slice, 0 ) ) ;
446411 }
447-
448412 Ok ( StaticNode :: F64 ( val) )
449413 }
450414 Err ( _) => {
451- return Err ( Error :: new_c (
452- offset,
453- get ! ( slice, offset) as char ,
454- ErrorType :: InvalidNumber ,
455- ) )
415+ err ! ( offset, get!( slice, offset) ) ;
456416 }
457417 }
458418}
0 commit comments