]> git.lizzy.rs Git - rust.git/commitdiff
Implement method for digit grouping suggestions
authorJay Hardee <hardeejj9@gmail.com>
Sun, 30 Jul 2017 22:37:11 +0000 (18:37 -0400)
committerJay Hardee <hardeejj9@gmail.com>
Mon, 31 Jul 2017 23:06:51 +0000 (19:06 -0400)
clippy_lints/src/literal_digit_grouping.rs

index 9b26cc53d471c5d56948e7f07557d2816555f185..5bc9190b1f5642e275894a75ca62ee072d9b150a 100644 (file)
@@ -140,6 +140,49 @@ pub fn new(lit: &str, float: bool) -> DigitInfo {
             float: float,
         }
     }
+
+    /// Returns digits grouped in a sensible way.
+    fn grouping_hint(&self) -> String {
+        let group_size = self.radix.suggest_grouping();
+        if self.digits.contains('.') {
+            let mut parts = self.digits.split(".");
+            let int_part_hint = parts
+                .next()
+                .unwrap()
+                .chars()
+                .rev()
+                .filter(|&c| c != '_')
+                .collect::<Vec<_>>()
+                .chunks(group_size)
+                .map(|chunk| chunk.into_iter().rev().collect())
+                .rev()
+                .collect::<Vec<String>>()
+                .join("_");
+            let frac_part_hint = parts
+                .next()
+                .unwrap()
+                .chars()
+                .filter(|&c| c != '_')
+                .collect::<Vec<_>>()
+                .chunks(group_size)
+                .map(|chunk| chunk.into_iter().collect())
+                .collect::<Vec<String>>()
+                .join("_");
+            format!("{}.{}{}", int_part_hint, frac_part_hint, self.suffix.unwrap_or(""))
+        } else {
+            let hint = self.digits
+                .chars()
+                .rev()
+                .filter(|&c| c != '_')
+                .collect::<Vec<_>>()
+                .chunks(group_size)
+                .map(|chunk| chunk.into_iter().rev().collect())
+                .rev()
+                .collect::<Vec<String>>()
+                .join("_");
+            format!("{}{}{}", self.prefix.unwrap_or(""), hint, self.suffix.unwrap_or(""))
+        }
+    }
 }
 
 enum WarningType {