From 8fd15da7fac5487d57ecc80b7cd9a58da2617b46 Mon Sep 17 00:00:00 2001 From: Andreas Reis <887320+twwn@users.noreply.github.com> Date: Sun, 6 Oct 2024 15:40:46 +0200 Subject: [PATCH] typeanswer: cleanup * DiffNonCombining's new() used String where plain Vec is appropriate * get rid of normalize_typed for DiffTrait again by pulling code into DiffNonCombining's new() * two DiffNonCombining testcases --- rslib/src/typeanswer.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/rslib/src/typeanswer.rs b/rslib/src/typeanswer.rs index 5277e88ff16..bd919579fa5 100644 --- a/rslib/src/typeanswer.rs +++ b/rslib/src/typeanswer.rs @@ -50,7 +50,6 @@ trait DiffTrait { fn get_expected_original(&self) -> Cow; fn new(expected: &str, typed: &str) -> Self; - fn normalize_typed(typed: &str) -> Vec; // Entry Point fn to_html(&self) -> String { @@ -166,13 +165,10 @@ impl DiffTrait for Diff { fn new(expected: &str, typed: &str) -> Self { Self { - typed: Self::normalize_typed(typed), + typed: normalize(typed), expected: normalize(expected), } } - fn normalize_typed(typed: &str) -> Vec { - normalize(typed) - } fn render_expected_tokens(&self, tokens: &[DiffToken]) -> String { render_tokens(tokens) @@ -199,9 +195,17 @@ impl DiffTrait for DiffNonCombining { fn new(expected: &str, typed: &str) -> Self { // filter out combining elements - let mut expected_stripped = String::new(); - // tokenized into "char+combining" for final rendering + let mut typed_stripped: Vec = Vec::new(); + let mut expected_stripped: Vec = Vec::new(); + // also tokenize into "char+combining" for final rendering let mut expected_split: Vec = Vec::new(); + + for c in normalize(typed) { + if !unicode_normalization::char::is_combining_mark(c) { + typed_stripped.push(c); + } + } + for c in normalize(expected) { if unicode_normalization::char::is_combining_mark(c) { if let Some(last) = expected_split.last_mut() { @@ -215,21 +219,14 @@ impl DiffTrait for DiffNonCombining { Self { base: Diff { - typed: Self::normalize_typed(typed), - expected: expected_stripped.chars().collect(), + typed: typed_stripped, + expected: expected_stripped, }, expected_split, expected_original: expected.to_string(), } } - fn normalize_typed(typed: &str) -> Vec { - normalize_to_nfkd(typed) - .chars() - .filter(|c| !unicode_normalization::char::is_combining_mark(*c)) - .collect() - } - // Since the combining characters are still required learning content, use // expected_split to show them directly in the "expected" line, rather than // having to otherwise e.g. include their field twice in the note template. @@ -419,4 +416,16 @@ mod test { "1123

123
" ); } + + #[test] + fn combining_marks() { + assert_eq!( + compare_answer("שִׁנּוּן", "שנון", false), + "שִׁנּוּן" + ); + assert_eq!( + compare_answer("חוֹף", "חופ", false), + "חופ

חוֹף
" + ); + } }