line_col

Struct LineColLookup

Source
pub struct LineColLookup<'source> { /* private fields */ }
Expand description

Pre-cached line/column lookup table for a string slice.

Implementations§

Source§

impl<'source> LineColLookup<'source>

Source

pub fn new(src: &'source str) -> Self

Creates a new line/col lookup table. The src parameter provides the input string used to calculate lines and columns.

Internally, this scans src and caches the starting positions of all lines. This means this is an O(n) operation.

Source

pub fn get(&self, index: usize) -> (usize, usize)

Looks up the 1-based line and column numbers of the specified byte index.

Returns a tuple with the line number first, then column number.

§Example
use line_col::*;
let text = "One\nTwo";
let lookup = LineColLookup::new(text);
assert_eq!(lookup.get(0), (1, 1)); // 'O' (line 1, col 1)
assert_eq!(lookup.get(1), (1, 2)); // 'n' (line 1, col 2)
assert_eq!(lookup.get(2), (1, 3)); // 'e' (line 1, col 3)
assert_eq!(lookup.get(4), (2, 1)); // 'T' (line 2, col 1)
assert_eq!(lookup.get(5), (2, 2)); // 'w' (line 2, col 2)
assert_eq!(lookup.get(6), (2, 3)); // 'o' (line 2, col 3)
assert_eq!(lookup.get(7), (2, 4)); // <end> (line 2, col 4)
§Panics

Panics if index is greater than the length of the input &str.

§Notes

This function uses a binary search to locate the line on which index resides. This means that it runs in approximately O(log n) time.

Source

pub fn get_by_cluster(&self, index: usize) -> (usize, usize)

Looks up the 1-based line and column numbers of the specified byte index. The column number correlates to the number of grapheme clusters up to and at the specified index.

Returns a tuple with the line number first, then column number.

§Panics

Panics if index is greater than the length of the input &str.

§Notes

This function uses a binary search to locate the line on which index resides. This means that it runs in approximately O(log n) time.

Auto Trait Implementations§

§

impl<'source> !Freeze for LineColLookup<'source>

§

impl<'source> !RefUnwindSafe for LineColLookup<'source>

§

impl<'source> Send for LineColLookup<'source>

§

impl<'source> !Sync for LineColLookup<'source>

§

impl<'source> Unpin for LineColLookup<'source>

§

impl<'source> UnwindSafe for LineColLookup<'source>

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.