pub struct LinePositions { /* private fields */ }Expand description
A struct for efficiently converting absolute string positions to line-relative positions.
Implementations§
Source§impl LinePositions
impl LinePositions
Sourcepub fn from_offset(&self, offset: usize) -> (LineNumber, usize)
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}Sourcepub fn from_region(
&self,
region_start: usize,
region_end: usize,
) -> Vec<SingleLineSpan>
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.
Sourcepub fn from_region_relative_to(
&self,
start: SingleLineSpan,
region_start: usize,
region_end: usize,
) -> Vec<SingleLineSpan>
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
impl Debug for LinePositions
Auto Trait Implementations§
impl Freeze for LinePositions
impl RefUnwindSafe for LinePositions
impl Send for LinePositions
impl Sync for LinePositions
impl Unpin for LinePositions
impl UnwindSafe for LinePositions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more