use super::config::BlocksConfig;
use crate::lang::text::is_space;
fn opens_after(prev: char) -> bool {
is_space(prev) || matches!(prev, '(' | '[' | '{' | '-' | '\u{2013}' | '\u{2014}')
}
fn is_word(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
fn is_decade_abbreviation(text: &str, at: usize, prev: Option<char>) -> bool {
if !prev.is_some_and(is_word) {
return false;
}
let mut after = text[at..].chars();
after.next();
let two_digits = after.next().is_some_and(|c| c.is_ascii_digit())
&& after.next().is_some_and(|c| c.is_ascii_digit());
two_digits && after.next() == Some('s')
}
fn opening_position(text: &str, at: usize, prev_is_content: bool, single: bool) -> bool {
let prev = text[..at].chars().next_back();
if single
&& text[at..].chars().nth(1).is_some_and(|c| c.is_ascii_digit())
&& !is_decade_abbreviation(text, at, prev)
{
return true;
}
match prev {
None => !prev_is_content,
Some(prev) => opens_after(prev),
}
}
pub(crate) fn substitution_at(
text: &str,
at: usize,
config: &BlocksConfig,
prev_is_content: bool,
) -> Option<(usize, &'static str)> {
debug_assert!(
text.is_char_boundary(at),
"the normalization scan advances by whole characters"
);
let rest = &text[at..];
if config.ligature_unicode_dashes {
if rest.starts_with("---") {
return Some((3, "\u{2014}"));
}
if rest.starts_with("--") {
return Some((2, "\u{2013}"));
}
}
if config.ligature_unicode_ellipses && rest.starts_with("...") {
return Some((3, "\u{2026}"));
}
if config.ligature_unicode_quotes {
if rest.starts_with("``") {
return Some((2, "\u{201c}"));
}
if rest.starts_with("''") {
return Some((2, "\u{201d}"));
}
if rest.starts_with('`') {
return Some((1, "\u{2018}"));
}
}
if rest.starts_with('\'') {
if config.auto_unicode_quotes {
let opening = opening_position(text, at, prev_is_content, true);
return Some((1, if opening { "\u{2018}" } else { "\u{2019}" }));
}
if config.ligature_unicode_quotes {
return Some((1, "\u{2019}"));
}
}
if config.auto_unicode_quotes && rest.starts_with('"') {
let opening = opening_position(text, at, prev_is_content, false);
return Some((1, if opening { "\u{201c}" } else { "\u{201d}" }));
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_longest_dash_wins() {
let config = BlocksConfig::default();
assert_eq!(
substitution_at("a---b", 1, &config, false),
Some((3, "\u{2014}"))
);
assert_eq!(
substitution_at("a--b", 1, &config, false),
Some((2, "\u{2013}"))
);
}
#[test]
fn a_closed_gate_leaves_its_family_alone() {
let config = BlocksConfig {
ligature_unicode_dashes: false,
..BlocksConfig::default()
};
assert_eq!(substitution_at("a--b", 1, &config, false), None);
}
#[test]
fn a_double_quote_opens_after_whitespace_and_closes_after_a_letter() {
let config = BlocksConfig::default();
assert_eq!(
substitution_at("a \"b\"", 2, &config, false),
Some((1, "\u{201c}"))
);
assert_eq!(
substitution_at("a \"b\"", 4, &config, false),
Some((1, "\u{201d}"))
);
}
#[test]
fn a_lone_apostrophe_is_decided_by_its_position_when_both_families_are_on() {
let config = BlocksConfig::default();
assert_eq!(
substitution_at("'hi'", 0, &config, false),
Some((1, "\u{2018}"))
);
assert_eq!(
substitution_at("'hi'", 3, &config, false),
Some((1, "\u{2019}"))
);
}
#[test]
fn a_lone_apostrophe_starting_a_run_after_a_content_sibling_closes() {
let config = BlocksConfig::default();
assert_eq!(
substitution_at("'s", 0, &config, true),
Some((1, "\u{2019}"))
);
}
#[test]
fn the_ligature_family_keeps_the_pairs_and_the_backtick() {
let config = BlocksConfig {
auto_unicode_quotes: false,
..BlocksConfig::default()
};
assert_eq!(
substitution_at("``a''", 0, &config, false),
Some((2, "\u{201c}"))
);
assert_eq!(
substitution_at("``a''", 3, &config, false),
Some((2, "\u{201d}"))
);
assert_eq!(
substitution_at("`a'", 0, &config, false),
Some((1, "\u{2018}"))
);
assert_eq!(
substitution_at("`a'", 2, &config, false),
Some((1, "\u{2019}"))
);
}
#[test]
fn a_quote_after_a_dash_opens() {
let config = BlocksConfig::default();
assert_eq!(substitution_at("a--'q'", 3, &config, false), Some((1, "\u{2018}")));
assert_eq!(substitution_at("a–'q'", "a–".len(), &config, false), Some((1, "\u{2018}")));
assert_eq!(substitution_at("a—'q'", "a—".len(), &config, false), Some((1, "\u{2018}")));
assert_eq!(substitution_at("a--\"q\"", 3, &config, false), Some((1, "\u{201c}")));
}
#[test]
fn a_lone_apostrophe_before_a_digit_opens() {
let config = BlocksConfig::default();
assert_eq!(substitution_at("x'9", 1, &config, false), Some((1, "\u{2018}")));
assert_eq!(substitution_at("x\"9", 1, &config, false), Some((1, "\u{201d}")));
}
#[test]
fn a_decade_abbreviation_closes_before_its_digits() {
let config = BlocksConfig::default();
assert_eq!(substitution_at("x'90s", 1, &config, false), Some((1, "\u{2019}")));
assert_eq!(substitution_at("x'900s", 1, &config, false), Some((1, "\u{2018}")));
assert_eq!(substitution_at("the '90s", 4, &config, false), Some((1, "\u{2018}")));
}
#[test]
fn a_non_ascii_character_before_the_offset_is_no_boundary_problem() {
let config = BlocksConfig::default();
let text = "é\"";
assert_eq!(
substitution_at(text, "é".len(), &config, false),
Some((1, "\u{201d}"))
);
}
}