From 949f1e4acf39dd0d3af712cc99583e26c1d62af3 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 10 May 2020 17:47:27 +0200 Subject: [PATCH] Add case propagation also with prefix symbol. Fix #244 --- src/matcher/mod.rs | 35 ++++++++++++++++++++++++++++++++++- src/render/default.rs | 12 ++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/matcher/mod.rs b/src/matcher/mod.rs index 5b68433..84f3dbc 100644 --- a/src/matcher/mod.rs +++ b/src/matcher/mod.rs @@ -92,7 +92,14 @@ impl<'a> From<&'a AutoMatch> for Match{ let first_capitalized : Vec = triggers.iter().map(|trigger| { let capitalized = trigger.clone(); let mut v: Vec = capitalized.chars().collect(); - v[0] = v[0].to_uppercase().nth(0).unwrap(); + + // Capitalize the first alphabetic letter + // See issue #244 + let first_alphabetic = v.iter().position(|c| { + c.is_alphabetic() + }).unwrap_or(0); + + v[first_alphabetic] = v[first_alphabetic].to_uppercase().nth(0).unwrap(); v.into_iter().collect() }).collect(); @@ -482,4 +489,30 @@ mod tests { let _match : Match = serde_yaml::from_str(match_str).unwrap(); } + + #[test] + fn test_match_propagate_case_with_prefix_symbol() { + let match_str = r###" + trigger: ":hello" + replace: "This is a test" + propagate_case: true + "###; + + let _match : Match = serde_yaml::from_str(match_str).unwrap(); + + assert_eq!(_match.triggers, vec![":hello", ":Hello", ":HELLO"]) + } + + #[test] + fn test_match_propagate_case_non_alphabetic_should_not_crash() { + let match_str = r###" + trigger: ":.." + replace: "This is a test" + propagate_case: true + "###; + + let _match : Match = serde_yaml::from_str(match_str).unwrap(); + + assert_eq!(_match.triggers, vec![":..", ":..", ":.."]) + } } \ No newline at end of file diff --git a/src/render/default.rs b/src/render/default.rs index ec73178..b95d88d 100644 --- a/src/render/default.rs +++ b/src/render/default.rs @@ -173,8 +173,16 @@ impl super::Renderer for DefaultRenderer { // Handle case propagation let target_string = if m.propagate_case { let trigger = &m.triggers[trigger_offset]; - let first_char = trigger.chars().nth(0); - let second_char = trigger.chars().nth(1); + + // The check should be carried out from the position of the first + // alphabetic letter + // See issue #244 + let first_alphabetic = trigger.chars().position(|c| { + c.is_alphabetic() + }).unwrap_or(0); + + let first_char = trigger.chars().nth(first_alphabetic); + let second_char = trigger.chars().nth(first_alphabetic + 1); let mode: i32 = if let Some(first_char) = first_char { if first_char.is_uppercase() { if let Some(second_char) = second_char {