line-span

Build Status Latest Version Docs License

This crate features utilities 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.

Current Line:

Next Line:

Previous Line:

[LineSpan] and [LineSpanIter]

The crate includes the [LineSpanIter] iterator. It is functionally equivalent to [str::lines], with the addition that it includes the ability to get the start and end byte indices of each line.

An [LineSpanIter] can be created by calling line_spans, implemented in the [LineSpans] trait. The crate implements the [LineSpans] trait for [str] and [String].

Note, [LineSpan] implements [Deref] to [&str], so in general, swapping [lines] to [line_spans] would not cause many issues.

```rust use line_span::LineSpans;

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

for span in text.linespans() { println!("{:>2?}: {:?}", span.range(), span.asstr()); } ```

This will output the following:

text 0.. 3: "foo" 4.. 7: "bar" 9..12: "baz"

Current Line, Previous Line, and Next Line

```rust use linespan::{findlinerange, findnextlinerange, findprevline_range};

let text = "foo\nbar\r\nbaz"; // ^ let i = 5; // 'a' in "bar"

let currrange = findlinerange(text, i); let nextrange = findnextlinerange(text, i).unwrap(); let prevrange = findprevline_range(text, i).unwrap();

asserteq!(currrange, 4..7); asserteq!(&text[currrange], "bar");

asserteq!(prevrange, 0..3); asserteq!(&text[prevrange], "foo");

asserteq!(nextrange, 9..12); asserteq!(&text[nextrange], "baz"); ```