#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct StreamPosition {
byte : usize,
line_num : usize,
char_ofs : usize,
}
impl StreamPosition {
pub fn new() -> Self {
Self { byte:0, line_num:1, char_ofs:1 }
}
pub fn of_blc(byte:usize, line_num:usize, char_ofs:usize) -> Self {
Self { byte, line_num, char_ofs }
}
#[inline]
pub(crate) fn move_on_bytes(&mut self, n:usize) {
self.byte += n;
}
#[inline]
pub(crate) fn move_by(&mut self, n:usize, ch:char) {
self.byte += n;
match ch {
'\n' => {
self.line_num += 1;
self.char_ofs = 1;
}
_ => {
self.char_ofs += 1;
}
}
}
#[inline]
pub fn byte(&self) -> usize {
self.byte
}
#[inline]
pub fn line_position(&self) -> (usize, usize) {
(self.line_num, self.char_ofs)
}
}
impl std::fmt::Display for StreamPosition {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "line {} char {}", self.line_num, self.char_ofs)
}
}