pub struct LineOffsetTracker { /* private fields */ }Expand description
Keeps track of source offsets of newlines for the purposes of later calculating line and column information
§Example
use partiql_source_map::location::{ByteOffset, LineAndCharPosition};
use partiql_source_map::line_offset_tracker::{LineOffsetError, LineOffsetTracker};
let source = "12345\n789012345\n789012345\n789012345";
let mut tracker = LineOffsetTracker::default();
tracker.record(6.into());
tracker.record(16.into());
tracker.record(26.into());
// We added 3 newlines, so there should be 4 lines of source
assert_eq!(tracker.num_lines(), 4);
assert_eq!(tracker.at(source, ByteOffset(0).into()), Ok(LineAndCharPosition::new(0,0)));
assert_eq!(tracker.at(source, ByteOffset(6).into()), Ok(LineAndCharPosition::new(1,0)));
assert_eq!(tracker.at(source, ByteOffset(30).into()), Ok(LineAndCharPosition::new(3,4)));
assert_eq!(tracker.at(source, ByteOffset(300).into()), Err(LineOffsetError::EndOfInput));Implementations§
Source§impl LineOffsetTracker
impl LineOffsetTracker
Sourcepub fn record(&mut self, line_start: ByteOffset)
pub fn record(&mut self, line_start: ByteOffset)
Record a newline at span in the source
Sourcepub fn append(&mut self, other: &LineOffsetTracker, offset: ByteOffset)
pub fn append(&mut self, other: &LineOffsetTracker, offset: ByteOffset)
Append the line starts from another LineOffsetTracker to this one, adding offset to each.
Sourcepub fn at(
&self,
source: &str,
BytePosition: BytePosition,
) -> Result<LineAndCharPosition, LineOffsetError>
pub fn at( &self, source: &str, BytePosition: BytePosition, ) -> Result<LineAndCharPosition, LineOffsetError>
Calculates a LineAndCharPosition for a byte offset from the given &str
source is source &str into which the byte offset applies
offset is the byte offset for which to find the LineAndCharPosition
If offset is larger than source.len(), then LineOffsetError::EndOfInput is returned
If offset is in the middle of a unicode codepoint, then LineOffsetError::InsideUnicodeCodepoint is returned
Trait Implementations§
Source§impl Clone for LineOffsetTracker
impl Clone for LineOffsetTracker
Source§fn clone(&self) -> LineOffsetTracker
fn clone(&self) -> LineOffsetTracker
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LineOffsetTracker
impl Debug for LineOffsetTracker
Auto Trait Implementations§
impl Freeze for LineOffsetTracker
impl RefUnwindSafe for LineOffsetTracker
impl Send for LineOffsetTracker
impl Sync for LineOffsetTracker
impl Unpin for LineOffsetTracker
impl UnwindSafe for LineOffsetTracker
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