From 4d5af8fe4cd0a06dbc6d91492e793e429a9954c7 Mon Sep 17 00:00:00 2001 From: Brady Fomegne Date: Wed, 18 Sep 2024 16:53:05 +0100 Subject: [PATCH] fix(translator): fix problem with the input length (#262) This commit fix the problem of panick when the input contains only one character and this character has a length >= 2. The solution consisted to count the number of characters present in the input instead of the input length. --- engine/translator/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/engine/translator/src/lib.rs b/engine/translator/src/lib.rs index 7361939..c9f8ab8 100644 --- a/engine/translator/src/lib.rs +++ b/engine/translator/src/lib.rs @@ -341,7 +341,7 @@ impl Translator { #[cfg(feature = "rhai")] let engine = Engine::new(); let predicates = self.dictionary.iter().filter_map(|(key, values)| { - if input.len() < 2 || input.len() > key.len() || key[0..1] != input[0..1] { + if input.chars().count() < 2 || input.len() > key.len() || key[0..1] != input[0..1] { return None; }; @@ -452,6 +452,8 @@ mod tests { #[cfg(feature = "rhai")] let mut translator = Translator::new(dictionary, true); + // Test the filtering + translator.translate("รน"); // #[cfg(feature = "rhai")] {