Compute digits vec only once

This commit is contained in:
Terry Raimondo 2018-06-03 12:26:50 +02:00
parent ed011c45c4
commit 2c1451817c

View File

@ -193,18 +193,19 @@ impl<'a> DigitInfo<'a> {
self.suffix.unwrap_or("")
)
} else {
let mut hint = self.digits
let filtered_digits_vec = self.digits
.chars()
.rev()
.filter(|&c| c != '_')
.collect::<Vec<_>>()
.rev()
.collect::<Vec<_>>();
let mut hint = filtered_digits_vec
.chunks(group_size)
.map(|chunk| chunk.into_iter().rev().collect())
.rev()
.collect::<Vec<String>>()
.join("_");
// Forces hexadecimal values to be grouped by 4 being filled with zeroes (e.g 0x00ab_cdef)
let nb_digits_to_fill = self.digits.chars().filter(|&c| c != '_').collect::<Vec<_>>().len() % 4;
let nb_digits_to_fill = filtered_digits_vec.len() % 4;
if self.radix == Radix::Hexadecimal && nb_digits_to_fill != 0 {
hint = format!("{:0>4}{}", &hint[..nb_digits_to_fill], &hint[nb_digits_to_fill..]);
}