normalization/hangul.mbt
normalization/hangul.mbt1///|2/// Hangul syllable constants (from Unicode Chapter 3.12)3/// These enable algorithmic decomposition/composition without lookup tables45///|6/// First Hangul syllable (가)7let hangul_s_base : Int32 = 0xAC0089///|10/// First leading consonant jamo (ᄀ)11let hangul_l_base : Int32 = 0x11001213///|14/// First vowel jamo (ᅡ)15let hangul_v_base : Int32 = 0x11611617///|18/// First trailing consonant jamo (ᆨ) - note: T_BASE itself represents no trailing19let hangul_t_base : Int32 = 0x11A72021///|22/// Number of leading consonant jamo (19)23let hangul_l_count : Int32 = 192425///|26/// Number of vowel jamo (21)27let hangul_v_count : Int32 = 212829///|30/// Number of trailing consonant jamo including none (28)31let hangul_t_count : Int32 = 283233///|34/// V_COUNT * T_COUNT = 58835let hangul_n_count : Int32 = 5883637///|38/// Total number of Hangul syllables (11172)39let hangul_s_count : Int32 = 111724041///|42/// Check if a character is a precomposed Hangul syllable43fn is_hangul_syllable(c : Char) -> Bool {44 let cp = c.to_int()45 cp >= hangul_s_base && cp < hangul_s_base + hangul_s_count46}4748///|49/// Check if a character is a leading consonant jamo (L)50fn is_hangul_l(c : Char) -> Bool {51 let cp = c.to_int()52 cp >= hangul_l_base && cp < hangul_l_base + hangul_l_count53}5455///|56/// Check if a character is a vowel jamo (V)57fn is_hangul_v(c : Char) -> Bool {58 let cp = c.to_int()59 cp >= hangul_v_base && cp < hangul_v_base + hangul_v_count60}6162///|63/// Check if a character is a trailing consonant jamo (T)64/// Note: T_BASE (0x11A7) itself is not a valid trailing jamo65fn is_hangul_t(c : Char) -> Bool {66 let cp = c.to_int()67 cp > hangul_t_base && cp < hangul_t_base + hangul_t_count68}6970///|71/// Check if a character is an LV syllable (no trailing consonant)72fn is_hangul_lv(c : Char) -> Bool {73 if !is_hangul_syllable(c) {74 return false75 }76 (c.to_int() - hangul_s_base) % hangul_t_count == 077}7879///|80/// Try to compose two characters into a Hangul syllable81/// Returns Some(syllable) if composition is possible, None otherwise82fn try_compose_hangul(first : Char, second : Char) -> Char? {83 // L + V -> LV syllable84 if is_hangul_l(first) && is_hangul_v(second) {85 let l_index = first.to_int() - hangul_l_base86 let v_index = second.to_int() - hangul_v_base87 return Some(88 (hangul_s_base + (l_index * hangul_v_count + v_index) * hangul_t_count).unsafe_to_char(),89 )90 }91 // LV + T -> LVT syllable92 if is_hangul_lv(first) && is_hangul_t(second) {93 return Some(94 (first.to_int() + (second.to_int() - hangul_t_base)).unsafe_to_char(),95 )96 }97 None98}99
///|
/// Hangul syllable constants (from Unicode Chapter 3.12)
/// These enable algorithmic decomposition/composition without lookup tables
///|
/// First Hangul syllable (가)
let hangul_s_base : Int32 = 0xAC00
///|
/// First leading consonant jamo (ᄀ)
let hangul_l_base : Int32 = 0x1100
///|
/// First vowel jamo (ᅡ)
let hangul_v_base : Int32 = 0x1161
///|
/// First trailing consonant jamo (ᆨ) - note: T_BASE itself represents no trailing
let hangul_t_base : Int32 = 0x11A7
///|
/// Number of leading consonant jamo (19)
let hangul_l_count : Int32 = 19
///|
/// Number of vowel jamo (21)
let hangul_v_count : Int32 = 21
///|
/// Number of trailing consonant jamo including none (28)
let hangul_t_count : Int32 = 28
///|
/// V_COUNT * T_COUNT = 588
let hangul_n_count : Int32 = 588
///|
/// Total number of Hangul syllables (11172)
let hangul_s_count : Int32 = 11172
///|
/// Check if a character is a precomposed Hangul syllable
fn is_hangul_syllable(c : Char) -> Bool {
let cp = c.to_int()
cp >= hangul_s_base && cp < hangul_s_base + hangul_s_count
}
///|
/// Check if a character is a leading consonant jamo (L)
fn is_hangul_l(c : Char) -> Bool {
let cp = c.to_int()
cp >= hangul_l_base && cp < hangul_l_base + hangul_l_count
}
///|
/// Check if a character is a vowel jamo (V)
fn is_hangul_v(c : Char) -> Bool {
let cp = c.to_int()
cp >= hangul_v_base && cp < hangul_v_base + hangul_v_count
}
///|
/// Check if a character is a trailing consonant jamo (T)
/// Note: T_BASE (0x11A7) itself is not a valid trailing jamo
fn is_hangul_t(c : Char) -> Bool {
let cp = c.to_int()
cp > hangul_t_base && cp < hangul_t_base + hangul_t_count
}
///|
/// Check if a character is an LV syllable (no trailing consonant)
fn is_hangul_lv(c : Char) -> Bool {
if !is_hangul_syllable(c) {
return false
}
(c.to_int() - hangul_s_base) % hangul_t_count == 0
}
///|
/// Try to compose two characters into a Hangul syllable
/// Returns Some(syllable) if composition is possible, None otherwise
fn try_compose_hangul(first : Char, second : Char) -> Char? {
// L + V -> LV syllable
if is_hangul_l(first) && is_hangul_v(second) {
let l_index = first.to_int() - hangul_l_base
let v_index = second.to_int() - hangul_v_base
return Some(
(hangul_s_base + (l_index * hangul_v_count + v_index) * hangul_t_count).unsafe_to_char(),
)
}
// LV + T -> LVT syllable
if is_hangul_lv(first) && is_hangul_t(second) {
return Some(
(first.to_int() + (second.to_int() - hangul_t_base)).unsafe_to_char(),
)
}
None
}