@@ -87,20 +87,23 @@ fn dynamically_format_decimal(
87
87
88
88
// number of zeros between most significant digit and decimal point
89
89
let leading_zero_count = this. scale
90
- . to_usize ( )
91
- . and_then ( |scale| scale. checked_sub ( abs_int. len ( ) ) )
90
+ . to_u64 ( )
91
+ . and_then ( |scale| scale. checked_sub ( abs_int. len ( ) as u64 ) )
92
92
. unwrap_or ( 0 ) ;
93
93
94
94
// number of zeros between least significant digit and decimal point
95
95
let trailing_zero_count = this. scale
96
96
. checked_neg ( )
97
- . and_then ( |d| d. to_usize ( ) ) ;
97
+ . and_then ( |d| d. to_u64 ( ) ) ;
98
98
99
99
// this ignores scientific-formatting if precision is requested
100
100
let trailing_zeros = f. precision ( ) . map ( |_| 0 )
101
101
. or ( trailing_zero_count)
102
102
. unwrap_or ( 0 ) ;
103
103
104
+ let leading_zero_threshold = leading_zero_threshold as u64 ;
105
+ let trailing_zero_threshold = trailing_zero_threshold as u64 ;
106
+
104
107
// use exponential form if decimal point is outside
105
108
// the upper and lower thresholds of the decimal
106
109
if leading_zero_threshold < leading_zero_count {
@@ -128,14 +131,14 @@ fn format_full_scale(
128
131
debug_assert_ne ! ( digits. len( ) , 0 ) ;
129
132
130
133
if this. scale <= 0 {
131
- // formating an integer value (add trailing zeros to the right)
134
+ // formatting an integer value (add trailing zeros to the right)
132
135
zero_right_pad_integer_ascii_digits ( & mut digits, & mut exp, f. precision ( ) ) ;
133
136
} else {
134
- let scale = this. scale as usize ;
137
+ let scale = this. scale as u64 ;
135
138
// no-precision behaves the same as precision matching scale (i.e. no padding or rounding)
136
- let prec = f. precision ( ) . unwrap_or ( scale) ;
139
+ let prec = f. precision ( ) . and_then ( |prec| prec . to_u64 ( ) ) . unwrap_or ( scale) ;
137
140
138
- if scale < digits. len ( ) {
141
+ if scale < digits. len ( ) as u64 {
139
142
// format both integer and fractional digits (always 'trim' to precision)
140
143
trim_ascii_digits ( & mut digits, scale, prec, & mut exp, this. sign ) ;
141
144
} else {
@@ -151,7 +154,7 @@ fn format_full_scale(
151
154
152
155
// add exp part to buffer (if not zero)
153
156
if exp != 0 {
154
- write ! ( buf, "E {:+}" , exp) ?;
157
+ write ! ( buf, "e {:+}" , exp) ?;
155
158
}
156
159
157
160
// write buffer to formatter
@@ -169,7 +172,10 @@ fn zero_right_pad_integer_ascii_digits(
169
172
) {
170
173
debug_assert ! ( * exp >= 0 ) ;
171
174
172
- let trailing_zero_count = exp. to_usize ( ) . unwrap ( ) ;
175
+ let trailing_zero_count = match exp. to_usize ( ) {
176
+ Some ( n) => n,
177
+ None => { return ; }
178
+ } ;
173
179
let total_additional_zeros = trailing_zero_count. saturating_add ( precision. unwrap_or ( 0 ) ) ;
174
180
if total_additional_zeros > FMT_MAX_INTEGER_PADDING {
175
181
return ;
@@ -195,16 +201,20 @@ fn zero_right_pad_integer_ascii_digits(
195
201
/// Fill zeros into utf-8 digits
196
202
fn trim_ascii_digits (
197
203
digits : & mut Vec < u8 > ,
198
- scale : usize ,
199
- prec : usize ,
204
+ scale : u64 ,
205
+ prec : u64 ,
200
206
exp : & mut i128 ,
201
207
sign : Sign ,
202
208
) {
203
- debug_assert ! ( scale < digits. len( ) ) ;
209
+ debug_assert ! ( scale < digits. len( ) as u64 ) ;
204
210
// there are both integer and fractional digits
205
- let integer_digit_count = digits. len ( ) - scale;
211
+ let integer_digit_count = ( digits. len ( ) as u64 - scale)
212
+ . to_usize ( )
213
+ . expect ( "Number of digits exceeds maximum usize" ) ;
206
214
207
215
if prec < scale {
216
+ let prec = prec. to_usize ( )
217
+ . expect ( "Precision exceeds maximum usize" ) ;
208
218
apply_rounding_to_ascii_digits (
209
219
digits, exp, integer_digit_count + prec, sign
210
220
) ;
@@ -215,30 +225,34 @@ fn trim_ascii_digits(
215
225
}
216
226
217
227
if scale < prec {
228
+ let trailing_zero_count = ( prec - scale)
229
+ . to_usize ( )
230
+ . expect ( "Too Big" ) ;
231
+
218
232
// precision required beyond scale
219
- digits. resize ( digits. len ( ) + ( prec - scale ) , b'0' ) ;
233
+ digits. resize ( digits. len ( ) + trailing_zero_count , b'0' ) ;
220
234
}
221
235
}
222
236
223
237
224
238
fn shift_or_trim_fractional_digits (
225
239
digits : & mut Vec < u8 > ,
226
- scale : usize ,
227
- prec : usize ,
240
+ scale : u64 ,
241
+ prec : u64 ,
228
242
exp : & mut i128 ,
229
243
sign : Sign ,
230
244
) {
231
- debug_assert ! ( scale >= digits. len( ) ) ;
245
+ debug_assert ! ( scale >= digits. len( ) as u64 ) ;
232
246
// there are no integer digits
233
- let leading_zeros = scale - digits. len ( ) ;
247
+ let leading_zeros = scale - digits. len ( ) as u64 ;
234
248
235
249
match prec. checked_sub ( leading_zeros) {
236
250
None => {
237
251
digits. clear ( ) ;
238
252
digits. push ( b'0' ) ;
239
253
if prec > 0 {
240
254
digits. push ( b'.' ) ;
241
- digits. resize ( 2 + prec, b'0' ) ;
255
+ digits. resize ( 2 + prec as usize , b'0' ) ;
242
256
}
243
257
}
244
258
Some ( 0 ) => {
@@ -251,11 +265,15 @@ fn shift_or_trim_fractional_digits(
251
265
if leading_zeros != 0 {
252
266
digits. push ( b'0' ) ;
253
267
digits. push ( b'.' ) ;
254
- digits. resize ( 1 + leading_zeros, b'0' ) ;
268
+ digits. resize ( 1 + leading_zeros as usize , b'0' ) ;
255
269
}
256
270
digits. push ( rounded_value + b'0' ) ;
257
271
}
258
272
Some ( digit_prec) => {
273
+ let digit_prec = digit_prec as usize ;
274
+ let leading_zeros = leading_zeros
275
+ . to_usize ( )
276
+ . expect ( "Number of leading zeros exceeds max usize" ) ;
259
277
let trailing_zeros = digit_prec. saturating_sub ( digits. len ( ) ) ;
260
278
if digit_prec < digits. len ( ) {
261
279
apply_rounding_to_ascii_digits ( digits, exp, digit_prec, sign) ;
@@ -863,8 +881,8 @@ mod test {
863
881
}
864
882
865
883
impl_case ! ( fmt_default: "{}" => "1e+100000" ) ;
866
- impl_case ! ( fmt_d1: "{:.1}" => "1E +100000" ) ;
867
- impl_case ! ( fmt_d4: "{:.4}" => "1E +100000" ) ;
884
+ impl_case ! ( fmt_d1: "{:.1}" => "1e +100000" ) ;
885
+ impl_case ! ( fmt_d4: "{:.4}" => "1e +100000" ) ;
868
886
}
869
887
870
888
@@ -905,7 +923,7 @@ mod test {
905
923
}
906
924
907
925
impl_case ! ( fmt_default: "{}" => "13400476439814628800e+2502" ) ;
908
- impl_case ! ( fmt_d1: "{:.1}" => "13400476439814628800E +2502" ) ;
926
+ impl_case ! ( fmt_d1: "{:.1}" => "13400476439814628800e +2502" ) ;
909
927
}
910
928
}
911
929
0 commit comments