diff --git a/crates/model/src/edit.rs b/crates/model/src/edit.rs index 0b02e094c..464ba6e2c 100644 --- a/crates/model/src/edit.rs +++ b/crates/model/src/edit.rs @@ -528,6 +528,12 @@ pub fn change_case(s: &str, mode: &str) -> String { /// `title` and `sentence` carry their word/sentence state across piece boundaries. Each output /// piece is the changed form of the matching input piece, even where a character changes length. pub fn change_case_parts(parts: &[&str], mode: &str) -> Vec { + if mode == "lower" { + let lower = parts.concat().to_lowercase(); + let mut chars = lower.chars(); + // Context changes sigma's form, while each character's expansion length stays the same. + return parts.iter().map(|s| chars.by_ref().take(s.chars().map(|c| c.to_lowercase().count()).sum()).collect()).collect(); + } let mut start = true; // All characters of all pieces, so word-final sigma sees neighbours across piece boundaries. let all: Vec = parts.iter().flat_map(|s| s.chars()).collect(); @@ -536,7 +542,6 @@ pub fn change_case_parts(parts: &[&str], mode: &str) -> Vec { .iter() .map(|s| match mode { "upper" => s.to_uppercase(), - "lower" => s.to_lowercase(), "toggle" => s.chars().map(|c| if c.is_uppercase() { c.to_lowercase().collect::() } else { c.to_uppercase().collect() }).collect(), _ => { // `title` restarts after whitespace, sentence case after sentence punctuation. @@ -593,6 +598,18 @@ mod tests { assert_eq!(change_case_parts(&["ΟΔΟΣ", " ΜΟΥ"], "title"), ["Οδος", " Μου"]); } + #[test] + fn lowercase_keeps_context_and_run_boundaries() { + assert_eq!(change_case_parts(&["ΟΔΟ", "Σ"], "lower"), ["οδο", "ς"]); + assert_eq!(change_case_parts(&["ΟΣ", "Α"], "lower"), ["οσ", "α"]); + assert_eq!(change_case_parts(&["", "ΟΔΟ", "", "Σ", ""], "lower"), ["", "οδο", "", "ς", ""]); + assert_eq!(change_case_parts(&["İΟΔΟ", "Σ"], "lower"), ["i\u{307}οδο", "ς"]); + assert_eq!(change_case_parts(&["Ο", "\u{301}", "Σ"], "lower"), ["ο", "\u{301}", "ς"]); + assert_eq!(change_case_parts(&["ΟΣ", "\u{301}", "Α"], "lower"), ["οσ", "\u{301}", "α"]); + assert_eq!(change_case_parts(&["", "", ""], "lower"), ["", "", ""]); + assert!(change_case_parts(&[], "lower").is_empty()); + } + #[test] fn insert_and_split() { let mut b = body("Hello");