pub trait LineSpanExt {
    // Required methods
    fn line_spans(&self) -> LineSpanIter<'_> ;
    fn find_line_start(&self, index: usize) -> usize;
    fn find_line_end(&self, index: usize) -> usize;
    fn find_line_range(&self, index: usize) -> Range<usize>;
    fn find_next_line_start(&self, index: usize) -> Option<usize>;
    fn find_next_line_end(&self, index: usize) -> Option<usize>;
    fn find_next_line_range(&self, index: usize) -> Option<Range<usize>>;
    fn find_prev_line_start(&self, index: usize) -> Option<usize>;
    fn find_prev_line_end(&self, index: usize) -> Option<usize>;
    fn find_prev_line_range(&self, index: usize) -> Option<Range<usize>>;
}
Expand description

Trait implementing utility methods for finding the start, end, and range of lines from a byte index. Further also being able to find the next and previous line, from an arbitrary byte index.

Required Methods§

source

fn line_spans(&self) -> LineSpanIter<'_>

Creates a LineSpanIter.

Example
use line_span::LineSpanExt;

let text = "foo\nbar\r\nbaz";

for span in text.line_spans() {
    println!(
        "{:>2?}: {:?} {:?} {:?}",
        span.range(),
        span.as_str(),
        span.as_str_with_ending(),
        span.ending_str(),
    );
}

This will output the following:

(Manually aligned for better readability)

0.. 3: "foo" "foo\n"   "\n"
4.. 7: "bar" "bar\r\n" "\r\n"
9..12: "baz" "baz"     ""
source

fn find_line_start(&self, index: usize) -> usize

Find the start (byte index) of the line, which index is within.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
let i = 5; // 'a'

let start = text.find_line_start(i);

assert_eq!(start, 4);
assert_eq!(&text[start..], "bar\nbaz");
source

fn find_line_end(&self, index: usize) -> usize

Find the end (byte index) of the line, which index is within.

Note the end is the last character, excluding \n and \r\n.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
let i = 5; // 'a'

let end = text.find_line_end(i);

assert_eq!(end, 7);
assert_eq!(&text[..end], "foo\nbar");
source

fn find_line_range(&self, index: usize) -> Range<usize>

Find the start and end (byte index) of the line, which index is within.

Note the end is the last character, excluding \n and \r\n.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//                ^
let i = 5; // 'a'

let range = text.find_line_range(i);
assert_eq!(range, 4..7);

let line = &text[range];
assert_eq!(line, "bar");
source

fn find_next_line_start(&self, index: usize) -> Option<usize>

Find the start (byte index) of the next line, the line after the one which index is within.

Returns None if there is no next line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//           ^
let i = 1; // 'o'

let start = text.find_next_line_start(i).unwrap();

assert_eq!(start, 4);
assert_eq!(&text[start..], "bar\nbaz");
source

fn find_next_line_end(&self, index: usize) -> Option<usize>

Find the end (byte index) of the next line, the line after the one which index is within.

Note the end is the last character, excluding \n and \r\n.

Returns None if there is no next line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//           ^
let i = 1; // 'o'

let end = text.find_next_line_end(i).unwrap();

assert_eq!(end, 7);
assert_eq!(&text[..end], "foo\nbar");
source

fn find_next_line_range(&self, index: usize) -> Option<Range<usize>>

Find the start and end (byte index) of the next line, the line after the one which index is within.

Note the end is the last character, excluding \n and \r\n.

Returns None if there is no next line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//           ^
let i = 1; // 'o'

let range = text.find_next_line_range(i).unwrap();
assert_eq!(range, 4..7);

let line = &text[range];
assert_eq!(line, "bar");
source

fn find_prev_line_start(&self, index: usize) -> Option<usize>

Find the start (byte index) of the previous line, the line before the one which index is within.

Returns None if there is no previous line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//                     ^
let i = 9; // 'a'

let start = text.find_prev_line_start(i).unwrap();

assert_eq!(start, 4);
assert_eq!(&text[start..], "bar\nbaz");
source

fn find_prev_line_end(&self, index: usize) -> Option<usize>

Find the end (byte index) of the previous line, the line before the one which index is within.

Note the end is the last character, excluding \n and \r\n.

Returns None if there is no previous line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//                     ^
let i = 9; // 'a'

let end = text.find_prev_line_end(i).unwrap();

assert_eq!(end, 7);
assert_eq!(&text[..end], "foo\nbar");
source

fn find_prev_line_range(&self, index: usize) -> Option<Range<usize>>

Find the start and end (byte index) of the previous line, the line before the one which index is within.

Note the end is the last character, excluding \n and \r\n.

Returns None if there is no previous line.

Panics

Panics if index is out of bounds.

Example
let text = "foo\nbar\nbaz";
//                     ^
let i = 9; // 'a'

let range = text.find_prev_line_range(i).unwrap();
assert_eq!(range, 4..7);

let line = &text[range];
assert_eq!(line, "bar");

Implementations on Foreign Types§

source§

impl LineSpanExt for str

Implementors§