icu_experimental/measure/
power.rs

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
5use zerotrie::ZeroTrieSimpleAscii;
6
7/// A trie that contains the powers.
8const POWERS_TRIE: ZeroTrieSimpleAscii<[u8; 64]> = ZeroTrieSimpleAscii::from_sorted_str_tuples(&[
9    ("cubic", 3),
10    ("pow1", 1),
11    ("pow10", 10),
12    ("pow11", 11),
13    ("pow12", 12),
14    ("pow13", 13),
15    ("pow14", 14),
16    ("pow15", 15),
17    ("pow2", 2),
18    ("pow3", 3),
19    ("pow4", 4),
20    ("pow5", 5),
21    ("pow6", 6),
22    ("pow7", 7),
23    ("pow8", 8),
24    ("pow9", 9),
25    ("square", 2),
26]);
27
28// TODO: consider returning Option<(u8, &str)> instead of (1, part) for the case when the power is not found.
29// TODO: complete all the cases for the powers.
30// TODO: consider using a trie for the powers.
31/// Extracts the power from the given CLDR ID part.
32///     - If the power is not found, the function returns (1, part).
33///     - If the power is found, the function will return (power, part without the string of the power).
34pub fn get_power(part: &[u8]) -> (u8, &[u8]) {
35    let mut cursor = POWERS_TRIE.cursor();
36    let mut longest_match = (1, part);
37    for (i, &b) in part.iter().enumerate() {
38        cursor.step(b);
39        if cursor.is_empty() {
40            break;
41        }
42        if let Some(value) = cursor.take_value() {
43            longest_match = (value as u8, &part[i + 1..]);
44        }
45    }
46    longest_match
47}