#[derive(Debug, Default, Clone)]
pub struct QuoteMap {
ranges: Vec<(usize, usize)>,
pub open_at_end: bool,
}
const OPENERS: [char; 4] = ['\u{201C}', '\u{201E}', '\u{00AB}', '\u{300C}'];
const CLOSERS: [char; 3] = ['\u{201D}', '\u{00BB}', '\u{300D}'];
pub fn is_quote_mark(c: char) -> bool {
c == '"' || OPENERS.contains(&c) || CLOSERS.contains(&c)
}
impl QuoteMap {
pub(super) fn ranges(&self) -> &[(usize, usize)] {
&self.ranges
}
pub fn scan(text: &str, open: bool) -> Self {
let mut ranges = Vec::new();
let mut open_from = open.then_some(0usize);
for (pos, c) in text.char_indices() {
if c == '"' {
match open_from.take() {
Some(from) => ranges.push((from, pos + 1)),
None => open_from = Some(pos),
}
} else if OPENERS.contains(&c) {
open_from.get_or_insert(pos);
} else if CLOSERS.contains(&c) {
if let Some(from) = open_from.take() {
ranges.push((from, pos + c.len_utf8()));
}
}
}
let open_at_end = open_from.is_some();
if let Some(from) = open_from {
ranges.push((from, text.len()));
}
Self {
ranges,
open_at_end,
}
}
pub fn range_at(&self, pos: usize) -> Option<(usize, usize)> {
self.ranges
.iter()
.copied()
.find(|&(a, b)| a <= pos && pos < b)
}
pub fn inside(&self, pos: usize) -> bool {
self.ranges.iter().any(|&(a, b)| a <= pos && pos < b)
}
pub fn contains_span(&self, text: &str, start: usize, end: usize) -> bool {
if end <= start {
return true;
}
let first = self.inside(start);
let last = self.inside(end - 1);
first == last && !text[start..end].chars().any(is_quote_mark)
}
}
#[cfg(test)]
mod tests {
use super::QuoteMap;
#[test]
fn marks_speech_and_carries_state_across_a_split() {
let text = "\"Marco left,\" Nora says.";
let map = QuoteMap::scan(text, false);
assert!(map.inside(2));
assert!(!map.inside(text.find("Nora").unwrap()));
assert!(!map.open_at_end);
let open = QuoteMap::scan("\"I know.", false);
assert!(open.open_at_end);
let rest = QuoteMap::scan("You don't.\" she says.", open.open_at_end);
assert!(rest.inside(0));
assert!(!rest.inside(rest_pos("she")));
}
fn rest_pos(word: &str) -> usize {
"You don't.\" she says.".find(word).unwrap()
}
#[test]
fn a_span_across_a_mark_is_not_contained() {
let text = "\"Papa's stuck at work,\" Nora says.";
let map = QuoteMap::scan(text, false);
assert!(!map.contains_span(text, 1, text.find("Nora").unwrap() + 4));
assert!(map.contains_span(text, 1, 5));
}
}