Skip to content

Commit abf62d8

Browse files
author
Michael Wright
committed
literal representation restructure 6
Add `group_digits` helper function.
1 parent ec664e8 commit abf62d8

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -233,53 +233,16 @@ impl<'a> DigitInfo<'a> {
233233

234234
let (integer, fraction, exponent) = &self.split_digit_parts();
235235

236-
let int_digits: Vec<_> = integer.chars().rev().filter(|&c| c != '_').collect();
237-
let int_part_hint = int_digits
238-
.chunks(group_size)
239-
.map(|chunk| chunk.iter().rev().collect())
240-
.rev()
241-
.collect::<Vec<String>>()
242-
.join("_");
243-
244-
// Pad leading hexidecimal group with zeros
245-
if self.radix == Radix::Hexadecimal {
246-
debug_assert!(group_size > 0);
247-
let first_group_size = (int_digits.len() + group_size - 1) % group_size + 1;
248-
for _ in 0..group_size - first_group_size {
249-
output.push('0');
250-
}
251-
}
252-
253-
output.push_str(&int_part_hint);
236+
Self::group_digits(&mut output, integer, group_size, true, self.radix == Radix::Hexadecimal);
254237

255238
if let Some(fraction) = fraction {
256-
let frac_part_hint = fraction
257-
.chars()
258-
.filter(|&c| c != '_')
259-
.collect::<Vec<_>>()
260-
.chunks(group_size)
261-
.map(|chunk| chunk.iter().collect())
262-
.collect::<Vec<String>>()
263-
.join("_");
264-
265239
output.push('.');
266-
output.push_str(&frac_part_hint);
240+
Self::group_digits(&mut output, fraction, group_size, false, false);
267241
}
268242

269243
if let Some((separator, exponent)) = exponent {
270-
let after_e_hint = exponent
271-
.chars()
272-
.rev()
273-
.filter(|&c| c != '_')
274-
.collect::<Vec<_>>()
275-
.chunks(group_size)
276-
.map(|chunk| chunk.iter().rev().collect())
277-
.rev()
278-
.collect::<Vec<String>>()
279-
.join("_");
280-
281244
output.push(*separator);
282-
output.push_str(&after_e_hint);
245+
Self::group_digits(&mut output, exponent, group_size, true, false);
283246
}
284247

285248
if let Some(suffix) = self.suffix {
@@ -296,6 +259,38 @@ impl<'a> DigitInfo<'a> {
296259

297260
output
298261
}
262+
263+
fn group_digits(output: &mut String, input: &str, group_size: usize, partial_group_first: bool, pad: bool) {
264+
debug_assert!(group_size > 0);
265+
266+
let mut digits = input.chars().filter(|&c| c != '_');
267+
268+
let first_group_size;
269+
270+
if partial_group_first {
271+
first_group_size = (digits.clone().count() + group_size - 1) % group_size + 1;
272+
if pad {
273+
for _ in 0..group_size - first_group_size {
274+
output.push('0');
275+
}
276+
}
277+
} else {
278+
first_group_size = group_size;
279+
}
280+
281+
for _ in 0..first_group_size {
282+
if let Some(digit) = digits.next() {
283+
output.push(digit);
284+
}
285+
}
286+
287+
for (c, i) in digits.zip((0..group_size).cycle()) {
288+
if i == 0 {
289+
output.push('_');
290+
}
291+
output.push(c);
292+
}
293+
}
299294
}
300295

301296
enum WarningType {

0 commit comments

Comments
 (0)