use super::{Hit, Position};
struct Cursor {
line: usize,
column: usize,
}
impl Cursor {
const fn start() -> Self {
Self { line: 1, column: 1 }
}
fn advance(&mut self, character: char) {
if character == '\n' {
self.line = self.line.saturating_add(1);
self.column = 1;
} else {
self.column = self.column.saturating_add(1);
}
}
const fn position(&self, byte: usize) -> Position {
Position {
line: self.line,
column: self.column,
byte,
}
}
}
pub(crate) fn located(text: &str) -> impl Iterator<Item = Hit> + '_ {
text.char_indices()
.scan(Cursor::start(), |cursor, (byte, character)| {
let position = cursor.position(byte);
cursor.advance(character);
Some(Hit {
position,
character,
})
})
}
#[must_use]
pub fn scan_str(text: &str, allowed: impl Fn(char) -> bool) -> Vec<Hit> {
located(text)
.filter(|hit| !allowed(hit.character))
.collect()
}
#[cfg(test)]
mod tests {
use super::{located, scan_str};
use crate::scan::{Hit, Position};
const ZWSP: char = '\u{200b}';
fn ascii_only(character: char) -> bool {
character.is_ascii()
}
fn hit(line: usize, column: usize, byte: usize, found: char) -> Hit {
Hit {
position: Position { line, column, byte },
character: found,
}
}
#[test]
fn column_counts_characters_not_bytes() {
let hits = scan_str("\u{00e9}\u{200b}", ascii_only);
let expected = vec![hit(1, 1, 0, '\u{00e9}'), hit(1, 2, 2, ZWSP)];
assert_eq!(hits, expected);
}
#[test]
fn carriage_return_line_feed_starts_one_new_line() {
let hits = scan_str("a\r\n\u{200b}b", ascii_only);
assert_eq!(hits, vec![hit(2, 1, 3, ZWSP)]);
}
#[test]
fn hits_arrive_in_byte_order_within_one_text() {
let hits = scan_str("\u{200b}a\u{00a0}", ascii_only);
assert_eq!(hits, vec![hit(1, 1, 0, ZWSP), hit(1, 3, 4, '\u{00a0}')]);
}
#[test]
fn the_walk_yields_every_character_with_its_position() {
let walked: Vec<Hit> = located("a\r\n\u{e9}").collect();
let expected = vec![
hit(1, 1, 0, 'a'),
hit(1, 2, 1, '\r'),
hit(1, 3, 2, '\n'),
hit(2, 1, 3, '\u{e9}'),
];
assert_eq!(walked, expected);
assert_eq!(located("").count(), 0);
}
#[test]
fn the_test_is_the_callers_and_nothing_is_reported_without_it() {
let hits = scan_str("\u{200b}\u{00a0}", |_| true);
assert_eq!(hits, Vec::new());
}
}