mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-16 23:24:42 +00:00
fix: lowercase Greek sigma across formatting runs (#2151)
Use whole-text lowercase context while preserving the output pieces for each formatting run, including Unicode expansion.
This commit is contained in:
1 file changed
+18
-1
@@ -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<String> {
|
||||
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<char> = parts.iter().flat_map(|s| s.chars()).collect();
|
||||
@@ -536,7 +542,6 @@ pub fn change_case_parts(parts: &[&str], mode: &str) -> Vec<String> {
|
||||
.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::<String>() } 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");
|
||||
|
||||
Reference in new issue
Block a user