diff --git a/src/lib.rs b/src/lib.rs index 44eaaf0..23c78c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,14 +96,13 @@ impl<'a> Default for Options<'a> { } impl<'a> Options<'a> { - pub fn special_characters(&self) -> Vec { - 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 } } @@ -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 { diff --git a/tests/fmt.rs b/tests/fmt.rs index 61293f5..37518cb 100644 --- a/tests/fmt.rs +++ b/tests/fmt.rs @@ -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()) }