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

Record a newline at span in the source

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

Calculate the number of lines of source seen so far.

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

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Create a Located from its inner value.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.