icu_plurals 2.3.0

Unicode Plural Rules categorizer for numeric input
Documentation
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
// 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 fixed_decimal::{CompactDecimal, Decimal, UnsignedDecimal};

const LIMIT_18_DIGITS: u64 = 1_000_000_000_000_000_000;

/// A full plural operands representation of a number. See [CLDR Plural Rules](https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) for complete operands description.
///
/// Plural operands in compliance with [CLDR Plural Rules](https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules).
///
/// See [full operands description](https://unicode.org/reports/tr35/tr35-numbers.html#Operands).
///
/// # Data Types
///
/// The following types can be converted to [`PluralOperands`]:
///
/// - Integers, signed and unsigned
/// - Strings representing an arbitrary-precision decimal
/// - [`Decimal`]
///
/// This crate does not support selection from a floating-point number, because floats are not
/// capable of carrying trailing zeros, which are required for proper plural rule selection. For
/// example, in English, "1 star" has a different plural form than "1.0 stars", but this
/// distinction cannot be represented using a float. Clients should use [`Decimal`] instead.
///
/// # Examples
///
/// From int
///
/// ```
/// use icu::plurals::PluralOperands;
/// use icu::plurals::RawPluralOperands;
///
/// assert_eq!(
///     PluralOperands::from(RawPluralOperands {
///         i: 2,
///         v: 0,
///         w: 0,
///         f: 0,
///         t: 0,
///         c: 0,
///     }),
///     PluralOperands::from(2_usize)
/// );
/// ```
///
/// From &str
///
/// ```
/// use icu::plurals::PluralOperands;
/// use icu::plurals::RawPluralOperands;
///
/// assert_eq!(
///     Ok(PluralOperands::from(RawPluralOperands {
///         i: 123,
///         v: 2,
///         w: 2,
///         f: 45,
///         t: 45,
///         c: 0,
///     })),
///     "123.45".parse()
/// );
/// ```
///
/// From [`Decimal`]
///
/// ```
/// use fixed_decimal::Decimal;
/// use icu::plurals::PluralOperands;
/// use icu::plurals::RawPluralOperands;
///
/// assert_eq!(
///     PluralOperands::from(RawPluralOperands {
///         i: 123,
///         v: 2,
///         w: 2,
///         f: 45,
///         t: 45,
///         c: 0,
///     }),
///     (&{
///         let mut decimal = Decimal::from(12345);
///         decimal.multiply_pow10(-2);
///         decimal
///     })
///         .into()
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[allow(clippy::exhaustive_structs)] // mostly stable, new operands may be added at the cadence of ICU's release cycle
pub struct PluralOperands {
    /// Integer value of input
    pub(crate) i: u64,
    /// Number of visible fraction digits with trailing zeros
    pub(crate) v: usize,
    /// Number of visible fraction digits without trailing zeros
    pub(crate) w: usize,
    /// Visible fraction digits with trailing zeros
    pub(crate) f: u64,
    /// Visible fraction digits without trailing zeros
    pub(crate) t: u64,
    /// Exponent of the power of 10 used in compact decimal formatting
    pub(crate) c: usize,
}

#[derive(displaydoc::Display, Debug, PartialEq, Eq)]
#[non_exhaustive]
#[cfg(feature = "datagen")]
pub enum OperandsError {
    /// Input to the Operands parsing was empty.
    #[displaydoc("Input to the Operands parsing was empty")]
    Empty,
    /// Input to the Operands parsing was invalid.
    #[displaydoc("Input to the Operands parsing was invalid")]
    Invalid,
}

#[cfg(feature = "datagen")]
impl core::error::Error for OperandsError {}

#[cfg(feature = "datagen")]
impl From<core::num::ParseIntError> for OperandsError {
    fn from(_: core::num::ParseIntError) -> Self {
        Self::Invalid
    }
}

#[cfg(feature = "datagen")]
impl core::str::FromStr for PluralOperands {
    type Err = OperandsError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        fn get_exponent(input: &str) -> Result<(&str, usize), OperandsError> {
            if let Some((base, exponent)) = input.split_once('e') {
                Ok((base, exponent.parse()?))
            } else {
                Ok((input, 0))
            }
        }

        if input.is_empty() {
            return Err(OperandsError::Empty);
        }

        let abs_str = input.strip_prefix('-').unwrap_or(input);

        let (
            integer_digits,
            num_fraction_digits0,
            num_fraction_digits,
            fraction_digits0,
            fraction_digits,
            exponent,
        ) = if let Some((int_str, rest)) = abs_str.split_once('.') {
            let (dec_str, exponent) = get_exponent(rest)?;

            let integer_digits = u64::from_str(int_str)?;

            let dec_str_no_zeros = dec_str.trim_end_matches('0');

            let num_fraction_digits0 = dec_str.len();
            let num_fraction_digits = dec_str_no_zeros.len();

            let fraction_digits0 = u64::from_str(dec_str)?;
            let fraction_digits =
                if num_fraction_digits == 0 || num_fraction_digits == num_fraction_digits0 {
                    fraction_digits0
                } else {
                    u64::from_str(dec_str_no_zeros)?
                };

            (
                integer_digits,
                num_fraction_digits0,
                num_fraction_digits,
                fraction_digits0,
                fraction_digits,
                exponent,
            )
        } else {
            let (abs_str, exponent) = get_exponent(abs_str)?;
            let integer_digits = u64::from_str(abs_str)?;
            (integer_digits, 0, 0, 0, 0, exponent)
        };

        Ok(Self {
            i: integer_digits,
            v: num_fraction_digits0,
            w: num_fraction_digits,
            f: fraction_digits0,
            t: fraction_digits,
            c: exponent,
        })
    }
}

macro_rules! impl_integer_type {
    ($ty:ident) => {
        impl From<$ty> for PluralOperands {
            #[inline]
            #[allow(trivial_numeric_casts)]
            fn from(input: $ty) -> Self {
                Self {
                    i: input as u64,
                    v: 0,
                    w: 0,
                    f: 0,
                    t: 0,
                    c: 0,
                }
            }
        }
    };
    ($($ty:ident)+) => {
        $(impl_integer_type!($ty);)+
    };
}

macro_rules! impl_signed_integer_type {
    ($ty:ident) => {
        impl From<$ty> for PluralOperands {
            #[inline]
            fn from(input: $ty) -> Self {
                input.unsigned_abs().into()
            }
        }
    };
    ($($ty:ident)+) => {
        $(impl_signed_integer_type!($ty);)+
    };
}

impl_integer_type!(u8 u16 u32 u64 usize);
impl_signed_integer_type!(i8 i16 i32 i64 i128 isize);

impl From<u128> for PluralOperands {
    #[inline]
    fn from(input: u128) -> Self {
        let i = if input < LIMIT_18_DIGITS as u128 {
            input as u64
        } else {
            // Add LIMIT_18_DIGITS so that is_exactly_one and is_exactly_zero
            // do not return true for large magnitude numbers like 1e18+1.
            (input % LIMIT_18_DIGITS as u128) as u64 + LIMIT_18_DIGITS
        };
        Self {
            i,
            v: 0,
            w: 0,
            f: 0,
            t: 0,
            c: 0,
        }
    }
}

impl PluralOperands {
    #[doc(hidden)] // todo: figure out whether to expose this
    /// Creates a [`PluralOperands`] from a significand and power of ten.
    pub fn from_significand_and_exponent(dec: &UnsignedDecimal, exp: u8) -> PluralOperands {
        let exp_i16 = i16::from(exp);

        let mag_range = dec.magnitude_range();
        let upper_mag = (*mag_range.end()).saturating_add(exp_i16);
        let lower_mag = (*mag_range.start()).saturating_add(exp_i16);
        let mag_high = upper_mag.clamp(0, 17);
        let mag_low = lower_mag.clamp(-18, 0);

        let mut i: u64 = 0;
        for magnitude in (0..=mag_high).rev() {
            i *= 10;
            i += dec.digit_at(magnitude - exp_i16) as u64;
        }

        if upper_mag > 17 {
            i += LIMIT_18_DIGITS;
        }

        let mut f: u64 = 0;
        let mut t: u64 = 0;
        let mut w: usize = 0;
        for magnitude in (mag_low..=-1).rev() {
            let digit = dec.digit_at(magnitude - exp_i16) as u64;
            f *= 10;
            f += digit;
            if digit != 0 {
                t = f;
                w = (-magnitude) as usize;
            }
        }

        Self {
            i,
            v: (-mag_low) as usize,
            w,
            f,
            t,
            c: usize::from(exp),
        }
    }

    /// Whether these [`PluralOperands`] are exactly the number 0, which might be a special case.
    pub(crate) fn is_exactly_zero(self) -> bool {
        self == Self {
            i: 0,
            v: 0,
            w: 0,
            f: 0,
            t: 0,
            c: 0,
        }
    }

    /// Whether these [`PluralOperands`] are exactly the number 1, which might be a special case.
    pub(crate) fn is_exactly_one(self) -> bool {
        self == Self {
            i: 1,
            v: 0,
            w: 0,
            f: 0,
            t: 0,
            c: 0,
        }
    }
}

impl From<&Decimal> for PluralOperands {
    /// Converts a [`fixed_decimal::Decimal`] to [`PluralOperands`]. Retains at most 18
    /// digits each from the integer and fraction parts.
    fn from(dec: &Decimal) -> Self {
        (&dec.absolute).into()
    }
}

impl From<&UnsignedDecimal> for PluralOperands {
    fn from(value: &UnsignedDecimal) -> Self {
        Self::from_significand_and_exponent(value, 0)
    }
}

impl From<&CompactDecimal> for PluralOperands {
    /// Converts a [`fixed_decimal::CompactDecimal`] to [`PluralOperands`]. Retains at most 18
    /// digits each from the integer and fraction parts.
    ///
    /// # Examples
    ///
    /// ```
    /// use fixed_decimal::CompactDecimal;
    /// use fixed_decimal::Decimal;
    /// use icu::locale::locale;
    /// use icu::plurals::PluralCategory;
    /// use icu::plurals::PluralOperands;
    /// use icu::plurals::PluralRules;
    /// use icu::plurals::RawPluralOperands;
    ///
    /// let fixed_decimal = "1000000.20".parse::<Decimal>().unwrap();
    /// let compact_decimal = "1.00000020c6".parse::<CompactDecimal>().unwrap();
    ///
    /// assert_eq!(
    ///     PluralOperands::from(RawPluralOperands {
    ///         i: 1000000,
    ///         v: 2,
    ///         w: 1,
    ///         f: 20,
    ///         t: 2,
    ///         c: 0,
    ///     }),
    ///     PluralOperands::from(&fixed_decimal)
    /// );
    ///
    /// assert_eq!(
    ///     PluralOperands::from(RawPluralOperands {
    ///         i: 1000000,
    ///         v: 2,
    ///         w: 1,
    ///         f: 20,
    ///         t: 2,
    ///         c: 6,
    ///     }),
    ///     PluralOperands::from(&compact_decimal)
    /// );
    ///
    /// let rules = PluralRules::try_new_cardinal(locale!("fr").into()).unwrap();
    /// assert_eq!(rules.category_for(&fixed_decimal), PluralCategory::Other);
    /// assert_eq!(rules.category_for(&compact_decimal), PluralCategory::Many);
    /// ```
    fn from(compact: &CompactDecimal) -> Self {
        Self::from_significand_and_exponent(&compact.significand().absolute, compact.exponent())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::str::FromStr;
    use fixed_decimal::Decimal;

    #[test]
    fn test_u128_overflow() {
        let limit_18 = LIMIT_18_DIGITS;

        // Small u128
        let val_small: u128 = 123;
        let ops = PluralOperands::from(val_small);
        assert_eq!(ops.i, 123);
        assert_eq!(ops.v, 0);

        // u128::MAX should end in 5.
        let val_max = u128::MAX;
        let ops = PluralOperands::from(val_max);

        assert!(ops.i >= limit_18, "i should be offset by LIMIT_18_DIGITS");
        assert_eq!(ops.i % 10, 5, "Mod 10 Check");
        assert_eq!(ops.i % 100, 55, "Mod 100 Check");

        // Just above limit
        let val_limit = limit_18 as u128;
        let ops = PluralOperands::from(val_limit);
        // limit % limit + limit = 0 + limit
        assert_eq!(ops.i, limit_18);

        let val_limit_plus_1 = val_limit + 1;
        let ops = PluralOperands::from(val_limit_plus_1);
        assert_eq!(ops.i, limit_18 + 1);
    }

    #[test]
    fn test_fixed_decimal_overflow() {
        let limit_18 = LIMIT_18_DIGITS;

        // Huge FixedDecimal
        let mut s = "1".to_string();
        for _ in 0..29 {
            s.push('0');
        }
        s.push('7');

        let dec = Decimal::from_str(&s).unwrap();
        let ops = PluralOperands::from(&dec);

        assert!(ops.i >= limit_18, "i should be offset");
        assert_eq!(ops.i % 10, 7, "Mod 10 check");
        assert_eq!(ops.i, limit_18 + 7);
    }

    #[test]
    fn test_i128_overflow() {
        let limit_18 = LIMIT_18_DIGITS;

        let val = i128::MIN;
        let ops = PluralOperands::from(val);

        assert!(ops.i >= limit_18);
        assert_eq!(ops.i % 10, 8);
    }

    #[test]
    fn test_compact_decimal_overflow() {
        use fixed_decimal::CompactDecimal;
        let limit_18 = LIMIT_18_DIGITS;

        let mut s = "9".repeat(i16::MAX as usize);
        s.push_str("c2");

        let cd = CompactDecimal::from_str(&s).unwrap();
        let ops = PluralOperands::from(&cd);

        assert!(ops.i >= limit_18, "i should be offset");
    }
}