| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! Algorithms to determine where to position grouping separators. |
| 6 |
|
| 7 |
use crate::options::GroupingStrategy; |
| 8 |
use crate::provider::GroupingSizes; |
| 9 |
use core::cmp; |
| 10 |
|
| 11 |
/// Returns whether to display a grouping separator at the given magnitude. |
| 12 |
/// |
| 13 |
/// `upper_magnitude` is the magnitude of the highest-power digit, used for resolving minimum |
| 14 |
/// grouping digits. |
| 15 |
pub fn check( |
| 16 |
upper_magnitude: i16, |
| 17 |
magnitude: i16, |
| 18 |
strategy: GroupingStrategy, |
| 19 |
sizes: GroupingSizes, |
| 20 |
) -> bool { |
| 21 |
let primary = if sizes.primary == 0 { |
| 22 |
return false; |
| 23 |
} else { |
| 24 |
sizes.primary as i16 |
| 25 |
}; |
| 26 |
if magnitude < primary { |
| 27 |
return false; |
| 28 |
} |
| 29 |
let min_grouping = { |
| 30 |
use GroupingStrategy::*; |
| 31 |
match strategy { |
| 32 |
Never => return false, |
| 33 |
// Note: Auto and Always are the same for DecimalFormatter. |
| 34 |
// When currencies are implemented, this will change. |
| 35 |
Auto | Always => cmp::max(1, sizes.min_grouping) as i16, |
| 36 |
Min2 => cmp::max(2, sizes.min_grouping) as i16, |
| 37 |
} |
| 38 |
}; |
| 39 |
if upper_magnitude < primary + min_grouping - 1 { |
| 40 |
return false; |
| 41 |
} |
| 42 |
let secondary = if sizes.secondary == 0 { |
| 43 |
primary |
| 44 |
} else { |
| 45 |
sizes.secondary as i16 |
| 46 |
}; |
| 47 |
let magnitude_prime = magnitude - primary; |
| 48 |
if magnitude_prime % secondary == 0 { |
| 49 |
return true; |
| 50 |
} |
| 51 |
false |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_grouper() { |
| 56 |
use crate::DecimalFormatter; |
| 57 |
use crate::input::Decimal; |
| 58 |
use crate::options; |
| 59 |
use crate::provider::*; |
| 60 |
use icu_provider::prelude::*; |
| 61 |
use std::cell::RefCell; |
| 62 |
use writeable::assert_writeable_eq; |
| 63 |
|
| 64 |
let western_sizes = GroupingSizes { |
| 65 |
min_grouping: 1, |
| 66 |
primary: 3, |
| 67 |
secondary: 3, |
| 68 |
}; |
| 69 |
let indic_sizes = GroupingSizes { |
| 70 |
min_grouping: 1, |
| 71 |
primary: 3, |
| 72 |
secondary: 2, |
| 73 |
}; |
| 74 |
let western_sizes_min3 = GroupingSizes { |
| 75 |
min_grouping: 3, |
| 76 |
primary: 3, |
| 77 |
secondary: 3, |
| 78 |
}; |
| 79 |
|
| 80 |
// primary=0 implies no grouping; the other fields are ignored |
| 81 |
let zero_test = GroupingSizes { |
| 82 |
min_grouping: 0, |
| 83 |
primary: 0, |
| 84 |
secondary: 0, |
| 85 |
}; |
| 86 |
|
| 87 |
// secondary=0 implies that it inherits from primary |
| 88 |
let blank_secondary = GroupingSizes { |
| 89 |
min_grouping: 0, |
| 90 |
primary: 3, |
| 91 |
secondary: 0, |
| 92 |
}; |
| 93 |
|
| 94 |
#[derive(Debug)] |
| 95 |
struct TestCase { |
| 96 |
strategy: GroupingStrategy, |
| 97 |
sizes: GroupingSizes, |
| 98 |
// Expected results for numbers with magnitude 3, 4, 5, and 6 |
| 99 |
expected: [&'static str; 4], |
| 100 |
} |
| 101 |
let cases = [ |
| 102 |
TestCase { |
| 103 |
strategy: GroupingStrategy::Auto, |
| 104 |
sizes: western_sizes, |
| 105 |
expected: ["1,000", "10,000", "100,000", "1,000,000"], |
| 106 |
}, |
| 107 |
TestCase { |
| 108 |
strategy: GroupingStrategy::Min2, |
| 109 |
sizes: western_sizes, |
| 110 |
expected: ["1000", "10,000", "100,000", "1,000,000"], |
| 111 |
}, |
| 112 |
TestCase { |
| 113 |
strategy: GroupingStrategy::Auto, |
| 114 |
sizes: indic_sizes, |
| 115 |
expected: ["1,000", "10,000", "1,00,000", "10,00,000"], |
| 116 |
}, |
| 117 |
TestCase { |
| 118 |
strategy: GroupingStrategy::Min2, |
| 119 |
sizes: indic_sizes, |
| 120 |
expected: ["1000", "10,000", "1,00,000", "10,00,000"], |
| 121 |
}, |
| 122 |
TestCase { |
| 123 |
strategy: GroupingStrategy::Auto, |
| 124 |
sizes: western_sizes_min3, |
| 125 |
expected: ["1000", "10000", "100,000", "1,000,000"], |
| 126 |
}, |
| 127 |
TestCase { |
| 128 |
strategy: GroupingStrategy::Min2, |
| 129 |
sizes: western_sizes_min3, |
| 130 |
expected: ["1000", "10000", "100,000", "1,000,000"], |
| 131 |
}, |
| 132 |
TestCase { |
| 133 |
strategy: GroupingStrategy::Auto, |
| 134 |
sizes: zero_test, |
| 135 |
expected: ["1000", "10000", "100000", "1000000"], |
| 136 |
}, |
| 137 |
TestCase { |
| 138 |
strategy: GroupingStrategy::Min2, |
| 139 |
sizes: zero_test, |
| 140 |
expected: ["1000", "10000", "100000", "1000000"], |
| 141 |
}, |
| 142 |
TestCase { |
| 143 |
strategy: GroupingStrategy::Auto, |
| 144 |
sizes: blank_secondary, |
| 145 |
expected: ["1,000", "10,000", "100,000", "1,000,000"], |
| 146 |
}, |
| 147 |
TestCase { |
| 148 |
strategy: GroupingStrategy::Min2, |
| 149 |
sizes: blank_secondary, |
| 150 |
expected: ["1000", "10,000", "100,000", "1,000,000"], |
| 151 |
}, |
| 152 |
]; |
| 153 |
for cas in &cases { |
| 154 |
for i in 0..4 { |
| 155 |
let dec = { |
| 156 |
let mut dec = Decimal::from(1); |
| 157 |
dec.multiply_pow10((i as i16) + 3); |
| 158 |
dec |
| 159 |
}; |
| 160 |
let symbols = DecimalSymbols { |
| 161 |
grouping_sizes: cas.sizes, |
| 162 |
..DecimalSymbols::new_en_for_testing() |
| 163 |
}; |
| 164 |
let digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; |
| 165 |
struct Provider(RefCell<Option<DecimalSymbols<'static>>>, [char; 10]); |
| 166 |
impl DataProvider<DecimalSymbolsV1> for Provider { |
| 167 |
fn load( |
| 168 |
&self, |
| 169 |
_req: DataRequest, |
| 170 |
) -> Result<DataResponse<DecimalSymbolsV1>, DataError> { |
| 171 |
Ok(DataResponse { |
| 172 |
metadata: Default::default(), |
| 173 |
payload: DataPayload::from_owned( |
| 174 |
self.0 |
| 175 |
.borrow_mut() |
| 176 |
.take() |
| 177 |
// We only have one payload |
| 178 |
.ok_or(DataErrorKind::Custom.into_error())?, |
| 179 |
), |
| 180 |
}) |
| 181 |
} |
| 182 |
} |
| 183 |
impl DataProvider<DecimalDigitsV1> for Provider { |
| 184 |
fn load( |
| 185 |
&self, |
| 186 |
_req: DataRequest, |
| 187 |
) -> Result<DataResponse<DecimalDigitsV1>, DataError> { |
| 188 |
Ok(DataResponse { |
| 189 |
metadata: Default::default(), |
| 190 |
payload: DataPayload::from_owned(self.1), |
| 191 |
}) |
| 192 |
} |
| 193 |
} |
| 194 |
let provider = Provider(RefCell::new(Some(symbols)), digits); |
| 195 |
let options = options::DecimalFormatterOptions { |
| 196 |
grouping_strategy: Some(cas.strategy), |
| 197 |
..Default::default() |
| 198 |
}; |
| 199 |
let formatter = |
| 200 |
DecimalFormatter::try_new_unstable(&provider, Default::default(), options).unwrap(); |
| 201 |
let actual = formatter.format(&dec); |
| 202 |
assert_writeable_eq!(actual, cas.expected[i], "{:?}", cas); |
| 203 |
} |
| 204 |
} |
| 205 |
} |