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
use unicode_script::{Script, ScriptExtension};
#[derive(Copy, Clone, PartialEq, Debug, Hash)]
pub struct AugmentedScriptSet {
pub base: ScriptExtension,
pub hanb: bool,
pub jpan: bool,
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 == ScriptExtension::Single(Script::Common)
|| ext == ScriptExtension::Single(Script::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: ScriptExtension::Single(Script::Common),
hanb: true,
jpan: true,
kore: true,
}
}
}
impl AugmentedScriptSet {
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;
}
pub fn is_empty(&self) -> bool {
self.base.is_empty() && !self.hanb && !self.jpan && !self.kore
}
pub fn is_all(&self) -> bool {
self.base == ScriptExtension::Single(Script::Common)
|| self.base == ScriptExtension::Single(Script::Inherited)
}
pub fn for_char(c: char) -> Self {
ScriptExtension::from(c).into()
}
pub fn for_str(s: &str) -> Self {
let mut set = AugmentedScriptSet::default();
for ch in s.chars() {
set.intersect_with(ch.into())
}
set
}
}
pub trait MixedScript {
fn is_single_script(self) -> bool;
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()
}
}