Skip to content

Commit

Permalink
html: refactor split point calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Nov 14, 2024
1 parent fd78d7e commit 4fbbbfc
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,35 @@ impl regex::Replacer for BionicWordReplacer {
dst.push_str(space);

let word = caps.get(2).unwrap().as_str();
if word.len() <= 1 {
let idx = split_at(word);
if idx == 0 {
dst.push_str(word);
} else {
let midpoint = midpoint(word);
dst.push_str("<b>");
dst.push_str(&word[..midpoint]);
dst.push_str(&word[..idx]);
dst.push_str("</b>");
dst.push_str(&word[midpoint..]);
dst.push_str(&word[idx..]);
}
}
}

fn midpoint(word: &str) -> usize {
let mut midpoint;
if word.len() <= 3 {
midpoint = 1;
} else {
midpoint = word.len() / 2;
}
while !word.is_char_boundary(midpoint) {
midpoint += 1;
fn split_at(word: &str) -> usize {
let n = match word.chars().count() {
c if c <= 1 => 0,
c if c <= 3 => 1,
c if c <= 8 => 2,
c if c <= 12 => 4,
_ => 5,
};
if n == 0 {
return 0;
}
midpoint

word.char_indices()
.skip(n)
.next()
.map(|(p, _)| p)
.expect(word)
}

#[cfg(test)]
Expand All @@ -107,15 +113,23 @@ mod tests {
#[test]
fn test_rewrite_to_bionic() {
let tests = vec![
("<p>hello world</p>", "<p><b>he</b>llo <b>wo</b>rld</p>"),
("<li>hello</li>", "<li><b>he</b>llo</li>"),
("The", "<b>T</b>he"),
("Bionic", "<b>Bi</b>onic"),
("reading", "<b>re</b>ading"),
("method", "<b>me</b>thod"),
("can", "<b>c</b>an"),
("be", "<b>b</b>e"),
("individually", "<b>indi</b>vidually"),
("flexibility", "<b>flex</b>ibility"),
("customization", "<b>custo</b>mization"),
("highlighted", "<b>high</b>lighted"),
];

for (input, expected) in tests {
let input = format!("<p>{}</p>", input);
let mut input = input.as_bytes();
let mut output = Vec::new();
rewrite_to_bionic(&mut output, &mut input).unwrap();
assert_eq!(String::from_utf8(output).unwrap(), expected);
assert_eq!(String::from_utf8(output).unwrap(), format!("<p>{}</p>", expected));
}
}
}

0 comments on commit 4fbbbfc

Please sign in to comment.