mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
typeanswer: hoist strip_expected (#3464)
(renamed from prepare_expected) Single use makes it likelier to get inlined, too.
This commit is contained in:
parent
cc45db0e22
commit
cfd1eba7ec
1 changed files with 15 additions and 13 deletions
|
@ -34,12 +34,12 @@ macro_rules! format_typeans {
|
||||||
|
|
||||||
// Public API
|
// Public API
|
||||||
pub fn compare_answer(expected: &str, typed: &str, combining: bool) -> String {
|
pub fn compare_answer(expected: &str, typed: &str, combining: bool) -> String {
|
||||||
if typed.is_empty() {
|
let stripped = strip_expected(expected);
|
||||||
format_typeans!(htmlescape::encode_minimal(&prepare_expected(expected)))
|
|
||||||
} else if combining {
|
match typed.is_empty() {
|
||||||
Diff::new(expected, typed).to_html()
|
true => format_typeans!(htmlescape::encode_minimal(&stripped)),
|
||||||
} else {
|
false if combining => Diff::new(&stripped, typed).to_html(),
|
||||||
DiffNonCombining::new(expected, typed).to_html()
|
false => DiffNonCombining::new(&stripped, typed).to_html(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ fn slice(chars: &[char], start: usize, end: usize) -> String {
|
||||||
chars[start..end].iter().collect()
|
chars[start..end].iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_expected(expected: &str) -> String {
|
fn strip_expected(expected: &str) -> String {
|
||||||
let no_av_tags = strip_av_tags(expected);
|
let no_av_tags = strip_av_tags(expected);
|
||||||
let no_linebreaks = LINEBREAKS.replace_all(&no_av_tags, " ");
|
let no_linebreaks = LINEBREAKS.replace_all(&no_av_tags, " ");
|
||||||
strip_html(&no_linebreaks).trim().to_string()
|
strip_html(&no_linebreaks).trim().to_string()
|
||||||
|
@ -167,7 +167,7 @@ impl DiffTrait for Diff {
|
||||||
fn new(expected: &str, typed: &str) -> Self {
|
fn new(expected: &str, typed: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
typed: Self::normalize_typed(typed),
|
typed: Self::normalize_typed(typed),
|
||||||
expected: normalize(&prepare_expected(expected)),
|
expected: normalize(expected),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn normalize_typed(typed: &str) -> Vec<char> {
|
fn normalize_typed(typed: &str) -> Vec<char> {
|
||||||
|
@ -202,7 +202,7 @@ impl DiffTrait for DiffNonCombining {
|
||||||
let mut expected_stripped = String::new();
|
let mut expected_stripped = String::new();
|
||||||
// tokenized into "char+combining" for final rendering
|
// tokenized into "char+combining" for final rendering
|
||||||
let mut expected_split: Vec<String> = Vec::new();
|
let mut expected_split: Vec<String> = Vec::new();
|
||||||
for c in normalize(&prepare_expected(expected)) {
|
for c in normalize(expected) {
|
||||||
if unicode_normalization::char::is_combining_mark(c) {
|
if unicode_normalization::char::is_combining_mark(c) {
|
||||||
if let Some(last) = expected_split.last_mut() {
|
if let Some(last) = expected_split.last_mut() {
|
||||||
last.push(c);
|
last.push(c);
|
||||||
|
@ -219,7 +219,7 @@ impl DiffTrait for DiffNonCombining {
|
||||||
expected: expected_stripped.chars().collect(),
|
expected: expected_stripped.chars().collect(),
|
||||||
},
|
},
|
||||||
expected_split,
|
expected_split,
|
||||||
expected_original: prepare_expected(expected),
|
expected_original: expected.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +340,8 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn html_and_media() {
|
fn html_and_media() {
|
||||||
let ctx = Diff::new("[sound:foo.mp3]<b>1</b> 2", "1 2");
|
let stripped = strip_expected("[sound:foo.mp3]<b>1</b> 2");
|
||||||
|
let ctx = Diff::new(&stripped, "1 2");
|
||||||
// the spacing is handled by wrapping html output in white-space: pre-wrap
|
// the spacing is handled by wrapping html output in white-space: pre-wrap
|
||||||
assert_eq!(ctx.to_tokens().expected_tokens, &[good("1 2")]);
|
assert_eq!(ctx.to_tokens().expected_tokens, &[good("1 2")]);
|
||||||
}
|
}
|
||||||
|
@ -387,9 +388,10 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tags_removed() {
|
fn tags_removed() {
|
||||||
assert_eq!(prepare_expected("<div>123</div>"), "123");
|
let stripped = strip_expected("<div>123</div>");
|
||||||
|
assert_eq!(stripped, "123");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Diff::new("<div>123</div>", "123").to_html(),
|
Diff::new(&stripped, "123").to_html(),
|
||||||
"<code id=typeans><span class=typeGood>123</span></code>"
|
"<code id=typeans><span class=typeGood>123</span></code>"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue