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

source

pub fn record(&mut self, line_start: ByteOffset)

Record a newline at span in the source

source

pub fn append(&mut self, other: &LineOffsetTracker, offset: ByteOffset)

Append the line starts from another LineOffsetTracker to this one, adding offset to each.

source

pub fn num_lines(&self) -> usize

Calculate the number of lines of source seen so far.

source

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

source§

fn clone(&self) -> LineOffsetTracker

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LineOffsetTracker

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for LineOffsetTracker

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, Loc> ToLocated<Loc> for T
where Loc: Display,

source§

fn to_located<IntoLoc>(self, location: IntoLoc) -> Located<Self, Loc>
where IntoLoc: Into<Location<Loc>>,

Create a Located from its inner value.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.