Skip to main content

LineIndex

Struct LineIndex 

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

Precomputed line, character, and UTF-16 offset tables for a source string.

Byte offsets passed to LineIndex::line_col and LineIndex::char_line_col are clamped to the source length, and LineIndex::utf16_range clamps ranges to source bounds. LineIndex::utf16_col is deliberately fallible for line/column pairs and returns CoordinateRangeError when the pair is outside this source.

Implementations§

Source§

impl LineIndex

Source

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

Builds line and column lookup tables for source.

Source

pub const fn source_len_bytes(&self) -> ByteOffset

Source length in bytes, represented as the EOF byte offset.

Source

pub fn line_col(&self, offset: TextSize) -> ByteLineCol

Maps a byte offset to a 1-based line and byte column, clamping past EOF.

Source

pub fn char_line_col(&self, offset: TextSize) -> CharLineCol

Maps a byte offset to a 1-based line and Unicode scalar column.

Non-UTF-8-boundary offsets snap to the previous character boundary.

Source

pub fn utf16_col( &self, line: LineNumber, byte_col: ByteColumn, ) -> Result<Utf16Offset, CoordinateRangeError>

Returns the UTF-16 code-unit offset from the start of line to byte_col.

Both coordinates must be within this source. Zero cannot be represented by LineNumber or ByteColumn, and this method returns a typed error instead of clamping lines or columns past the source.

§Errors

Returns CoordinateRangeError when line is past the source line count or byte_col is past the end column for that line.

Source

pub fn utf16_range(&self, range: TextRange) -> Utf16Range

Maps a byte range to UTF-16 start/end offsets, clamping to source bounds.

Trait Implementations§

Source§

impl Clone for LineIndex

Source§

fn clone(&self) -> LineIndex

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for LineIndex

Source§

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

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

impl Eq for LineIndex

Source§

impl PartialEq for LineIndex

Source§

fn eq(&self, other: &LineIndex) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for LineIndex

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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

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.