thousands/
display.rs

1use std::fmt::Display;
2
3use super::{Separable, SeparatorPolicy};
4use super::helpers::SeparatorIterator;
5
6impl Separable for str {
7    fn separate_by_policy(&self, policy: SeparatorPolicy) -> String {
8        let (before, number, after, count) = find_span(&self, |c| policy.digits.contains(&c));
9        let iter = SeparatorIterator::new(&policy, count);
10
11        let mut result = String::with_capacity(self.len() + iter.sep_len());
12
13        result.push_str(before);
14
15        for (digit, comma_after) in number.chars().zip(iter) {
16            result.push(digit);
17            if comma_after {
18                result.push_str(policy.separator);
19            }
20        }
21
22        result.push_str(after);
23
24        result
25    }
26}
27
28impl<T: Display> Separable for T {
29    fn separate_by_policy(&self, policy: SeparatorPolicy) -> String {
30        self.to_string().as_str().separate_by_policy(policy)
31    }
32}
33
34fn find_span<F: Fn(char) -> bool>(s: &str, is_digit: F) -> (&str, &str, &str, usize) {
35    let start        = len_not_matching(s, &is_digit);
36    let (len, count) = len_and_count_matching(&s[start ..], &is_digit);
37    let limit        = start + len;
38
39    (&s[.. start], &s[start .. limit], &s[limit ..], count)
40}
41
42fn len_not_matching<F>(s: &str, mut pred: F) -> usize
43where F: FnMut(char) -> bool {
44
45    if let Some((i, _)) = s.char_indices().find(|p| pred(p.1)) {
46        i
47    } else {
48        s.len()
49    }
50}
51
52fn len_and_count_matching<F>(s: &str, pred: F) -> (usize, usize)
53where F: Fn(char) -> bool {
54
55    let mut count = 0;
56    let     len = len_not_matching(s, |c|
57        if pred(c) {
58            count += 1;
59            false
60        } else {
61            true
62        });
63
64    (len, count)
65}
66
67#[cfg(test)]
68mod test {
69    use super::super::{Separable, SeparatorPolicy, digits, policies};
70
71    #[test]
72    fn integer_thousands_commas() {
73        assert_eq!( "12345".separate_with_commas(),
74                    "12,345" );
75    }
76
77    #[test]
78    fn smilies() {
79        let policy = SeparatorPolicy {
80            separator: "😃😃",
81            groups:    &[1],
82            digits:    &['🙁'],
83        };
84
85        assert_eq!( "  🙁🙁🙁🙁🙁  ".separate_by_policy(policy),
86                    "  🙁😃😃🙁😃😃🙁😃😃🙁😃😃🙁  " );
87    }
88
89    #[test]
90    fn three_two_two_two() {
91        let policy = SeparatorPolicy {
92            separator: ",",
93            groups:    &[3, 2],
94            digits:    &digits::ASCII_DECIMAL,
95        };
96
97        assert_eq!( "1234567890".separate_by_policy(policy),
98                    "1,23,45,67,890" );
99    }
100
101    #[test]
102    fn minus_sign_and_decimal_point() {
103        assert_eq!( "-1234.5".separate_with_commas(),
104                    "-1,234.5" );
105    }
106
107    #[test]
108    fn hex_four() {
109        assert_eq!( "deadbeef".separate_by_policy(policies::HEX_FOUR),
110                    "dead beef" );
111    }
112}