#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TexRange {
pub start: usize,
pub end: usize,
}
impl TexRange {
pub fn single(offset: usize) -> Self {
Self {
start: offset,
end: offset.saturating_add(1),
}
}
pub fn span(start: usize, end: usize) -> Self {
Self { start, end }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TexPosition {
pub line: u32,
pub character: u32,
}
pub fn offset_to_position(text: &str, offset: usize) -> TexPosition {
let offset = offset.min(text.len());
let mut line = 0u32;
let mut character = 0u32;
let mut current = 0usize;
for ch in text.chars() {
if current >= offset {
break;
}
if ch == '\n' {
line += 1;
character = 0;
} else {
character += 1;
}
current += ch.len_utf8();
}
TexPosition { line, character }
}
pub fn range_to_positions(text: &str, range: TexRange) -> (TexPosition, TexPosition) {
(
offset_to_position(text, range.start),
offset_to_position(text, range.end),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn offset_at_start() {
let pos = offset_to_position("abc\ndef", 0);
assert_eq!(pos.line, 0);
assert_eq!(pos.character, 0);
}
#[test]
fn offset_after_newline() {
let text = "abc\ndef";
let pos = offset_to_position(text, 4);
assert_eq!(pos.line, 1);
assert_eq!(pos.character, 0);
}
#[test]
fn offset_past_end_clamps() {
let pos = offset_to_position("hi", 99);
assert_eq!(pos.line, 0);
assert_eq!(pos.character, 2);
}
}