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
use super::unicode_data::{
    compose_index, decompose_compat_index, decompose_index, COMPOSE0, COMPOSE1, COMPOSE1_COUNT,
    DECOMPOSE, DECOMPOSE_COMPAT,
};
use core::char::from_u32_unchecked;

/// Decomposition of a character.
#[derive(Copy, Clone)]
pub struct Decompose {
    inner: DecomposeInner,
    len: u8,
    cur: u8,
}

impl Decompose {
    /// Returns the sequence of characters that represent the
    /// decomposition.
    pub fn chars(&self) -> &[char] {
        match self.inner {
            DecomposeInner::Slice(chars) => chars,
            DecomposeInner::Array(ref chars, len) => &chars[..len as usize],
        }
    }
}

impl Iterator for Decompose {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cur >= self.len {
            return None;
        }
        let item = self.chars()[self.cur as usize];
        self.cur += 1;
        Some(item)
    }
}

#[derive(Copy, Clone)]
enum DecomposeInner {
    Slice(&'static [char]),
    Array([char; 3], u32),
}

impl DecomposeInner {
    fn len(&self) -> u8 {
        match self {
            Self::Slice(s) => s.len() as u8,
            Self::Array(_, len) => *len as u8,
        }
    }
}

impl From<DecomposeInner> for Decompose {
    fn from(inner: DecomposeInner) -> Self {
        Self {
            inner,
            len: inner.len(),
            cur: 0,
        }
    }
}

pub fn compose_pair(a: char, b: char) -> Option<char> {
    if let Some(c) = compose_hangul(a, b) {
        return Some(c);
    }
    let l = pair_index(a as u32, &COMPOSE0[..])?;
    let r = pair_index(b as u32, &COMPOSE1[..])?;
    let c = compose_index(l * COMPOSE1_COUNT + r) as u32;
    if c != 0 {
        return Some(unsafe { core::char::from_u32_unchecked(c) });
    }
    None
}

fn pair_index(c: u32, table: &[(u32, u16, u16)]) -> Option<usize> {
    let c = c as usize;
    for entry in table {
        let start = entry.0 as usize;
        if start == 0 || c < start {
            return None;
        }
        let end = start + entry.1 as usize;
        if c <= end {
            return Some(entry.2 as usize + (c - start));
        }
    }
    None
}

const LBASE: u32 = 0x1100;
const VBASE: u32 = 0x1161;
const TBASE: u32 = 0x11A7;
const LCOUNT: u32 = 19;
const VCOUNT: u32 = 21;
const TCOUNT: u32 = 28;
const SBASE: u32 = 0xAC00;
const NCOUNT: u32 = VCOUNT * TCOUNT;
const SCOUNT: u32 = LCOUNT * NCOUNT;

fn is_hangul(c: char) -> bool {
    let c = c as u32;
    (SBASE..(SBASE + SCOUNT)).contains(&c)
}

fn compose_hangul(a: char, b: char) -> Option<char> {
    let a = a as u32;
    let b = b as u32;
    if !(VBASE..(TBASE + TCOUNT)).contains(&b) {
        return None;
    }
    if !(LBASE..(LBASE + LCOUNT)).contains(&a) && !(SBASE..(SBASE + SCOUNT)).contains(&a) {
        return None;
    }
    if a >= SBASE {
        if (a - SBASE) % TCOUNT == 0 {
            Some(unsafe { from_u32_unchecked(a + (b - TBASE)) })
        } else {
            None
        }
    } else {
        let li = a - LBASE;
        let vi = b - VBASE;
        Some(unsafe { from_u32_unchecked(SBASE + li * NCOUNT + vi * TCOUNT) })
    }
}

fn decompose_hangul(c: char) -> DecomposeInner {
    let si = c as u32 - SBASE;
    let li = si / NCOUNT;
    let mut chars = [' '; 3];
    let mut len = 2;
    unsafe {
        chars[0] = from_u32_unchecked(LBASE + li);
        let vi = (si % NCOUNT) / TCOUNT;
        chars[1] = from_u32_unchecked(VBASE + vi);
        let ti = si % TCOUNT;
        if ti > 0 {
            chars[2] = from_u32_unchecked(TBASE + ti);
            len += 1;
        }
    }
    DecomposeInner::Array(chars, len)
}

pub fn decompose(c: char) -> Decompose {
    if c <= '\x7F' {
        DecomposeInner::Array([c, ' ', ' '], 1).into()
    } else if is_hangul(c) {
        decompose_hangul(c).into()
    } else {
        let index = decompose_index(c as usize);
        if index == 0 {
            DecomposeInner::Array([c, ' ', ' '], 1).into()
        } else {
            let buf = &DECOMPOSE[index..];
            let end = 1 + buf[0] as usize;
            DecomposeInner::Slice(unsafe { &*(&buf[1..end] as *const [u32] as *const [char]) })
                .into()
        }
    }
}

pub fn decompose_compat(c: char) -> Decompose {
    if c <= '\x7F' {
        DecomposeInner::Array([c, ' ', ' '], 1).into()
    } else if is_hangul(c) {
        decompose_hangul(c).into()
    } else {
        let index = decompose_compat_index(c as usize);
        if index == 0 {
            DecomposeInner::Array([c, ' ', ' '], 1).into()
        } else if index == 1 {
            let index = decompose_index(c as usize);
            let buf = &DECOMPOSE[index..];
            let end = 1 + buf[0] as usize;
            DecomposeInner::Slice(unsafe { &*(&buf[1..end] as *const [u32] as *const [char]) })
                .into()
        } else {
            let buf = &DECOMPOSE_COMPAT[index..];
            let end = 1 + buf[0] as usize;
            DecomposeInner::Slice(unsafe { &*(&buf[1..end] as *const [u32] as *const [char]) })
                .into()
        }
    }
}