forked from KimHyeonwoo/go-hangul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamo.go
421 lines (390 loc) · 13.1 KB
/
jamo.go
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package hangul
// Choseong is an initial consonant in Hangul.
// Unicode `Hangul Jamo` range for Choseong: 0x1100 ~ 0x1112
// Unicode `Hangul Compatibility Jamo` range for Choseong: 0x3131 ~ 0x314E
// ref: https://en.wikipedia.org/wiki/Korean_language_and_computers#Hangul_in_Unicode
type Choseong int32
const (
ChoseongKiyeok = Choseong(iota) // ㄱ
ChoseongSsangKiyeok // ㄲ
ChoseongNieun // ㄴ
ChoseongTikeut // ㄷ
ChoseongSsangTikeut // ㄸ
ChoseongRieul // ㄹ
ChoseongMieum // ㅁ
ChoseongPieup // ㅂ
ChoseongSsangPieup // ㅃ
ChoseongSios // ㅅ
ChoseongSsangSios // ㅆ
ChoseongIeung // ㅇ
ChoseongCieuc // ㅈ
ChoseongSsangCieuc // ㅉ
ChoseongChieuch // ㅊ
ChoseongKhieukh // ㅋ
ChoseongThieuth // ㅌ
ChoseongPhieuph // ㅍ
ChoseongHieuh // ㅎ
ChoseongBase = 0x1100
ChoseongCount = 19
)
// CompatibilityRune returns the rune for Unicode `Hangul Compatibility Jamo`.
func (c Choseong) CompatibilityRune() rune {
switch c {
case ChoseongKiyeok:
return 'ㄱ'
case ChoseongSsangKiyeok:
return 'ㄲ'
case ChoseongNieun:
return 'ㄴ'
case ChoseongTikeut:
return 'ㄷ'
case ChoseongSsangTikeut:
return 'ㄸ'
case ChoseongRieul:
return 'ㄹ'
case ChoseongMieum:
return 'ㅁ'
case ChoseongPieup:
return 'ㅂ'
case ChoseongSsangPieup:
return 'ㅃ'
case ChoseongSios:
return 'ㅅ'
case ChoseongSsangSios:
return 'ㅆ'
case ChoseongIeung:
return 'ㅇ'
case ChoseongCieuc:
return 'ㅈ'
case ChoseongSsangCieuc:
return 'ㅉ'
case ChoseongChieuch:
return 'ㅊ'
case ChoseongKhieukh:
return 'ㅋ'
case ChoseongThieuth:
return 'ㅌ'
case ChoseongPhieuph:
return 'ㅍ'
case ChoseongHieuh:
return 'ㅎ'
}
return 0
}
// Rune returns the rune for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the rune for `Hangul Compatibility Jamo`.
// Otherwise, it returns the rune for `Hangul Jamo`.
func (c Choseong) Rune(compatibility bool) rune {
if compatibility {
return c.CompatibilityRune()
}
return rune(c + ChoseongBase)
}
// Decompose returns the slice of runes for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the slice of runes for `Hangul Compatibility Jamo`.
// Otherwise, it returns the slice of runes for `Hangul Jamo`.
// If `level` is FullDecompose, it decomposes a double consonants into two to three runes.
func (c Choseong) Decompose(compatibility bool, level int) []rune {
if level < DefaultDecompose || level > FullDecompose {
return nil
}
if level == DefaultDecompose {
return []rune{c.Rune(compatibility)}
}
// FullDecompose
switch c {
case ChoseongSsangKiyeok:
return []rune{ChoseongKiyeok.Rune(compatibility), ChoseongKiyeok.Rune(compatibility)}
case ChoseongSsangTikeut:
return []rune{ChoseongTikeut.Rune(compatibility), ChoseongTikeut.Rune(compatibility)}
case ChoseongSsangPieup:
return []rune{ChoseongPieup.Rune(compatibility), ChoseongPieup.Rune(compatibility)}
case ChoseongSsangSios:
return []rune{ChoseongSios.Rune(compatibility), ChoseongSios.Rune(compatibility)}
case ChoseongSsangCieuc:
return []rune{ChoseongCieuc.Rune(compatibility), ChoseongCieuc.Rune(compatibility)}
}
// Nothing to decompose
return []rune{c.Rune(compatibility)}
}
// Jungseong is a medial vowel in Hangul.
// Unicode `Hangul Jamo` range for Jungseong: 0x1161 ~ 0x1175
// Unicode `Hangul Compatibility Jamo` range for Jungseong: 0x314F ~ 0x3163
// ref: https://en.wikipedia.org/wiki/Korean_language_and_computers#Hangul_in_Unicode
type Jungseong int32
const (
JungseongA = Jungseong(iota) // ㅏ
JungseongAe // ㅐ
JungseongYa // ㅑ
JungseongYae // ㅒ
JungseongEo // ㅓ
JungseongE // ㅔ
JungseongYeo // ㅕ
JungseongYe // ㅖ
JungseongO // ㅗ
JungseongWa // ㅘ
JungseongWae // ㅙ
JungseongOe // ㅚ
JungseongYo // ㅛ
JungseongU // ㅜ
JungseongWeo // ㅝ
JungseongWe // ㅞ
JungseongWi // ㅟ
JungseongYu // ㅠ
JungseongEu // ㅡ
JungseongYi // ㅢ
JungseongI // ㅣ
JungseongBase = 0x1161
JungseongCount = 21
)
// CompatibilityRune returns the rune for Unicode `Hangul Compatibility Jamo`.
func (j Jungseong) CompatibilityRune() rune {
switch j {
case JungseongA:
return 'ㅏ'
case JungseongAe:
return 'ㅐ'
case JungseongYa:
return 'ㅑ'
case JungseongYae:
return 'ㅒ'
case JungseongEo:
return 'ㅓ'
case JungseongE:
return 'ㅔ'
case JungseongYeo:
return 'ㅕ'
case JungseongYe:
return 'ㅖ'
case JungseongO:
return 'ㅗ'
case JungseongWa:
return 'ㅘ'
case JungseongWae:
return 'ㅙ'
case JungseongOe:
return 'ㅚ'
case JungseongYo:
return 'ㅛ'
case JungseongU:
return 'ㅜ'
case JungseongWeo:
return 'ㅝ'
case JungseongWe:
return 'ㅞ'
case JungseongWi:
return 'ㅟ'
case JungseongYu:
return 'ㅠ'
case JungseongEu:
return 'ㅡ'
case JungseongYi:
return 'ㅢ'
case JungseongI:
return 'ㅣ'
}
return 0
}
// Rune returns the rune for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the rune for `Hangul Compatibility Jamo`.
// Otherwise, it returns the rune for `Hangul Jamo`.
func (j Jungseong) Rune(compatibility bool) rune {
if compatibility {
return j.CompatibilityRune()
}
return rune(j + JungseongBase)
}
// Decompose returns the slice of runes for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the slice of runes for `Hangul Compatibility Jamo`.
// Otherwise, it returns the slice of runes for `Hangul Jamo`.
// If `level` is FullDecompose, it decomposes a double vowels into two to three runes.
func (j Jungseong) Decompose(compatibility bool, level int) []rune {
if level < DefaultDecompose || level > FullDecompose {
return nil
}
if level == DefaultDecompose {
return []rune{j.Rune(compatibility)}
}
// FullDecompose
switch j {
case JungseongAe:
return []rune{JungseongA.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongYae:
return []rune{JungseongYa.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongE:
return []rune{JungseongEo.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongYe:
return []rune{JungseongYeo.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongWa:
return []rune{JungseongO.Rune(compatibility), JungseongA.Rune(compatibility)}
case JungseongWae:
return []rune{JungseongO.Rune(compatibility), JungseongA.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongOe:
return []rune{JungseongO.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongWeo:
return []rune{JungseongU.Rune(compatibility), JungseongEo.Rune(compatibility)}
case JungseongWe:
return []rune{JungseongU.Rune(compatibility), JungseongEo.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongWi:
return []rune{JungseongU.Rune(compatibility), JungseongI.Rune(compatibility)}
case JungseongYi:
return []rune{JungseongEu.Rune(compatibility), JungseongI.Rune(compatibility)}
}
// Nothing to decompose
return []rune{j.Rune(compatibility)}
}
// Jongseong is a final consonant in Hangul.
// Unicode `Hangul Jamo` range for Jongseong: U+11A8 ~ U+11C2
// Unicode `Hangul Compatibility Jamo` range for Jongseong: U+3131 ~ U+314E
// ref: https://en.wikipedia.org/wiki/Korean_language_and_computers#Hangul_in_Unicode
type Jongseong int32 // 0x11A8 ~ 0x11C2
const (
JongseongNull = Jongseong(iota) // Blank
JongseongKiyeok // ㄱ
JongseongSsangKiyeok // ㄲ
JongseongKiyeokSios // ㄳ
JongseongNieun // ㄴ
JongseongNieunCieuc // ㄵ
JongseongNieunHieuh // ㄶ
JongseongTikeut // ㄷ
JongseongRieul // ㄹ
JongseongRieulKiyeok // ㄺ
JongseongRieulMieum // ㄻ
JongseongRieulPieup // ㄼ
JongseongRieulSios // ㄽ
JongseongRieulThieuth // ㄾ
JongseongRieulHieuh // ㅀ
JongseongRieulPhieuph // ㄿ
JongseongMieum // ㅁ
JongseongPieup // ㅂ
JongseongPieupSios // ㅄ
JongseongSios // ㅅ
JongseongSsangSios // ㅆ
JongseongIeung // ㅇ
JongseongCieuc // ㅈ
JongseongChieuch // ㅊ
JongseongKhieukh // ㅋ
JongseongThieuth // ㅌ
JongseongPhieuph // ㅍ
JongseongHieuh // ㅎ
JongseongBase = 0x11A7
JongseongCount = 28
)
// CompatibilityRune returns the rune for Unicode `Hangul Compatibility Jamo`.
func (j Jongseong) CompatibilityRune() rune {
switch j {
case JongseongKiyeok:
return 'ㄱ'
case JongseongSsangKiyeok:
return 'ㄲ'
case JongseongKiyeokSios:
return 'ㄳ'
case JongseongNieun:
return 'ㄴ'
case JongseongNieunCieuc:
return 'ㄵ'
case JongseongNieunHieuh:
return 'ㄶ'
case JongseongTikeut:
return 'ㄷ'
case JongseongRieul:
return 'ㄹ'
case JongseongRieulKiyeok:
return 'ㄺ'
case JongseongRieulMieum:
return 'ㄻ'
case JongseongRieulPieup:
return 'ㄼ'
case JongseongRieulSios:
return 'ㄽ'
case JongseongRieulThieuth:
return 'ㄾ'
case JongseongRieulPhieuph:
return 'ㄿ'
case JongseongRieulHieuh:
return 'ㅀ'
case JongseongMieum:
return 'ㅁ'
case JongseongPieup:
return 'ㅂ'
case JongseongPieupSios:
return 'ㅄ'
case JongseongSios:
return 'ㅅ'
case JongseongSsangSios:
return 'ㅆ'
case JongseongIeung:
return 'ㅇ'
case JongseongCieuc:
return 'ㅈ'
case JongseongChieuch:
return 'ㅊ'
case JongseongKhieukh:
return 'ㅋ'
case JongseongThieuth:
return 'ㅌ'
case JongseongPhieuph:
return 'ㅍ'
case JongseongHieuh:
return 'ㅎ'
}
return 0
}
// Rune returns the rune for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the rune for `Hangul Compatibility Jamo`.
// Otherwise, it returns the rune for `Hangul Jamo`.
func (j Jongseong) Rune(compatibility bool) rune {
if j == JongseongNull {
return 0
}
if compatibility {
return j.CompatibilityRune()
}
return rune(j + JongseongBase)
}
// Decompose returns the slice of runes for Unicode `Hangul Jamo` or `Hangul Compatibility Jamo`.
// If `compatibility` is true, it returns the slice of runes for `Hangul Compatibility Jamo`.
// Otherwise, it returns the slice of runes for `Hangul Jamo`.
// If `level` is FullDecompose, it decomposes a double consonants into two to three runes.
func (j Jongseong) Decompose(compatibility bool, level int) []rune {
if level < DefaultDecompose || level > FullDecompose {
return nil
}
if j == JongseongNull {
return nil
}
if level == DefaultDecompose {
return []rune{j.Rune(compatibility)}
}
// FullDecompose
switch j {
case JongseongSsangKiyeok:
return []rune{JongseongKiyeok.Rune(compatibility), JongseongKiyeok.Rune(compatibility)}
case JongseongKiyeokSios:
return []rune{JongseongKiyeok.Rune(compatibility), JongseongSios.Rune(compatibility)}
case JongseongNieunCieuc:
return []rune{JongseongNieun.Rune(compatibility), JongseongCieuc.Rune(compatibility)}
case JongseongNieunHieuh:
return []rune{JongseongNieun.Rune(compatibility), JongseongHieuh.Rune(compatibility)}
case JongseongRieulKiyeok:
return []rune{JongseongRieul.Rune(compatibility), JongseongKiyeok.Rune(compatibility)}
case JongseongRieulMieum:
return []rune{JongseongRieul.Rune(compatibility), JongseongMieum.Rune(compatibility)}
case JongseongRieulPieup:
return []rune{JongseongRieul.Rune(compatibility), JongseongPieup.Rune(compatibility)}
case JongseongRieulSios:
return []rune{JongseongRieul.Rune(compatibility), JongseongSios.Rune(compatibility)}
case JongseongRieulThieuth:
return []rune{JongseongRieul.Rune(compatibility), JongseongThieuth.Rune(compatibility)}
case JongseongRieulPhieuph:
return []rune{JongseongRieul.Rune(compatibility), JongseongPhieuph.Rune(compatibility)}
case JongseongRieulHieuh:
return []rune{JongseongRieul.Rune(compatibility), JongseongHieuh.Rune(compatibility)}
case JongseongPieupSios:
return []rune{JongseongPieup.Rune(compatibility), JongseongSios.Rune(compatibility)}
case JongseongSsangSios:
return []rune{JongseongSios.Rune(compatibility), JongseongSios.Rune(compatibility)}
}
// Nothing to decompose
return []rune{j.Rune(compatibility)}
}