Skip to main content

lekhani_parser/
engine.rs

1//! Zero-Allocation High-Speed Phonetic Parsing Engine
2
3use std::sync::Arc;
4use super::compiler::CompiledLayout;
5use super::rule::{ConditionScope, MatchType};
6use super::tables::*;
7
8#[derive(Debug, Clone)]
9pub struct LekhaniParser {
10    pub layout: Arc<CompiledLayout>,
11}
12
13impl LekhaniParser {
14    pub fn new(layout: Arc<CompiledLayout>) -> Self {
15        Self { layout }
16    }
17
18    /// Convert input string into Unicode Bengali, writing directly into `output`.
19    /// Zero heap allocations on the hot path.
20    pub fn convert_into(&self, input: &str, output: &mut String) {
21        if input.is_empty() {
22            return;
23        }
24
25        // Handle Latin Chandra Bindu pre-normalization: e.g. "c^ad" -> "ca^d"
26        let norm_storage: String;
27        let effective_input = if input.as_bytes().contains(&b'^') {
28            norm_storage = normalize_chandra_latin(input);
29            norm_storage.as_str()
30        } else {
31            input
32        };
33
34        let has_combining = if effective_input.is_ascii() {
35            self.convert_ascii_into(effective_input, output)
36        } else {
37            self.convert_mixed_into(effective_input, output)
38        };
39
40        // Post-process to ensure canonical Unicode Combining Class sequence
41        if has_combining {
42            canonicalize_combining_marks(output);
43        }
44    }
45
46    /// Convert input string using pure raw grammar rules without extra diacritic reordering.
47    /// Exactly matches the raw Avro grammar specification.
48    pub fn convert_raw_into(&self, input: &str, output: &mut String) {
49        if input.is_empty() {
50            return;
51        }
52
53        if input.is_ascii() {
54            self.convert_ascii_into(input, output);
55        } else {
56            self.convert_mixed_into(input, output);
57        }
58    }
59
60    /// Convert input string into Unicode Bengali, returning a newly allocated String.
61    pub fn convert(&self, input: &str) -> String {
62        let mut out = String::with_capacity(input.len() * 3);
63        self.convert_into(input, &mut out);
64        out
65    }
66
67    /// Convert input string using raw rules, returning a newly allocated String.
68    pub fn convert_raw(&self, input: &str) -> String {
69        let mut out = String::with_capacity(input.len() * 3);
70        self.convert_raw_into(input, &mut out);
71        out
72    }
73
74    /// Fast path for pure ASCII input. Returns whether any combining marks were emitted.
75    fn convert_ascii_into(&self, input: &str, output: &mut String) -> bool {
76        let in_bytes = input.as_bytes();
77        let len = in_bytes.len();
78
79        // Stack-allocated case-normalized buffer for up to 128 bytes only if normalization needed
80        let mut stack_buf = [0u8; 128];
81        let heap_buf: Vec<u8>;
82        let fixed: &[u8] = if in_bytes.iter().any(|&b| needs_avro_norm(b)) {
83            if len <= 128 {
84                for i in 0..len {
85                    stack_buf[i] = normalize_avro_byte(in_bytes[i]);
86                }
87                &stack_buf[..len]
88            } else {
89                heap_buf = in_bytes.iter().map(|&b| normalize_avro_byte(b)).collect();
90                &heap_buf
91            }
92        } else {
93            in_bytes
94        };
95
96        let mut has_combining = false;
97        let mut cur = 0;
98        while cur < len {
99            if let Some((pat_idx, match_len)) = self.layout.trie.longest_match(fixed, cur) {
100                let pat = &self.layout.patterns[pat_idx as usize];
101                if pat.may_have_combining {
102                    has_combining = true;
103                }
104                let start = cur as isize;
105                let end = (cur + match_len) as isize;
106                let mut matched_rule = false;
107
108                for rule in &pat.rules {
109                    let mut conditions_met = true;
110
111                    for cond in &rule.conditions {
112                        let is_neg = cond.is_negative;
113                        let chk = match cond.match_type {
114                            MatchType::Prefix => start - 1,
115                            MatchType::Suffix => end,
116                        };
117
118                        let cond_result = match cond.scope {
119                            ConditionScope::Punctuation => {
120                                if (cond.match_type == MatchType::Prefix && chk < 0)
121                                    || (cond.match_type == MatchType::Suffix && chk >= len as isize)
122                                {
123                                    true
124                                } else if chk >= 0 && (chk as usize) < len {
125                                    is_punctuation_byte(fixed[chk as usize])
126                                } else {
127                                    false
128                                }
129                            }
130                            ConditionScope::Vowel => {
131                                if chk >= 0 && (chk as usize) < len {
132                                    is_vowel_byte(fixed[chk as usize])
133                                } else {
134                                    false
135                                }
136                            }
137                            ConditionScope::Consonant => {
138                                if chk >= 0 && (chk as usize) < len {
139                                    is_consonant_byte(fixed[chk as usize])
140                                } else {
141                                    false
142                                }
143                            }
144                            ConditionScope::Number => {
145                                if chk >= 0 && (chk as usize) < len {
146                                    is_number_byte(fixed[chk as usize])
147                                } else {
148                                    false
149                                }
150                            }
151                            ConditionScope::Exact => {
152                                let needle = cond.exact_value.as_bytes();
153                                let n_len = needle.len() as isize;
154                                let (s, e) = match cond.match_type {
155                                    MatchType::Prefix => (start - n_len, start),
156                                    MatchType::Suffix => (end, end + n_len),
157                                };
158
159                                if s >= 0 && e < len as isize {
160                                    &fixed[s as usize..e as usize] == needle
161                                } else {
162                                    false
163                                }
164
165                            }
166                        };
167
168                        if cond_result == is_neg {
169                            conditions_met = false;
170                            break;
171                        }
172                    }
173
174                    if conditions_met {
175                        output.push_str(&rule.replace);
176                        cur += match_len;
177                        matched_rule = true;
178                        break;
179                    }
180                }
181
182                if !matched_rule {
183                    output.push_str(&pat.default_replace);
184                    cur += match_len;
185                }
186            } else {
187                output.push(fixed[cur] as char);
188                cur += 1;
189            }
190        }
191        has_combining
192    }
193
194    /// Mixed ASCII / non-ASCII path: segments ASCII chunks and passes Unicode directly
195    fn convert_mixed_into(&self, input: &str, output: &mut String) -> bool {
196        let mut ascii_chunk = String::with_capacity(input.len());
197        let mut has_combining = false;
198
199        for ch in input.chars() {
200            if ch.is_ascii() {
201                ascii_chunk.push(ch);
202            } else {
203                if !ascii_chunk.is_empty() {
204                    if self.convert_ascii_into(&ascii_chunk, output) {
205                        has_combining = true;
206                    }
207                    ascii_chunk.clear();
208                }
209                output.push(ch);
210            }
211        }
212
213        if !ascii_chunk.is_empty() {
214            if self.convert_ascii_into(&ascii_chunk, output) {
215                has_combining = true;
216            }
217        }
218        has_combining
219    }
220}
221
222/// Normalizes Latin caret (^) placement: e.g. "c^ad" -> "ca^d"
223fn normalize_chandra_latin(text: &str) -> String {
224    let chars: Vec<char> = text.chars().collect();
225    let mut out = String::with_capacity(text.len());
226    let mut i = 0;
227    while i < chars.len() {
228        if chars[i] == '^' && i + 1 < chars.len() {
229            let next = chars[i + 1];
230            if is_latin_vowel(next) {
231                let mut v_end = i + 1;
232                while v_end < chars.len() && is_latin_vowel(chars[v_end]) {
233                    v_end += 1;
234                }
235                for v in &chars[i + 1..v_end] {
236                    out.push(*v);
237                }
238                out.push('^');
239                i = v_end;
240                continue;
241            }
242        }
243        out.push(chars[i]);
244        i += 1;
245    }
246    out
247}
248
249#[inline(always)]
250fn is_latin_vowel(c: char) -> bool {
251    matches!(
252        c,
253        'a' | 'e' | 'i' | 'o' | 'u' | 'A' | 'E' | 'I' | 'O' | 'U' | 'y' | 'Y' | '`'
254    )
255}
256
257/// Canonicalize Bengali Unicode combining sequences (Nukta composition & Chandra Bindu ordering)
258fn canonicalize_combining_marks(text: &mut String) {
259    if text.contains('\u{09BC}') {
260        let replaced = text
261            .replace("\u{09AF}\u{09BC}", "য়")
262            .replace("\u{09A1}\u{09BC}", "ড়")
263            .replace("\u{09A2}\u{09BC}", "ঢ়");
264        *text = replaced;
265    }
266
267    if text.contains('ঁ') {
268        let replaced = text
269            .replace("ঁআ", "াঁ")
270            .replace("ঁা", "াঁ")
271            .replace("ঁএ", "েঁ")
272            .replace("ঁে", "েঁ")
273            .replace("ঁই", "িঁ")
274            .replace("ঁি", "িঁ")
275            .replace("ঁউ", "ুঁ")
276            .replace("ঁু", "ুঁ")
277            .replace("ঁও", "োঁ")
278            .replace("ঁো", "োঁ")
279            .replace("ঁঐ", "ৈঁ")
280            .replace("ঁৈ", "ৈঁ")
281            .replace("ঁঔ", "ৌঁ")
282            .replace("ঁৌ", "ৌঁ")
283            .replace("ঁৃ", "ৃঁ");
284        *text = replaced;
285    }
286}