@@ -124,12 +124,18 @@ impl Radix {
124
124
125
125
#[ derive( Debug ) ]
126
126
pub ( super ) struct DigitInfo < ' a > {
127
- /// Characters of a literal between the radix prefix and type suffix.
128
- crate digits : & ' a str ,
129
127
/// Which radix the literal was represented in.
130
128
crate radix : Radix ,
131
129
/// The radix prefix, if present.
132
130
crate prefix : Option < & ' a str > ,
131
+
132
+ /// The integer part of the number.
133
+ integer : & ' a str ,
134
+ /// The fraction part of the number.
135
+ fraction : Option < & ' a str > ,
136
+ /// The character used as exponent seperator (b'e' or b'E') and the exponent part.
137
+ exponent : Option < ( char , & ' a str ) > ,
138
+
133
139
/// The type suffix, including preceding underscore if present.
134
140
crate suffix : Option < & ' a str > ,
135
141
/// True for floating-point literals.
@@ -158,6 +164,9 @@ impl<'a> DigitInfo<'a> {
158
164
( Some ( p) , s)
159
165
} ;
160
166
167
+ let mut digits = sans_prefix;
168
+ let mut suffix = None ;
169
+
161
170
let len = sans_prefix. len ( ) ;
162
171
let mut last_d = '\0' ;
163
172
for ( d_idx, d) in sans_prefix. char_indices ( ) {
@@ -168,36 +177,33 @@ impl<'a> DigitInfo<'a> {
168
177
|| ( ( d == 'E' || d == 'e' ) && !has_possible_float_suffix ( & sans_prefix) ) )
169
178
|| !float && ( d == 'i' || d == 'u' || is_possible_suffix_index ( & sans_prefix, suffix_start, len) )
170
179
{
171
- let ( digits, suffix) = sans_prefix. split_at ( suffix_start) ;
172
- return Self {
173
- digits,
174
- radix,
175
- prefix,
176
- suffix : Some ( suffix) ,
177
- float,
178
- } ;
180
+ let ( d, s) = sans_prefix. split_at ( suffix_start) ;
181
+ digits = d;
182
+ suffix = Some ( s) ;
183
+ break ;
179
184
}
180
185
last_d = d
181
186
}
182
187
183
- // No suffix found
188
+ let ( integer, fraction, exponent) = Self :: split_digit_parts ( digits, float) ;
189
+
184
190
Self {
185
- digits : sans_prefix,
186
191
radix,
187
192
prefix,
188
- suffix : None ,
193
+ integer,
194
+ fraction,
195
+ exponent,
196
+ suffix,
189
197
float,
190
198
}
191
199
}
192
200
193
- fn split_digit_parts ( & self ) -> ( & str , Option < & str > , Option < ( char , & str ) > ) {
194
- let digits = self . digits ;
195
-
201
+ fn split_digit_parts ( digits : & str , float : bool ) -> ( & str , Option < & str > , Option < ( char , & str ) > ) {
196
202
let mut integer = digits;
197
203
let mut fraction = None ;
198
204
let mut exponent = None ;
199
205
200
- if self . float {
206
+ if float {
201
207
for ( i, c) in digits. char_indices ( ) {
202
208
match c {
203
209
'.' => {
@@ -231,17 +237,21 @@ impl<'a> DigitInfo<'a> {
231
237
232
238
let group_size = self . radix . suggest_grouping ( ) ;
233
239
234
- let ( integer, fraction, exponent) = & self . split_digit_parts ( ) ;
240
+ Self :: group_digits (
241
+ & mut output,
242
+ self . integer ,
243
+ group_size,
244
+ true ,
245
+ self . radix == Radix :: Hexadecimal ,
246
+ ) ;
235
247
236
- Self :: group_digits ( & mut output, integer, group_size, true , self . radix == Radix :: Hexadecimal ) ;
237
-
238
- if let Some ( fraction) = fraction {
248
+ if let Some ( fraction) = self . fraction {
239
249
output. push ( '.' ) ;
240
250
Self :: group_digits ( & mut output, fraction, group_size, false , false ) ;
241
251
}
242
252
243
- if let Some ( ( separator, exponent) ) = exponent {
244
- output. push ( * separator) ;
253
+ if let Some ( ( separator, exponent) ) = self . exponent {
254
+ output. push ( separator) ;
245
255
Self :: group_digits ( & mut output, exponent, group_size, true , false ) ;
246
256
}
247
257
@@ -395,15 +405,13 @@ impl LiteralDigitGrouping {
395
405
}
396
406
}
397
407
398
- let ( integer, fraction, _) = digit_info. split_digit_parts( ) ;
399
-
400
- let integral_group_size = Self :: get_group_size( integer. split( '_' ) , in_macro) ?;
401
- if let Some ( fraction) = fraction {
408
+ let integral_group_size = Self :: get_group_size( digit_info. integer. split( '_' ) , in_macro) ?;
409
+ if let Some ( fraction) = digit_info. fraction {
402
410
let fractional_group_size = Self :: get_group_size( fraction. rsplit( '_' ) , in_macro) ?;
403
411
404
412
let consistent = Self :: parts_consistent( integral_group_size,
405
413
fractional_group_size,
406
- integer. len( ) ,
414
+ digit_info . integer. len( ) ,
407
415
fraction. len( ) ) ;
408
416
if !consistent {
409
417
return Err ( WarningType :: InconsistentDigitGrouping ) ;
@@ -503,7 +511,7 @@ impl DecimalLiteralRepresentation {
503
511
then {
504
512
let hex = format!( "{:#X}" , val) ;
505
513
let digit_info = DigitInfo :: new( & hex, false ) ;
506
- let _ = Self :: do_lint( digit_info. digits ) . map_err( |warning_type| {
514
+ let _ = Self :: do_lint( digit_info. integer ) . map_err( |warning_type| {
507
515
warning_type. display( & digit_info. grouping_hint( ) , cx, lit. span)
508
516
} ) ;
509
517
}
0 commit comments