Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeanswer: [type:nc] - use nfkd again #3627

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions rslib/src/typeanswer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::sync::LazyLock;
use difflib::sequencematcher::SequenceMatcher;
use regex::Regex;
use unic_ucd_category::GeneralCategory;
use unicode_normalization::char::is_combining_mark;
use unicode_normalization::UnicodeNormalization;

use crate::card_rendering::strip_av_tags;
use crate::text::normalize_to_nfc;
Expand Down Expand Up @@ -195,18 +197,12 @@ impl DiffTrait for DiffNonCombining {

fn new(expected: &str, typed: &str) -> Self {
// filter out combining elements
let mut typed_stripped: Vec<char> = Vec::new();
let typed_stripped: Vec<char> = typed.nfkd().filter(|&c| !is_combining_mark(c)).collect();
let mut expected_stripped: Vec<char> = Vec::new();
// also tokenize into "char+combining" for final rendering
let mut expected_split: Vec<String> = Vec::new();

for c in normalize(typed) {
if !unicode_normalization::char::is_combining_mark(c) {
typed_stripped.push(c);
}
}

for c in normalize(expected) {
for c in expected.nfkd() {
if unicode_normalization::char::is_combining_mark(c) {
if let Some(last) = expected_split.last_mut() {
last.push(c);
Expand Down Expand Up @@ -423,5 +419,9 @@ mod test {
compare_answer("חוֹף", "חופ", false),
"<code id=typeans><span class=typeGood>חו</span><span class=typeBad>פ</span><br><span id=typearrow>&darr;</span><br><span class=typeGood>חוֹ</span><span class=typeMissed>ף</span></code>"
);
assert_eq!(
compare_answer("ば", "は", false),
"<code id=typeans><span class=typeGood>ば</span></code>"
);
}
}