|
| 1 | +use core::iter::FusedIterator; |
| 2 | + |
| 3 | +use tinyvec::ArrayVec; |
| 4 | + |
| 5 | +use crate::normalize::hangul_constants::{N_COUNT, S_BASE, T_COUNT}; |
| 6 | + |
| 7 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 8 | +enum JamoKind { |
| 9 | + L, |
| 10 | + V, |
| 11 | + T, |
| 12 | +} |
| 13 | + |
| 14 | +impl JamoKind { |
| 15 | + fn of(c: char) -> (Option<Self>, Option<Self>) { |
| 16 | + match c { |
| 17 | + // L |
| 18 | + '\u{1100}'..='\u{115F}' | '\u{A960}'..='\u{A97C}' => { |
| 19 | + (Some(JamoKind::L), Some(JamoKind::L)) |
| 20 | + } |
| 21 | + // V |
| 22 | + '\u{1160}'..='\u{11A7}' | '\u{D7B0}'..='\u{D7C6}' => { |
| 23 | + (Some(JamoKind::V), Some(JamoKind::V)) |
| 24 | + } |
| 25 | + // T |
| 26 | + '\u{11A8}'..='\u{11FF}' | '\u{D7CB}'..='\u{D7FB}' => { |
| 27 | + (Some(JamoKind::T), Some(JamoKind::T)) |
| 28 | + } |
| 29 | + // LV or LVT |
| 30 | + '\u{AC00}'..='\u{D7A3}' => ( |
| 31 | + Some(JamoKind::L), |
| 32 | + Some(if ((u32::from(c) - S_BASE) % N_COUNT) % T_COUNT == 0 { |
| 33 | + // LV |
| 34 | + JamoKind::V |
| 35 | + } else { |
| 36 | + // LVT |
| 37 | + JamoKind::T |
| 38 | + }), |
| 39 | + ), |
| 40 | + _ => (None, None), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Iterator over a string's characters, with '\u{115F}' and '\u{1160}' inserted |
| 46 | +/// where needed to ensure all Korean syllable blocks are in standard form |
| 47 | +/// by [UAX29 rules](https://www.unicode.org/reports/tr29/#Standard_Korean_Syllables). |
| 48 | +#[derive(Clone, Debug)] |
| 49 | +pub struct StandardKoreanSyllables<I> { |
| 50 | + prev_end_jamo_kind: Option<JamoKind>, |
| 51 | + buf: ArrayVec<[Option<char>; 3]>, |
| 52 | + inner: I, |
| 53 | +} |
| 54 | + |
| 55 | +impl<I: Iterator<Item = char>> Iterator for StandardKoreanSyllables<I> { |
| 56 | + type Item = char; |
| 57 | + |
| 58 | + fn next(&mut self) -> Option<Self::Item> { |
| 59 | + if let Some(c) = self.buf.pop() { |
| 60 | + c |
| 61 | + } else { |
| 62 | + let next_c = self.inner.next(); |
| 63 | + let prev_end_jamo_kind = self.prev_end_jamo_kind; |
| 64 | + let (next_start_jamo_kind, next_end_jamo_kind) = |
| 65 | + next_c.map_or((None, None), JamoKind::of); |
| 66 | + self.prev_end_jamo_kind = next_end_jamo_kind; |
| 67 | + |
| 68 | + insert_fillers( |
| 69 | + next_c, |
| 70 | + prev_end_jamo_kind, |
| 71 | + next_start_jamo_kind, |
| 72 | + &mut self.buf, |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + #[inline] |
| 78 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 79 | + let (inner_lo, inner_hi) = self.inner.size_hint(); |
| 80 | + let add_factor: usize = self.buf.len(); |
| 81 | + ( |
| 82 | + inner_lo.saturating_add(add_factor), |
| 83 | + inner_hi |
| 84 | + .and_then(|h| h.checked_mul(3)) // T → Lf Vf T |
| 85 | + .and_then(|h| h.checked_add(add_factor)), |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<I: Iterator<Item = char> + FusedIterator> FusedIterator for StandardKoreanSyllables<I> {} |
| 91 | + |
| 92 | +#[inline] |
| 93 | +fn insert_fillers( |
| 94 | + next_c: Option<char>, |
| 95 | + prev_end_jamo_kind: Option<JamoKind>, |
| 96 | + next_start_jamo_kind: Option<JamoKind>, |
| 97 | + buf: &mut ArrayVec<[Option<char>; 3]>, |
| 98 | +) -> Option<char> { |
| 99 | + match (prev_end_jamo_kind, next_start_jamo_kind) { |
| 100 | + // Insert choseong filler before V not preceded by L or V |
| 101 | + (None, Some(JamoKind::V)) | (Some(JamoKind::T), Some(JamoKind::V)) => { |
| 102 | + buf.push(next_c); |
| 103 | + Some('\u{115F}') |
| 104 | + } |
| 105 | + // Insert choseong and jungseong fillers before T preceded non-jamo |
| 106 | + (None, Some(JamoKind::T)) => { |
| 107 | + buf.push(next_c); |
| 108 | + buf.push(Some('\u{1160}')); |
| 109 | + Some('\u{115F}') |
| 110 | + } |
| 111 | + // Insert V filler between L and non-jamo |
| 112 | + (Some(JamoKind::L), None) => { |
| 113 | + buf.push(next_c); |
| 114 | + Some('\u{1160}') |
| 115 | + } |
| 116 | + // For L followed by T, insert V filler, L filler, then another V filler |
| 117 | + (Some(JamoKind::L), Some(JamoKind::T)) => { |
| 118 | + buf.push(next_c); |
| 119 | + buf.push(Some('\u{1160}')); |
| 120 | + buf.push(Some('\u{115F}')); |
| 121 | + Some('\u{1160}') |
| 122 | + } |
| 123 | + _ => next_c, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<I> StandardKoreanSyllables<I> { |
| 128 | + #[inline] |
| 129 | + pub(crate) fn new(iter: I) -> Self { |
| 130 | + Self { |
| 131 | + prev_end_jamo_kind: None, |
| 132 | + buf: ArrayVec::new(), |
| 133 | + inner: iter, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments