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 copy 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 more