Skip to content

Commit

Permalink
cleanup typing for special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 17, 2021
1 parent db6e12c commit 9aef43f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
22 changes: 10 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ impl<'a> Default for Options<'a> {
}

impl<'a> Options<'a> {
pub fn special_characters(&self) -> Vec<u8> {
let mut special_chacacters = vec!['#', '\\', '_', '<', '>', '|', '[', ']'];
special_chacacters.extend(self.code_block_token.chars());
special_chacacters.extend(self.list_token.chars());
special_chacacters.extend(self.emphasis_token.chars());
special_chacacters.extend(self.strong_token.chars());

special_chacacters.into_iter().map(|c| c as u8).collect()
pub fn special_characters(&self) -> String {
let mut special_characters = String::from("#\\_<>|[]");
special_characters.push_str(self.code_block_token);
special_characters.push_str(self.list_token);
special_characters.push_str(self.emphasis_token);
special_characters.push_str(self.strong_token);
special_characters
}
}

Expand Down Expand Up @@ -164,12 +163,11 @@ where
return Cow::Borrowed(t);
}

use std::convert::TryFrom;
let first = t.as_bytes()[0];
if options.special_characters().contains(&first) {
let first = t.chars().next().expect("at least one char");
if options.special_characters().contains(first) {
let mut s = String::with_capacity(t.len() + 1);
s.push('\\');
s.push(char::try_from(first as u32).expect("we know it's valid utf8"));
s.push(first);
s.push_str(&t[1..]);
Cow::Owned(s)
} else {
Expand Down
5 changes: 1 addition & 4 deletions tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,7 @@ mod escapes {
use crate::{fmts, CmarkToCmarkOptions, Event, Parser, Tag};

fn run_test_on_each_special_char(f: impl Fn(String, CowStr)) {
use std::convert::TryFrom;
let special_charcters = CmarkToCmarkOptions::default().special_characters();
for c in special_charcters.iter() {
let c = char::try_from(*c as u32).unwrap();
for c in CmarkToCmarkOptions::default().special_characters().chars() {
let s = format!(r#"\{special}"#, special = c);
f(s, c.to_string().into())
}
Expand Down

0 comments on commit 9aef43f

Please sign in to comment.