diff --git a/src/utility.rs b/src/utility.rs index ab984cb..fb6d149 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -154,43 +154,23 @@ impl PartialEq<(&str, &str, &str)> for SplittedString<'_> { } /// Convert preceding and trailing quotation marks(', ") into their curved form(‘, ’, “, ”) aka Smart Quote. -pub(crate) fn smart_quoter(mut splitted: SplittedString) -> SplittedString { +pub(crate) fn smart_quoter(splitted: SplittedString) -> SplittedString { // If the middle part is empty, there is no need to convert. if splitted.word().is_empty() { return splitted; } // Convert preceding quotation mark(', ") into its curved form(‘, “). - let mut preceding = String::with_capacity(splitted.preceding().len() + 3); - for ch in splitted.preceding().chars() { - match ch { - '\'' => { - preceding.push_str("‘"); - } - '"' => { - preceding.push_str("“"); - } - _ => preceding.push(ch), - } - } + let preceding = splitted.preceding.replace('\'', "‘").replace('"', "“"); // Convert trailing quotation mark(', ") into its curved form(’, ”). - let mut trailing = String::with_capacity(splitted.trailing().len() + 3); - for ch in splitted.trailing.chars() { - match ch { - '\'' => { - trailing.push_str("’"); - } - '"' => { - trailing.push_str("”"); - } - _ => trailing.push(ch), - } - } + let trailing = splitted.trailing.replace('\'', "’").replace('"', "”"); - splitted.preceding = Cow::Owned(preceding); - splitted.trailing = Cow::Owned(trailing); - return splitted; + SplittedString { + preceding: preceding.into(), + word: splitted.word, + trailing: trailing.into(), + } } #[cfg(test)] @@ -241,19 +221,19 @@ mod test { #[test] fn test_smart_quoting() { - assert_eq!(smart_quoter(SplittedString::split("\"", true)), ("\"".into(), "", "".into())); + assert_eq!(smart_quoter(SplittedString::split("\"", true)), ("\"", "", "")); - assert_eq!(smart_quoter(SplittedString::split("'Till", true)), ("‘".into(), "Till", "".into())); - assert_eq!(smart_quoter(SplittedString::split("\"Hey", true)), ("“".into(), "Hey", "".into())); - assert_eq!(smart_quoter(SplittedString::split("'\"Hey", true)), ("‘“".into(), "Hey", "".into())); + assert_eq!(smart_quoter(SplittedString::split("'Till", true)), ("‘", "Till", "")); + assert_eq!(smart_quoter(SplittedString::split("\"Hey", true)), ("“", "Hey", "")); + assert_eq!(smart_quoter(SplittedString::split("'\"Hey", true)), ("‘“", "Hey", "")); - assert_eq!(smart_quoter(SplittedString::split("finished'", true)), ("".into(), "finished", "’".into())); - assert_eq!(smart_quoter(SplittedString::split("Hey\"", true)), ("".into(), "Hey", "”".into())); - assert_eq!(smart_quoter(SplittedString::split("Hey'\"", true)), ("".into(), "Hey", "’”".into())); + assert_eq!(smart_quoter(SplittedString::split("finished'", true)), ("", "finished", "’")); + assert_eq!(smart_quoter(SplittedString::split("Hey\"", true)), ("", "Hey", "”")); + assert_eq!(smart_quoter(SplittedString::split("Hey'\"", true)), ("", "Hey", "’”")); - assert_eq!(smart_quoter(SplittedString::split("'Awkward'", true)), ("‘".into(), "Awkward", "’".into())); - assert_eq!(smart_quoter(SplittedString::split("\"Nevertheless\"", true)), ("“".into(), "Nevertheless", "”".into())); + assert_eq!(smart_quoter(SplittedString::split("'Awkward'", true)), ("‘", "Awkward", "’")); + assert_eq!(smart_quoter(SplittedString::split("\"Nevertheless\"", true)), ("“", "Nevertheless", "”")); - assert_eq!(smart_quoter(SplittedString::split("\"'Quotation'\"", true)), ("“‘".into(), "Quotation", "’”".into())); + assert_eq!(smart_quoter(SplittedString::split("\"'Quotation'\"", true)), ("“‘", "Quotation", "’”")); } }