thousands/policies.rs
1use super::digits::*;
2
3/// A policy for inserting separators into numbers.
4///
5/// The configurable aspects are:
6///
7/// - The separator character to insert.
8///
9/// - How to group the separators.
10///
11/// - What characters are considered digits (for skipping non-digits such as
12/// a minus sign).
13#[derive(Debug, Clone, Copy)]
14pub struct SeparatorPolicy<'a> {
15 /// The separator to insert.
16 pub separator: &'a str,
17 /// The grouping. The numbers in this array give the size of the groups, from
18 /// right to left, with the last number in the array giving the size of all
19 /// subsequent groups.
20 ///
21 /// So to group by threes, as is typical in many places,
22 /// this array should be `&[3]`. However, to get a grouping like `1,23,45,678`,
23 /// where the last group has size three and the others size two, you would use
24 /// `&[3, 2]`.
25 pub groups: &'a [u8],
26 /// The characters that are considered digits. If there are multiple groups of
27 /// digits separated by non-digits, we only add separators to the first group.
28 /// This means, for example, that the number `-12345.67` will only have separators
29 /// inserted into the `12345` portion.
30 pub digits: &'a [char],
31}
32
33/// Policy for placing a comma every three decimal digits.
34pub const COMMA_SEPARATOR: SeparatorPolicy = SeparatorPolicy {
35 separator: ",",
36 groups: &[3],
37 digits: ASCII_DECIMAL,
38};
39
40/// Policy for placing a space every three decimal digits.
41pub const SPACE_SEPARATOR: SeparatorPolicy = SeparatorPolicy {
42 separator: " ",
43 groups: &[3],
44 digits: ASCII_DECIMAL,
45};
46
47/// Policy for placing a period every three decimal digits.
48pub const DOT_SEPARATOR: SeparatorPolicy = SeparatorPolicy {
49 separator: ".",
50 groups: &[3],
51 digits: ASCII_DECIMAL,
52};
53
54/// Policy for placing an underscore every three decimal digits.
55pub const UNDERSCORE_SEPARATOR: SeparatorPolicy = SeparatorPolicy {
56 separator: "_",
57 groups: &[3],
58 digits: ASCII_DECIMAL,
59};
60
61/// Policy for placing a space every four hexadecimal digits.
62pub const HEX_FOUR: SeparatorPolicy = SeparatorPolicy {
63 separator: " ",
64 groups: &[4],
65 digits: ASCII_HEXADECIMAL,
66};