summaryrefslogtreecommitdiffstats
path: root/vendor/unic-langid-impl/src/likelysubtags/mod.rs
blob: dad9dc9a679a86fe4083ae8a51258803b4d8bb23 (plain)
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
mod tables;

pub use tables::CLDR_VERSION;

use crate::subtags;

unsafe fn lang_from_parts(
    input: (Option<u64>, Option<u32>, Option<u32>),
    lang: Option<subtags::Language>,
    script: Option<subtags::Script>,
    region: Option<subtags::Region>,
) -> Option<(
    subtags::Language,
    Option<subtags::Script>,
    Option<subtags::Region>,
)> {
    let lang = lang
        .or_else(|| input.0.map(|s| subtags::Language::from_raw_unchecked(s)))
        .unwrap();
    let script = script.or_else(|| input.1.map(|s| subtags::Script::from_raw_unchecked(s)));
    let region = region.or_else(|| input.2.map(|r| subtags::Region::from_raw_unchecked(r)));
    Some((lang, script, region))
}

pub fn maximize(
    lang: subtags::Language,
    script: Option<subtags::Script>,
    region: Option<subtags::Region>,
) -> Option<(
    subtags::Language,
    Option<subtags::Script>,
    Option<subtags::Region>,
)> {
    if !lang.is_empty() && script.is_some() && region.is_some() {
        return None;
    }

    if let Some(l) = Into::<Option<u64>>::into(lang) {
        if let Some(r) = region {
            let result = tables::LANG_REGION
                .binary_search_by_key(&(&l.into(), &r.into()), |(key_l, key_r, _)| (key_l, key_r))
                .ok();
            if let Some(r) = result {
                // safe because all table entries are well formed.
                return unsafe { lang_from_parts(tables::LANG_REGION[r].2, None, None, None) };
            }
        }

        if let Some(s) = script {
            let result = tables::LANG_SCRIPT
                .binary_search_by_key(&(&l.into(), &s.into()), |(key_l, key_s, _)| (key_l, key_s))
                .ok();
            if let Some(r) = result {
                // safe because all table entries are well formed.
                return unsafe { lang_from_parts(tables::LANG_SCRIPT[r].2, None, None, None) };
            }
        }

        let result = tables::LANG_ONLY
            .binary_search_by_key(&(&l.into()), |(key_l, _)| key_l)
            .ok();
        if let Some(r) = result {
            // safe because all table entries are well formed.
            return unsafe { lang_from_parts(tables::LANG_ONLY[r].1, None, script, region) };
        }
    } else if let Some(s) = script {
        if let Some(r) = region {
            let result = tables::SCRIPT_REGION
                .binary_search_by_key(&(&s.into(), &r.into()), |(key_s, key_r, _)| (key_s, key_r))
                .ok();
            if let Some(r) = result {
                // safe because all table entries are well formed.
                return unsafe { lang_from_parts(tables::SCRIPT_REGION[r].2, None, None, None) };
            }
        }

        let result = tables::SCRIPT_ONLY
            .binary_search_by_key(&(&s.into()), |(key_s, _)| key_s)
            .ok();
        if let Some(r) = result {
            // safe because all table entries are well formed.
            return unsafe { lang_from_parts(tables::SCRIPT_ONLY[r].1, None, None, region) };
        }
    } else if let Some(r) = region {
        let result = tables::REGION_ONLY
            .binary_search_by_key(&(&r.into()), |(key_r, _)| key_r)
            .ok();
        if let Some(r) = result {
            // safe because all table entries are well formed.
            return unsafe { lang_from_parts(tables::REGION_ONLY[r].1, None, None, None) };
        }
    }

    None
}

pub fn minimize(
    lang: subtags::Language,
    script: Option<subtags::Script>,
    region: Option<subtags::Region>,
) -> Option<(
    subtags::Language,
    Option<subtags::Script>,
    Option<subtags::Region>,
)> {
    // maximize returns None when all 3 components are
    // already filled so don't call it in that case.
    let max_langid = if !lang.is_empty() && script.is_some() && region.is_some() {
        (lang, script, region)
    } else {
        maximize(lang, script, region)?
    };

    if let Some(trial) = maximize(max_langid.0, None, None) {
        if trial == max_langid {
            return Some((max_langid.0, None, None));
        }
    }

    if max_langid.2.is_some() {
        if let Some(trial) = maximize(max_langid.0, None, max_langid.2) {
            if trial == max_langid {
                return Some((max_langid.0, None, max_langid.2));
            }
        }
    }

    if max_langid.1.is_some() {
        if let Some(trial) = maximize(max_langid.0, max_langid.1, None) {
            if trial == max_langid {
                return Some((max_langid.0, max_langid.1, None));
            }
        }
    }
    None
}