@@ -195,6 +195,7 @@ pub trait TextReader : Reader {
195195 let mut is_uint = false ;
196196 let mut is_negative = false ;
197197 let mut decimal_overflow = false ;
198+ let mut i64_overflow = false ;
198199
199200 let b = self . peek_byte ( ) ?;
200201 if b == b'+' {
@@ -207,6 +208,7 @@ pub trait TextReader : Reader {
207208 }
208209
209210 let ReadInt { value, digit_cnt, is_overflow, .. } = self . read_int ( 0 , false ) ?;
211+ i64_overflow = i64_overflow || is_overflow;
210212 decimal_overflow = decimal_overflow || is_overflow;
211213 if digit_cnt == 0 {
212214 return Err ( self . make_error ( "Number should contain at least one digit." , ReadErrorReason :: InvalidCharacter ) )
@@ -230,9 +232,10 @@ pub trait TextReader : Reader {
230232 is_decimal = true ;
231233 self . get_byte ( ) ?;
232234 let ReadInt { value, digit_cnt, is_overflow, .. } = self . read_int ( mantissa, true ) ?;
235+ i64_overflow = i64_overflow || is_overflow;
233236 decimal_overflow = decimal_overflow || is_overflow;
234237 mantissa = value;
235- if mantissa >= 0x80_0000_0000_0000 || mantissa < -0x80_0000_0000_0000 {
238+ if ! ( -0x80_0000_0000_0000 .. 0x80_0000_0000_0000 ) . contains ( & mantissa ) {
236239 decimal_overflow = true ;
237240 }
238241 dec_cnt = i64:: from ( digit_cnt) ;
@@ -245,6 +248,7 @@ pub trait TextReader : Reader {
245248 is_decimal = true ;
246249 self . get_byte ( ) ?;
247250 let ReadInt { value, digit_cnt, is_negative, is_overflow } = self . read_int ( 0 , false ) ?;
251+ i64_overflow = i64_overflow || is_overflow;
248252 decimal_overflow = decimal_overflow || is_overflow;
249253 exponent = value;
250254 if is_negative { exponent = -exponent; }
@@ -259,6 +263,13 @@ pub trait TextReader : Reader {
259263 let mantissa = if is_negative { -mantissa } else { mantissa } ;
260264 if is_decimal {
261265 if decimal_overflow {
266+ if !i64_overflow {
267+ #[ expect( clippy:: cast_possible_truncation, reason = "We hope that the new exponent is not big enough to truncate" ) ]
268+ let target_exp = ( exponent - dec_cnt) as i8 ;
269+ if let Some ( d) = Decimal :: try_new_with_precision_reduction ( mantissa, target_exp) {
270+ return Ok ( Value :: from ( d) ) ;
271+ }
272+ }
262273 return Err ( self . make_error ( "Not enough precision to read the Decimal" , ReadErrorReason :: NumericValueOverflow ) )
263274 }
264275 #[ expect( clippy:: cast_possible_truncation, reason = "We hope that the new exponent is not big enough to truncate" ) ]
0 commit comments