Skip to main content

LineIndex

Struct LineIndex 

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

Line starts of one source text, to turn UTF-8 byte offsets (the unit of every crate::Span) into line/column positions in UTF-8 bytes, chars, or UTF-16 code units.

Built once in O(n); each lookup is O(log n) (binary searches over the line starts and the multi-byte characters), whatever the line length. Line breaks are \n, \r\n, and a lone \r (YAML 1.2.2 §5.4). An offset past the end, or inside a multi-byte character, has no position (None); nothing here panics.

use oneyaml_core::{ColumnUnit, LineCol, LineIndex};

let src = "a: 1\nb: \u{e9}\u{1F600}x\n";
let idx = LineIndex::new(src);
let x = src.find('x').unwrap();
assert_eq!(idx.line_col(x, ColumnUnit::Utf8), Some(LineCol::new(1, 9)));
assert_eq!(idx.line_col(x, ColumnUnit::Char), Some(LineCol::new(1, 5)));
assert_eq!(idx.line_col(x, ColumnUnit::Utf16), Some(LineCol::new(1, 6)));
assert_eq!(idx.line_col(x - 1, ColumnUnit::Char), None); // inside the emoji
assert_eq!(idx.line_col(src.len() + 1, ColumnUnit::Char), None);
// A text ending in a line break has an empty last line.
assert_eq!(idx.line_count(), 3);
assert_eq!(idx.line_start(1), Some(5));
assert_eq!(idx.line_start(3), None);

Implementations§

Source§

impl LineIndex

Source

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

Indexes src.

Source

pub fn len(&self) -> usize

The length of the indexed text, in bytes.

Source

pub fn is_empty(&self) -> bool

true for an empty text.

Source

pub fn line_count(&self) -> usize

The number of lines (a text ending in a line break has an empty last line).

Source

pub fn line_start(&self, line: usize) -> Option<usize>

The byte offset where 0-based line line starts.

Source

pub fn line_col(&self, offset: usize, unit: ColumnUnit) -> Option<LineCol>

The position of byte offset, its column counted in unit; None past the end or inside a multi-byte character. O(log n).

Source

pub fn span(&self, span: Span, unit: ColumnUnit) -> Option<(LineCol, LineCol)>

The positions of a span’s start and end (see LineIndex::line_col).

Trait Implementations§

Source§

impl Clone for LineIndex

Source§

fn clone(&self) -> Self

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 Hash for LineIndex

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for LineIndex

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.