Skip to content

Commit

Permalink
Refactoring smart_quoter
Browse files Browse the repository at this point in the history
  • Loading branch information
gulshan committed Nov 23, 2023
1 parent 1a672fc commit 47466fa
Showing 1 changed file with 18 additions and 38 deletions.
56 changes: 18 additions & 38 deletions src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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", "’”"));
}
}

0 comments on commit 47466fa

Please sign in to comment.