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
// 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::operands::PluralOperands;
use crate::rules::reference::ast;
/// Function used to test [`Condition`] against [`PluralOperands`] to identify
/// the appropriate [`PluralCategory`].
///
/// # Examples
///
/// ```
/// use icu::plurals::rules::reference::parse_condition;
/// use icu::plurals::rules::reference::test_condition;
/// use icu::plurals::PluralOperands;
///
/// let operands = PluralOperands::from(5_usize);
/// let condition =
/// parse_condition(b"i = 4..6").expect("Failed to parse a rule.");
///
/// assert!(test_condition(&condition, &operands));
/// ```
///
/// [`PluralCategory`]: crate::PluralCategory
/// [`PluralOperands`]: crate::PluralOperands
/// [`Condition`]: super::ast::Condition
pub fn test_condition(condition: &ast::Condition, operands: &PluralOperands) -> bool {
condition.0.is_empty() || condition.0.iter().any(|c| test_and_condition(c, operands))
}
fn test_and_condition(condition: &ast::AndCondition, operands: &PluralOperands) -> bool {
condition.0.iter().all(|r| test_relation(r, operands))
}
fn test_relation(relation: &ast::Relation, operands: &PluralOperands) -> bool {
calculate_expression(&relation.expression, operands).map_or(false, |exp| {
test_range(&relation.range_list, exp, relation.operator)
})
}
// UTS 35 Part 2 Section 5.1 specifies that CLDR rules contain only integer values.
// See: https://unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax
//
// If a rule is asking for `n` it will compare it to a list of integers,
// and expect it to not match if there is any fractional part.
//
// That allows us to play a trick - we already have an integer portion of the number as operand `i`.
//
// In result, if we are asked to calculate an operand `n` and it contains a fractional part,
// we know that it will not match the value, which must be an integer without a fractional part.
//
// If that happens, we'll return `None`, and the matching will return `false`.
fn calculate_expression(expression: &ast::Expression, operands: &PluralOperands) -> Option<u64> {
let value = match expression.operand {
ast::Operand::N => {
if operands.w == 0 {
operands.i
} else {
return None;
}
}
ast::Operand::I => operands.i,
ast::Operand::F => operands.f,
ast::Operand::V => operands.v as u64,
ast::Operand::W => operands.w as u64,
ast::Operand::T => operands.t,
ast::Operand::C | ast::Operand::E => operands.c as u64,
};
if let Some(modulus) = &expression.modulus {
value.checked_rem_euclid(modulus.0)
} else {
Some(value)
}
}
fn test_range(range: &ast::RangeList, value: u64, operator: ast::Operator) -> bool {
let connect = match operator {
ast::Operator::Eq => Iterator::any,
ast::Operator::NotEq => Iterator::all,
};
connect(&mut range.0.iter(), |item| {
test_range_item(item, value, operator)
})
}
fn test_range_item(item: &ast::RangeListItem, value: u64, operator: ast::Operator) -> bool {
let value = match item {
ast::RangeListItem::Value(n) => n.0 == value,
ast::RangeListItem::Range(range) => range.contains(&ast::Value(value)),
};
match operator {
ast::Operator::Eq => value,
ast::Operator::NotEq => !value,
}
}