From df2dd3394eedab4b6a53acfac44db52d5728105f Mon Sep 17 00:00:00 2001 From: Andreas Reis <887320+twwn@users.noreply.github.com> Date: Mon, 30 Sep 2024 01:27:26 +0200 Subject: [PATCH] typeanswer: 'simplify' by removing normalize_typed (requiring a bool parameter) I'd prefer to keep this extra method. --- rslib/src/typeanswer.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/rslib/src/typeanswer.rs b/rslib/src/typeanswer.rs index c1d6547fb..54f037ae0 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 { @@ -108,8 +107,17 @@ trait DiffTrait { } // Utility Functions -fn normalize(string: &str) -> Vec { - normalize_to_nfkd(string).chars().collect() +fn normalize(string: &str, retain_combining: bool) -> Vec { + let binding = normalize_to_nfkd(string); + let chars = binding.chars(); + + if retain_combining { + chars.collect() + } else { + chars + .filter(|c| !unicode_normalization::char::is_combining_mark(*c)) + .collect() + } } fn slice(chars: &[char], start: usize, end: usize) -> String { @@ -166,13 +174,10 @@ impl DiffTrait for Diff { fn new(expected: &str, typed: &str) -> Self { Self { - typed: Self::normalize_typed(typed), - expected: normalize(&prepare_expected(expected)), + typed: normalize(typed, true), + expected: normalize(&prepare_expected(expected), true), } } - fn normalize_typed(typed: &str) -> Vec { - normalize(typed) - } fn render_expected_tokens(&self, tokens: &[DiffToken]) -> String { render_tokens(tokens) @@ -202,7 +207,7 @@ impl DiffTrait for DiffNonCombining { let mut expected_stripped = String::new(); // tokenized into "char+combining" for final rendering let mut expected_split: Vec = Vec::new(); - for c in normalize(&prepare_expected(expected)) { + for c in normalize(&prepare_expected(expected), true) { if unicode_normalization::char::is_combining_mark(c) { if let Some(last) = expected_split.last_mut() { last.push(c); @@ -215,7 +220,7 @@ impl DiffTrait for DiffNonCombining { Self { base: Diff { - typed: Self::normalize_typed(typed), + typed: normalize(typed, false), expected: expected_stripped.chars().collect(), }, expected_split, @@ -223,13 +228,6 @@ impl DiffTrait for DiffNonCombining { } } - 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.