1use crate::syllable::{self, CHO, JONG, JUNG};
6
7pub const CHO_COMPAT: [u32; 19] = [
9 0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147,
10 0x3148, 0x3149, 0x314A, 0x314B, 0x314C, 0x314D, 0x314E,
11];
12
13pub const JUNG_COMPAT: [u32; 21] = [
15 0x314F, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, 0x3158, 0x3159, 0x315A,
16 0x315B, 0x315C, 0x315D, 0x315E, 0x315F, 0x3160, 0x3161, 0x3162, 0x3163,
17];
18
19pub const JONG_COMPAT: [u32; 28] = [
21 0x0000, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3139, 0x313A, 0x313B, 0x313C,
22 0x313D, 0x313E, 0x313F, 0x3140, 0x3141, 0x3142, 0x3144, 0x3145, 0x3146, 0x3147, 0x3148, 0x314A,
23 0x314B, 0x314C, 0x314D, 0x314E,
24];
25
26pub fn cho_cp_for_compat(compat: u32) -> Option<u32> {
28 CHO_COMPAT.iter().position(|&c| c == compat).map(|i| CHO[i])
29}
30
31pub fn jung_cp_for_compat(compat: u32) -> Option<u32> {
33 JUNG_COMPAT
34 .iter()
35 .position(|&c| c == compat)
36 .map(|i| JUNG[i])
37}
38
39pub fn jong_cp_for_compat(compat: u32) -> Option<u32> {
41 JONG_COMPAT
42 .iter()
43 .enumerate()
44 .skip(1)
45 .find(|(_, &c)| c == compat)
46 .map(|(i, _)| JONG[i])
47}
48
49pub fn is_vowel_compat(compat: u32) -> bool {
51 (0x314F..=0x3163).contains(&compat)
52}
53
54pub fn cho_to_jong(cho_cp: u32) -> Option<u32> {
57 let compat = syllable::cho_index(cho_cp).map(|i| CHO_COMPAT[i as usize])?;
58 jong_cp_for_compat(compat)
59}
60
61pub fn jong_to_cho(jong_cp: u32) -> Option<u32> {
64 let compat = syllable::jong_index(jong_cp).map(|i| JONG_COMPAT[i as usize])?;
65 cho_cp_for_compat(compat)
66}
67
68pub fn cho_compat(cho_cp: u32) -> Option<u32> {
71 syllable::cho_index(cho_cp).map(|i| CHO_COMPAT[i as usize])
72}
73pub fn jung_compat(jung_cp: u32) -> Option<u32> {
75 syllable::jung_index(jung_cp).map(|i| JUNG_COMPAT[i as usize])
76}
77pub fn jong_compat(jong_cp: u32) -> Option<u32> {
79 syllable::jong_index(jong_cp).map(|i| JONG_COMPAT[i as usize])
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn compat_bridge_roundtrip() {
88 assert_eq!(cho_compat(0x1100), Some(0x3131));
90 assert_eq!(cho_cp_for_compat(0x3131), Some(0x1100));
91 assert_eq!(jong_compat(0x11A8), Some(0x3131));
93 assert_eq!(jong_cp_for_compat(0x3131), Some(0x11A8));
94 assert_eq!(jung_compat(0x1161), Some(0x314F));
96 assert_eq!(jung_cp_for_compat(0x314F), Some(0x1161));
97 }
98
99 #[test]
100 fn cho_jong_conversion() {
101 assert_eq!(cho_to_jong(0x1100), Some(0x11A8));
103 assert_eq!(jong_to_cho(0x11A8), Some(0x1100));
104 assert_eq!(cho_to_jong(0x110B), Some(0x11BC));
106 assert_eq!(jong_to_cho(0x11BC), Some(0x110B));
107 }
108
109 #[test]
110 fn is_vowel() {
111 assert!(is_vowel_compat(0x314F)); assert!(is_vowel_compat(0x3163)); assert!(!is_vowel_compat(0x3131)); }
115}