pub struct LineCol {
pub line: usize,
pub line_start: usize,
}Expand description
Tracks a zero-based line index and the byte offset where that line starts.
§Examples
use adze_linecol_core::LineCol;
let lc = LineCol::at_position(b"hello\nworld", 8);
assert_eq!(lc.line, 1);
assert_eq!(lc.column(8), 2);Fields§
§line: usizeZero-based line index.
line_start: usizeByte offset for the start of the current line.
Implementations§
Source§impl LineCol
impl LineCol
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new tracker at line 0, byte offset 0.
§Examples
use adze_linecol_core::LineCol;
let lc = LineCol::new();
assert_eq!(lc.line, 0);
assert_eq!(lc.line_start, 0);Sourcepub fn at_position(input: &[u8], position: usize) -> Self
pub fn at_position(input: &[u8], position: usize) -> Self
Compute line metadata for a byte position in input.
If position is beyond input.len(), the end of input is used.
§Examples
use adze_linecol_core::LineCol;
let lc = LineCol::at_position(b"hello\nworld\n", 6);
assert_eq!(lc.line, 1);
assert_eq!(lc.line_start, 6);
assert_eq!(lc.column(8), 2);Sourcepub fn advance_line(&mut self, new_line_start: usize)
pub fn advance_line(&mut self, new_line_start: usize)
Advance to a new line, setting the new line’s starting byte offset.
§Examples
use adze_linecol_core::LineCol;
let mut lc = LineCol::new();
lc.advance_line(5);
assert_eq!(lc.line, 1);
assert_eq!(lc.line_start, 5);Sourcepub fn process_byte(
&mut self,
byte: u8,
next_byte: Option<u8>,
current_offset: usize,
) -> bool
pub fn process_byte( &mut self, byte: u8, next_byte: Option<u8>, current_offset: usize, ) -> bool
Process one byte while scanning a stream and update line metadata.
Returns true if the byte advanced to a new line.
Note: for CRLF, this returns false for the CR byte and true for the LF byte.
§Examples
use adze_linecol_core::LineCol;
let mut lc = LineCol::new();
assert!(!lc.process_byte(b'a', None, 0));
assert!(lc.process_byte(b'\n', None, 1));
assert_eq!(lc.line, 1);
assert_eq!(lc.line_start, 2);Trait Implementations§
impl Copy for LineCol
impl Eq for LineCol
impl StructuralPartialEq for LineCol
Auto Trait Implementations§
impl Freeze for LineCol
impl RefUnwindSafe for LineCol
impl Send for LineCol
impl Sync for LineCol
impl Unpin for LineCol
impl UnsafeUnpin for LineCol
impl UnwindSafe for LineCol
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more