icu_provider_source/decimal/compact_decimal_pattern.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use crate::cldr_serde::numbers::DecimalFormat;
use icu::experimental::compactdecimal::provider::CompactDecimalPatternDataV1;
use icu::experimental::compactdecimal::provider::*;
use itertools::Itertools;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use zerovec::ule::encode_varule_to_box;
/// A [`ParsedPattern`] represents a compact decimal pattern, which consists of
/// literal text with an optional placeholder. The literal text is unescaped,
/// and the information about the number of 0s in the placeholder is stored
/// separately.
#[derive(PartialEq, Clone)]
struct ParsedPattern {
/// The unescaped literal text, e.g., " mille" for the pattern "00 mille",
/// "mille" for the pattern "mille".
pub(crate) literal_text: Cow<'static, str>,
/// The placeholder; `None` for patterns such as "mille".
pub(crate) placeholder: Option<ParsedPlaceholder>,
}
/// Represents the placeholder in a compact decimal pattern as its position in
/// the associated text and the number of 0s (which, together with the type
/// associated with the pattern, determines the power of ten being abbreviated).
#[derive(PartialEq, Clone)]
struct ParsedPlaceholder {
/// The position in the literal text where the placeholder is to be inserted;
/// in particular, this is 0 for insertion at the beginning, which is the
/// most frequent case, as in "00 mille".
pub(crate) index: usize,
pub(crate) number_of_0s: i8,
}
/// Parses a compact decimal pattern string, performing any validation that can
/// be done without the context of the associated type and count.
fn parse(pattern: &str) -> Result<Option<ParsedPattern>, Cow<'static, str>> {
let cldr_overrides: HashMap<String, String> = [
// Unescaped - in yrl (Nheengatu).
("0 millón-ita", "0 millón'-'ita"),
("0 billón-ita", "0 billón'-'ita"),
("0 tirillón-ita", "0 tirillón'-'ita"),
("0 miliãu-ita", "0 miliãu'-'ita"),
("0 biliãu-ita", "0 biliãu'-'ita"),
("0 tiriliãu-ita", "0 tiriliãu'-'ita"),
// All compact decimal patterns for sw (Swahili) are split by sign;
// the sign ends up where it would be as part of the significand, so
// this special handling is unneeded. Depending on the region subtag,
// the space may be breaking or nonbreaking.
("elfu 0;elfu -0", "elfu 0"),
("milioni 0;milioni -0", "milioni 0"),
("bilioni 0;bilioni -0", "bilioni 0"),
("trilioni 0;trilioni -0", "trilioni 0"),
("elfu\u{A0}0;elfu\u{A0}-0", "elfu\u{A0}0"),
("milioni\u{A0}0;milioni\u{A0}-0", "milioni\u{A0}0"),
("bilioni\u{A0}0;bilioni\u{A0}-0", "bilioni\u{A0}0"),
("trilioni\u{A0}0;trilioni\u{A0}-0", "trilioni\u{A0}0"),
("0M;-0M", "0M"),
("0B;-0B", "0B"),
("0T;-0T", "0B"),
// Unescaped E in hu (Hungarian).
("0\u{A0}E", "0\u{A0}'E'"),
]
.iter()
.flat_map(|(key, value)| {
[
(key.to_string(), value.to_string()),
(key.replace('0', "00"), value.replace('0', "00")),
(key.replace('0', "000"), value.replace('0', "000")),
]
})
.collect();
let pattern = cldr_overrides
.get(pattern)
.map(|s| s.as_str())
.unwrap_or(pattern);
if pattern == "0" {
Ok(None)
} else {
let mut placeholder: Option<ParsedPlaceholder> = None;
let mut literal_text = String::with_capacity(pattern.len());
// CLDR patterns use quoting for escaping, thus '.' for a literal FULL
// STOP, as opposed to . for the decimal separator. A doubled
// APOSTROPHE ('') represents a single one.
// See https://www.unicode.org/reports/tr35/tr35-numbers.html#Special_Pattern_Characters.
// We process the pattern in chunks delimited by ', which are
// alternatingly unescaped and escaped.
for (i, chunk) in pattern.split('\'').enumerate() {
let escaped = i % 2 == 1;
if escaped {
if chunk.is_empty() {
// '' means '.
literal_text.push('\'');
} else {
// Anything else wrapped in apostrophes is literal text.
literal_text.push_str(chunk);
}
} else {
// We are in unquoted text, so we need to check for the
// symbols defined in https://www.unicode.org/reports/tr35/tr35-numbers.html#Number_Pattern_Character_Definitions.
if chunk
.chars()
.any(|c| ('1'..'9').contains(&c) || "@#.-,E+%‰,¤*'".contains(c))
{
return Err(
format!("Unsupported symbol in compact decimal pattern {pattern}").into(),
);
}
// Given the chunk "me0000w", the prefix is "me", and
// additional_0s_and_suffix is "000w"; given the chunk
// "me0w", these are "me" and "w" respectively.
if let Some((prefix, additional_0s_and_suffix)) = chunk.split_once('0') {
if placeholder.is_some() {
return Err(format!(
"Multiple placeholders in compact decimal pattern {pattern})"
)
.into());
}
// The prefix goes into the literal text, and the position
// of the placeholder is then at the end of the accumulated
// text, at literal_text.len().
literal_text.push_str(prefix);
if let Some((middle_0s, suffix)) = additional_0s_and_suffix.rsplit_once('0') {
// More than one 0; in the "me0000w" example, middle_0s
// is "00", suffix is "w".
if !middle_0s.chars().all(|c| c == '0') {
return Err(format!(
"Multiple placeholders in compact decimal pattern {pattern}"
)
.into());
}
placeholder = Some(ParsedPlaceholder {
index: literal_text.len(),
number_of_0s: i8::try_from(middle_0s.len() + 2)
.map_err(|_| format!("Too many 0s in pattern {pattern}"))?,
});
literal_text.push_str(suffix);
} else {
// Only one 0, we are in the "me0w" case.
placeholder = Some(ParsedPlaceholder {
index: literal_text.len(),
number_of_0s: 1,
});
literal_text.push_str(additional_0s_and_suffix);
}
} else {
// No symbols, all literal text.
literal_text.push_str(chunk);
}
}
}
Ok(Some(ParsedPattern {
literal_text: Cow::Owned(literal_text),
placeholder,
}))
}
}
impl TryFrom<&DecimalFormat> for CompactDecimalPatternDataV1<'static> {
type Error = Cow<'static, str>;
fn try_from(other: &DecimalFormat) -> Result<Self, Self::Error> {
let mut parsed_patterns: BTreeMap<i8, BTreeMap<Count, Option<ParsedPattern>>> =
BTreeMap::new();
// First ingest the CLDR mapping.
for pattern in other.patterns.iter() {
let mut type_bytes = pattern.magnitude.bytes();
if !(type_bytes.next() == Some(b'1') && type_bytes.all(|b| b == b'0')) {
return Err(format!("Ill-formed type {}", pattern.magnitude).into());
}
let log10_type = i8::try_from(pattern.magnitude.len() - 1)
.map_err(|_| format!("Too many digits in type {}", pattern.magnitude))?;
let count = match &*pattern.count {
"zero" => Count::Zero,
"one" => Count::One,
"two" => Count::Two,
"few" => Count::Few,
"many" => Count::Many,
"other" => Count::Other,
"1" => Count::Explicit1,
_ => {
return Err(format!(
"Invalid count {} in type {}",
pattern.count, pattern.magnitude
)
.into())
}
};
let plural_map = parsed_patterns.entry(log10_type).or_default();
plural_map
.insert(count, parse(&pattern.pattern)?)
.map_or_else(
// TODO(egg): This should be try_insert.
|| Ok(()),
|_| {
Err(format!(
"Plural case {count:?} is duplicated for type 10^{log10_type}"
))
},
)?;
}
// Figure out which plural cases are used, and make the map dense by
// filling out the implicit fallbacks to the 0 (noncompact) pattern.
let plural_cases: BTreeSet<Count> = parsed_patterns
.iter()
.flat_map(|(_, plural_map)| plural_map.keys())
.copied()
.filter(|count| count != &Count::Explicit1)
.collect();
for log10_type in 0..=parsed_patterns.iter().last().map_or(0, |(key, _)| *key) {
for plural_case in &plural_cases {
parsed_patterns
.entry(log10_type)
.or_default()
.entry(*plural_case)
.or_insert(None);
}
}
let mut patterns: BTreeMap<i8, BTreeMap<Count, Pattern>> = BTreeMap::new();
// Compute the exponents based on the numbers of 0s in the placeholders
// and the type values: the exponent is 3 for type=1000, "0K", as well
// as for type=10000, "00K", etc.
// Remove duplicates of the count=other case in the same iteration.
for (log10_type, parsed_plural_map) in parsed_patterns {
let plural_map = patterns.entry(log10_type).or_default();
let other_pattern = parsed_plural_map
.get(&Count::Other)
.ok_or_else(|| format!("Missing other case for type 10^{log10_type}"))?
.clone();
let exponent: i8;
match &other_pattern {
None => {
if !parsed_plural_map.iter().all(|(_, p)| p.is_none()) {
return Err(format!(
"Non-0 pattern for type 10^{log10_type} whose pattern for count=other is 0"
)
.into());
}
exponent = 0;
}
Some(other_pattern) => {
let other_placeholder =
other_pattern.placeholder.as_ref().ok_or_else(|| {
format!("Missing placeholder in other case of type 10^{log10_type}")
})?;
for (count, pattern) in &parsed_plural_map {
if let Some(pattern) = pattern {
if let Some(placeholder) = &pattern.placeholder {
if placeholder.number_of_0s != other_placeholder.number_of_0s {
return Err(
format!(
"Inconsistent placeholders within type 10^{}: {} 0s for other, {} 0s for {:?}",
log10_type,
other_placeholder.number_of_0s,
placeholder.number_of_0s,
count
)
.into()
);
}
}
}
}
exponent = log10_type - other_placeholder.number_of_0s + 1;
if exponent < 1 {
return Err(format!(
"Too many 0s in type 10^{}, ({}, implying nonpositive exponent c={})",
log10_type, other_placeholder.number_of_0s, exponent
)
.into());
}
}
}
for (count, optional_pattern) in parsed_plural_map {
// Omit duplicates of the other case.
if count != Count::Other && optional_pattern == other_pattern {
continue;
}
plural_map.insert(
count,
match optional_pattern {
None => Pattern {
exponent: 0,
literal_text: std::borrow::Cow::Borrowed(""),
index: 0,
},
Some(pattern) => Pattern {
exponent,
literal_text: pattern.literal_text,
index: pattern
.placeholder
.map_or(Some(u8::MAX), |p| {
u8::try_from(p.index)
.ok()
.and_then(|i| (i < u8::MAX).then_some(i))
})
.ok_or_else(|| {
format!(
"Placeholder index is too large in type=10^{log10_type}, count={count:?}"
)
})?,
},
},
);
}
}
if !patterns
.iter()
.tuple_windows()
.all(|((_, low), (_, high))| {
low.get(&Count::Other).map(|p| p.exponent)
<= high.get(&Count::Other).map(|p| p.exponent)
})
{
Err(format!(
"Compact decimal exponents should be nondecreasing: {:?}",
patterns
.values()
.map(|plural_map| plural_map.get(&Count::Other).map(|p| p.exponent))
.collect::<Vec<_>>(),
))?;
}
// Deduplicate sequences of types that have the same plural map (up to =1), keeping the lowest type.
// The pattern 0 for type 1 is implicit.
let deduplicated_patterns = patterns
.iter()
.coalesce(
|(log10_low_type, low_plural_map), (log10_high_type, high_plural_map)| {
if low_plural_map == high_plural_map
|| (low_plural_map.contains_key(&Count::Explicit1)
&& low_plural_map
.iter()
.filter(|(count, _)| **count != Count::Explicit1)
.all(|(k, v)| high_plural_map.get(k) == Some(v))
&& high_plural_map
.iter()
.all(|(k, v)| low_plural_map.get(k) == Some(v)))
{
Ok((log10_low_type, low_plural_map))
} else {
Err((
(log10_low_type, low_plural_map),
(log10_high_type, high_plural_map),
))
}
},
)
.filter(|(log10_type, plural_map)| {
**log10_type != 0 || !plural_map.iter().all(|(_, pattern)| pattern.exponent == 0)
});
// Turn the BTreeMap of BTreeMaps into a ZeroMap2d.
Ok(CompactDecimalPatternDataV1 {
patterns: deduplicated_patterns
.flat_map(|(log10_type, plural_map)| {
plural_map.iter().map(|(count, pattern)| {
(*log10_type, *count, encode_varule_to_box(pattern))
})
})
.collect(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use icu_provider::prelude::*;
use zerofrom::ZeroFrom;
use zerovec::ule::AsULE;
#[test]
fn test_french_compressibility() {
// French compact-long thousands as of CLDR 42.
// The type=1000, count=one case is incorrect (it is inconsistent with the
// plural rules), but it is interesting because it forces a distinction
// between 1000 and 10000 to be made in the ICU4X data.
let cldr_42_long_french_data = CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"
{
"1000-count-1": "mille",
"1000-count-one": "0 millier",
"1000-count-other": "0 mille",
"10000-count-one": "00 mille",
"10000-count-other": "00 mille",
"100000-count-one": "000 mille",
"100000-count-other": "000 mille"
}
"#,
)
.unwrap(),
)
.unwrap();
let cldr_42_long_french: Box<[(i8, Count, Pattern)]> = cldr_42_long_french_data
.patterns
.iter0()
.flat_map(|kkv| {
let key0 = *kkv.key0();
kkv.into_iter1()
.map(move |(k, v)| (key0, Count::from_unaligned(*k), Pattern::zero_from(v)))
})
.collect();
assert_eq!(
cldr_42_long_french.as_ref(),
[
(
3,
Count::One,
Pattern {
index: 0,
exponent: 3,
literal_text: Cow::Borrowed(" millier")
}
),
(
3,
Count::Other,
Pattern {
index: 0,
exponent: 3,
literal_text: Cow::Borrowed(" mille")
}
),
(
3,
Count::Explicit1,
Pattern {
index: 255,
exponent: 3,
literal_text: Cow::Borrowed("mille")
}
),
(
4,
Count::Other,
Pattern {
index: 0,
exponent: 3,
literal_text: Cow::Borrowed(" mille")
}
),
]
);
// French compact-long thousands, with the anomalous « millier » removed.
// This allows 10000 and 1000 to be collapsed.
let compressible_long_french_data = CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"
{
"1000-count-1": "mille",
"1000-count-one": "0 mille",
"1000-count-other": "0 mille",
"10000-count-one": "00 mille",
"10000-count-other": "00 mille",
"100000-count-one": "000 mille",
"100000-count-other": "000 mille"
}
"#,
)
.unwrap(),
)
.unwrap();
let compressible_long_french: Box<[(i8, Count, Pattern)]> = compressible_long_french_data
.patterns
.iter0()
.flat_map(|kkv| {
let key0 = *kkv.key0();
kkv.into_iter1()
.map(move |(k, v)| (key0, Count::from_unaligned(*k), Pattern::zero_from(v)))
})
.collect();
assert_eq!(
compressible_long_french.as_ref(),
[
(
3,
Count::Other,
Pattern {
index: 0,
exponent: 3,
literal_text: Cow::Borrowed(" mille")
}
),
(
3,
Count::Explicit1,
Pattern {
index: 255,
exponent: 3,
literal_text: Cow::Borrowed("mille")
}
),
]
);
}
#[test]
fn test_holes() {
// Spanish compact-short data as of CLDR 42, up to 10¹¹.
// Note that the abbreviation for 10⁹ is used only starting with 10¹⁰.
let spanish_data = CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"
{
"1000-count-one": "0 mil",
"1000-count-other": "0 mil",
"10000-count-one": "00 mil",
"10000-count-other": "00 mil",
"100000-count-one": "000 mil",
"100000-count-other": "000 mil",
"1000000-count-one": "0 M",
"1000000-count-other": "0 M",
"10000000-count-one": "00 M",
"10000000-count-other": "00 M",
"100000000-count-one": "000 M",
"100000000-count-other": "000 M",
"1000000000-count-one": "0000 M",
"1000000000-count-other": "0000 M",
"10000000000-count-one": "00 mil M",
"10000000000-count-other": "00 mil M",
"100000000000-count-one": "000 mil M",
"100000000000-count-other": "000 mil M"
}
"#,
)
.unwrap(),
)
.unwrap();
let spanish: Box<[(i8, Count, Pattern)]> = spanish_data
.patterns
.iter0()
.flat_map(|kkv| {
let key0 = *kkv.key0();
kkv.into_iter1()
.map(move |(k, v)| (key0, Count::from_unaligned(*k), Pattern::zero_from(v)))
})
.collect();
assert_eq!(
spanish.as_ref(),
[
(
3,
Count::Other,
Pattern {
index: 0,
exponent: 3,
literal_text: Cow::Borrowed(" mil")
}
),
(
6,
Count::Other,
Pattern {
index: 0,
exponent: 6,
literal_text: Cow::Borrowed(" M")
}
),
(
10,
Count::Other,
Pattern {
index: 0,
exponent: 9,
literal_text: Cow::Borrowed(" mil M")
}
),
]
);
}
#[test]
fn test_pattern_syntax_errors() {
assert_eq!(
parse("M.").err().unwrap(),
"Unsupported symbol in compact decimal pattern M."
);
assert_eq!(parse("M'.'").unwrap().unwrap().literal_text, "M.");
assert_eq!(
parse("0 0").err().unwrap(),
"Multiple placeholders in compact decimal pattern 0 0"
);
assert_eq!(parse("0 '0'").unwrap().unwrap().literal_text, " 0");
let zeros = str::repeat("0", 256);
assert_eq!(
parse(&zeros[..128]).err().unwrap(),
String::from("Too many 0s in pattern ") + &zeros[..128]
);
assert_eq!(parse(&zeros[..127]).unwrap().unwrap().literal_text, "");
}
#[test]
fn test_inter_pattern_errors() {
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"{ "1000-count-other": "0k", "1000-count-other": "0K" }"#,
)
.unwrap(),
)
.err()
.unwrap(),
"Plural case Other is duplicated for type 10^3"
);
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(r#"{ "1-count-one": "0" }"#).unwrap()
)
.err()
.unwrap(),
"Missing other case for type 10^0"
);
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"{ "1000-count-one": "0k", "1000-count-other": "0" }"#
)
.unwrap()
)
.err()
.unwrap(),
"Non-0 pattern for type 10^3 whose pattern for count=other is 0"
);
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(r#"{ "1000-count-other": "k" }"#).unwrap()
)
.err()
.unwrap(),
"Missing placeholder in other case of type 10^3"
);
// Given this data, it is ambiguous whether the 10 000 should be formatted as 10 thousand or 1 myriad.
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
r#"
{
"10000-count-other": "00 thousand",
"10000-count-one": "0 myriad"
}
"#,
)
.unwrap(),
)
.err()
.unwrap(),
"Inconsistent placeholders within type 10^4: 2 0s for other, 1 0s for One"
);
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(r#"{ "1000-count-other": "00000 tenths" }"#)
.unwrap()
)
.err()
.unwrap(),
"Too many 0s in type 10^3, (5, implying nonpositive exponent c=-1)"
);
let long_pattern = format!("thous{}nds (0)", str::repeat("a", 244));
let overlong_pattern = format!("thous{}nds (0)", str::repeat("a", 245));
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
format!(r#"{{ "1000-count-other": "{overlong_pattern}" }}"#).as_str()
)
.unwrap()
)
.err()
.unwrap(),
"Placeholder index is too large in type=10^3, count=Other"
);
assert_eq!(
CompactDecimalPatternDataV1::try_from(
&serde_json::from_str::<DecimalFormat>(
format!(r#"{{ "1000-count-other": "{long_pattern}" }}"#).as_str()
)
.unwrap()
)
.unwrap()
.patterns
.get0(&3)
.and_then(|plural_map| plural_map.get1(&Count::Other))
.unwrap()
.index,
254
);
}
}