LinePositions

Struct LinePositions 

Source
pub struct LinePositions { /* private fields */ }
Expand description

A struct for efficiently converting absolute string positions to line-relative positions.

Implementations§

Source§

impl LinePositions

Source

pub fn from_offset(&self, offset: usize) -> (LineNumber, usize)

Return the line and column corresponding to this offset. offset is measured in bytes.

§Panics

Panics if offset is out of bounds.

Examples found in repository?
examples/simple.rs (line 10)
3fn main() {
4    let s = "foo\nbar\nbaz\n";
5    let s_lines: Vec<_> = s.lines().collect();
6
7    let line_positions = LinePositions::from(s);
8
9    let offset = 5;
10    let (line_num, column) = line_positions.from_offset(offset);
11
12    println!(
13        "Offset {} is on line {} (column {}), and the text of that line is {:?}.",
14        offset,
15        line_num.display(),
16        column,
17        s_lines[line_num.as_usize()]
18    );
19}
Source

pub fn from_region( &self, region_start: usize, region_end: usize, ) -> Vec<SingleLineSpan>

Convert this region into line spans. If the region includes a newline, the vec will contain multiple items.

§Panics

Panics if region_start or region_end are out of bounds, or if region_start is greater than region_end.

Source

pub fn from_region_relative_to( &self, start: SingleLineSpan, region_start: usize, region_end: usize, ) -> Vec<SingleLineSpan>

Given a region in the current LinePositions, convert it to be relative to a start offset in a larger, enclosing string.

§Panics

Panics if region_start or region_end are out of bounds, or if region_start is greater than region_end.

Trait Implementations§

Source§

impl Debug for LinePositions

Source§

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

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

impl From<&str> for LinePositions

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.