summaryrefslogtreecommitdiffstats
path: root/vendor/unicode-security/src/mixed_script.rs
blob: bffc10280091bb3ec6ef0c93d623bdf9712b6d56 (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
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
//! [Mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection)

use core::fmt::{self, Debug};
use unicode_script::{Script, ScriptExtension};

/// An Augmented script set, as defined by UTS 39
///
/// https://www.unicode.org/reports/tr39/#def-augmented-script-set
#[derive(Copy, Clone, PartialEq, Hash, Eq)]
pub struct AugmentedScriptSet {
    /// The base ScriptExtension value
    pub base: ScriptExtension,
    /// Han With Bopomofo
    pub hanb: bool,
    /// Japanese
    pub jpan: bool,
    /// Korean
    pub kore: bool,
}

impl From<ScriptExtension> for AugmentedScriptSet {
    fn from(ext: ScriptExtension) -> Self {
        let mut hanb = false;
        let mut jpan = false;
        let mut kore = false;

        if ext.is_common() || ext.is_inherited() || ext.contains_script(Script::Han) {
            hanb = true;
            jpan = true;
            kore = true;
        } else {
            if ext.contains_script(Script::Hiragana) || ext.contains_script(Script::Katakana) {
                jpan = true;
            }

            if ext.contains_script(Script::Hangul) {
                kore = true;
            }

            if ext.contains_script(Script::Bopomofo) {
                hanb = true;
            }
        }
        Self {
            base: ext,
            hanb,
            jpan,
            kore,
        }
    }
}

impl From<char> for AugmentedScriptSet {
    fn from(c: char) -> Self {
        AugmentedScriptSet::for_char(c)
    }
}

impl From<&'_ str> for AugmentedScriptSet {
    fn from(s: &'_ str) -> Self {
        AugmentedScriptSet::for_str(s)
    }
}

impl Default for AugmentedScriptSet {
    fn default() -> Self {
        AugmentedScriptSet {
            base: Script::Common.into(),
            hanb: true,
            jpan: true,
            kore: true,
        }
    }
}

impl Debug for AugmentedScriptSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            write!(f, "AugmentedScriptSet {{∅}}")?;
        } else if self.is_all() {
            write!(f, "AugmentedScriptSet {{ALL}}")?;
        } else {
            write!(f, "AugmentedScriptSet {{")?;
            let mut first_entry = true;
            let hanb = if self.hanb { Some("Hanb") } else { None };
            let jpan = if self.jpan { Some("Jpan") } else { None };
            let kore = if self.kore { Some("Kore") } else { None };
            for writing_system in None
                .into_iter()
                .chain(hanb)
                .chain(jpan)
                .chain(kore)
                .chain(self.base.iter().map(Script::short_name))
            {
                if !first_entry {
                    write!(f, ", ")?;
                } else {
                    first_entry = false;
                }
                write!(f, "{}", writing_system)?;
            }
            write!(f, "}}")?;
        }
        Ok(())
    }
}

impl fmt::Display for AugmentedScriptSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            write!(f, "Empty")?;
        } else if self.is_all() {
            write!(f, "All")?;
        } else {
            let mut first_entry = true;
            let hanb = if self.hanb {
                Some("Han with Bopomofo")
            } else {
                None
            };
            let jpan = if self.jpan { Some("Japanese") } else { None };
            let kore = if self.kore { Some("Korean") } else { None };
            for writing_system in None
                .into_iter()
                .chain(hanb)
                .chain(jpan)
                .chain(kore)
                .chain(self.base.iter().map(Script::full_name))
            {
                if !first_entry {
                    write!(f, ", ")?;
                } else {
                    first_entry = false;
                }
                write!(f, "{}", writing_system)?;
            }
        }
        Ok(())
    }
}

impl AugmentedScriptSet {
    /// Intersect this set with another
    pub fn intersect_with(&mut self, other: Self) {
        self.base.intersect_with(other.base);
        self.hanb = self.hanb && other.hanb;
        self.jpan = self.jpan && other.jpan;
        self.kore = self.kore && other.kore;
    }

    /// Check if the set is empty
    pub fn is_empty(&self) -> bool {
        self.base.is_empty() && !self.hanb && !self.jpan && !self.kore
    }

    /// Check if the set is "All" (Common or Inherited)
    pub fn is_all(&self) -> bool {
        self.base.is_common() || self.base.is_inherited()
    }

    /// Construct an AugmentedScriptSet for a given character
    pub fn for_char(c: char) -> Self {
        ScriptExtension::from(c).into()
    }

    /// Find the [resolved script set](https://www.unicode.org/reports/tr39/#def-resolved-script-set) of a given string
    pub fn for_str(s: &str) -> Self {
        let mut set = AugmentedScriptSet::default();
        for ch in s.chars() {
            set.intersect_with(ch.into())
        }
        set
    }
}

/// Extension trait for [mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection)
pub trait MixedScript {
    /// Check if a string is [single-script](https://www.unicode.org/reports/tr39/#def-single-script)
    ///
    /// Note that a single-script string may still contain multiple Script properties!
    fn is_single_script(self) -> bool;

    /// Find the [resolved script set](https://www.unicode.org/reports/tr39/#def-resolved-script-set) of a given string
    fn resolve_script_set(self) -> AugmentedScriptSet;
}

impl MixedScript for &'_ str {
    fn is_single_script(self) -> bool {
        !AugmentedScriptSet::for_str(self).is_empty()
    }

    fn resolve_script_set(self) -> AugmentedScriptSet {
        self.into()
    }
}

/// Check if a character is considered potential mixed script confusable.
///
/// If the specified character is not restricted from use for identifiers,
/// this function returns whether it is considered mixed script confusable
/// with another character that is not restricted from use for identifiers.
///
/// If the specified character is restricted from use for identifiers,
/// the return value is unspecified.
pub fn is_potential_mixed_script_confusable_char(c: char) -> bool {
    use crate::tables::potential_mixed_script_confusable::potential_mixed_script_confusable;

    potential_mixed_script_confusable(c)
}