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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use std::fmt;
use std::error;
#[cfg(test)]
mod test;
#[derive(Debug)]
pub enum HangeulError {
NotSyllable,
Uncomposable,
}
impl fmt::Display for HangeulError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
HangeulError::NotSyllable => write!(f, "HangeulError: Not correct Hangeul syllable"),
HangeulError::Uncomposable => write!(f, "HangeulError: Uncomposable")
}
}
}
impl error::Error for HangeulError {
fn description(&self) -> &str {
match *self {
HangeulError::NotSyllable => "HangeulError: Not correct Hangeul syllable",
HangeulError::Uncomposable => "HangeulError: Uncomposable"
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
_ => None
}
}
}
const JAMO_TO_CHOSEONG: [i32; 30] = [
0x00,
0x01,
-1,
0x02,
-1,
-1,
0x03,
0x04,
0x05,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
0x06,
0x07,
0x08,
-1,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x10,
0x11,
0x12,
];
const JAMO_TO_JONGSEONG: [i32; 30] = [
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
-1,
0x07,
0x08,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x10,
-1,
0x11,
0x12,
0x13,
0x14,
0x15,
-1,
0x16,
0x17,
0x18,
0x19,
0x1A,
];
const CHOSEONG_TO_JAMO: [u32; 19] = [
0x00,
0x01,
0x03,
0x06,
0x07,
0x08,
0x10,
0x11,
0x12,
0x14,
0x15,
0x16,
0x17,
0x18,
0x19,
0x1A,
0x1B,
0x1C,
0x1D,
];
const JONGSEONG_TO_JAMO: [u32; 27] = [
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x08,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x10,
0x11,
0x13,
0x14,
0x15,
0x16,
0x17,
0x19,
0x1A,
0x1B,
0x1C,
0x1D,
];
const SYLLABLE_START: u32 = 0xAC00;
const SYLLABLE_END: u32 = 0xD7AF;
const JAMO_START: u32 = 0x3131;
const JAMO_END: u32 = 0x3163;
const JAEUM_START: u32 = 0x3131;
const JAEUM_END: u32 = 0x314E;
const MOEUM_START: u32 = 0x314F;
const MOEUM_END: u32 = 0x3163;
fn _is_syllable(code:u32) -> bool {
(code >= SYLLABLE_START && code <= SYLLABLE_END)
}
pub fn is_syllable(c:char) -> bool {
let code = c as u32;
_is_syllable(code)
}
fn syllable_to_u32(c:char) -> Result<u32, HangeulError> {
let code = c as u32;
if _is_syllable(code) {
Ok(code)
} else {
Err(HangeulError::NotSyllable)
}
}
pub fn get_choseong(c:char) -> Result<char, HangeulError> {
let code = try!(syllable_to_u32(c));
let x = (code - SYLLABLE_START) / 21 / 28;
std::char::from_u32(CHOSEONG_TO_JAMO[x as usize] + JAMO_START).ok_or(HangeulError::NotSyllable)
}
pub use self::get_choseong as get_top;
pub fn get_jungseong(c:char) -> Result<char, HangeulError> {
let code = try!(syllable_to_u32(c));
std::char::from_u32(((code - SYLLABLE_START) % (21 * 28)) / 28 + MOEUM_START).ok_or(HangeulError::NotSyllable)
}
pub use self::get_jungseong as get_middle;
pub fn get_jongseong(c:char) -> Result<Option<char>, HangeulError> {
let code = try!(syllable_to_u32(c));
let x:i32 = (code - SYLLABLE_START) as i32 % 28 - 1;
if x >= 0 {
std::char::from_u32(JONGSEONG_TO_JAMO[x as usize] + JAMO_START)
.map_or(Err(HangeulError::NotSyllable), |c| Ok(Some(c)))
} else {
Ok(None)
}
}
pub use self::get_jongseong as get_bottom;
pub fn has_jongseong(c:char) -> Result<bool, HangeulError> {
let code = try!(syllable_to_u32(c));
Ok((code - SYLLABLE_START) % 28 != 0)
}
pub use self::has_jongseong as has_bottom;
pub fn ends_with_jongseong(s:&str) -> Result<bool, HangeulError> {
let c = match s.chars().last() {
Some(x) => x,
None => return Err(HangeulError::NotSyllable),
};
has_jongseong(c)
}
pub use self::ends_with_jongseong as ends_with_bottom;
pub fn is_jamo(c:char) -> bool {
let code = c as u32;
(code >= JAMO_START && code <= JAMO_END)
}
fn _is_jaeum(code:u32) -> bool {
(code >= JAEUM_START && code <= JAEUM_END)
}
pub fn is_jaeum(c:char) -> bool {
let code = c as u32;
_is_jaeum(code)
}
pub use self::is_jaeum as is_consonant;
fn _is_moeum(code:u32) -> bool {
(code >= MOEUM_START && code <= MOEUM_END)
}
pub fn is_moeum(c:char) -> bool {
let code = c as u32;
_is_moeum(code)
}
pub use self::is_moeum as is_vowel;
fn _is_choseong(code:u32) -> bool {
_is_jaeum(code) && (JAMO_TO_CHOSEONG[(code - JAEUM_START) as usize] != -1)
}
pub fn is_choseong(c:char) -> bool {
let code = c as u32;
_is_choseong(code)
}
pub use self::is_choseong as is_top;
fn _is_jongseong(code:u32) -> bool {
_is_jaeum(code) && (JAMO_TO_JONGSEONG[(code - JAEUM_START) as usize] != -1)
}
pub fn is_jongseong(c:char) -> bool {
let code = c as u32;
_is_jongseong(code)
}
pub use self::is_jongseong as is_bottom;
pub fn decompose(c:char) -> Result<(char, char, Option<char>), HangeulError> {
let cho = try!(get_choseong(c));
let jung = try!(get_jungseong(c));
let jong = try!(get_jongseong(c));
Ok((cho, jung, jong))
}
pub fn compose(choseong:char, jungseong:char, jongseong:Option<char>) -> Result<char, HangeulError> {
let cho_code = choseong as u32;
let jung_code = jungseong as u32;
let jong_code = match jongseong {
Some(c) => c as u32,
None => 0
};
if !_is_choseong(cho_code) || !_is_moeum(jung_code) || !(jong_code == 0 || _is_jongseong(jong_code)) {
return Err(HangeulError::Uncomposable);
}
let composed = match jong_code == 0 {
true => JAMO_TO_CHOSEONG[(cho_code - JAEUM_START) as usize] as u32 * 21 * 28
+ (jung_code - MOEUM_START) * 28
+ SYLLABLE_START,
false => JAMO_TO_CHOSEONG[(cho_code - JAEUM_START) as usize] as u32 * 21 * 28
+ (jung_code - MOEUM_START) * 28
+ JAMO_TO_JONGSEONG[(jong_code - JAEUM_START) as usize] as u32 + 1
+ SYLLABLE_START
};
std::char::from_u32(composed).ok_or(HangeulError::Uncomposable)
}