[−][src]Crate line_span
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:
find_line_start
to find the start of the current line.find_line_end
to find the end of the current line.find_line_range
to find the start and end current line.
Next Line:
find_next_line_start
to find the start of the next line.find_next_line_end
to find the end of the next line.find_next_line_range
to find the start and end of the next line.
Previous Line:
find_prev_line_start
to find the start of the previous line.find_prev_line_end
to find the end of the previous line.find_prev_line_range
to find both start and end of the 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.
use line_span::LineSpans; let text = "foo\nbar\r\nbaz"; for span in text.line_spans() { println!("{:>2?}: {:?}", span.range(), span.as_str()); }
This will output the following:
0.. 3: "foo"
4.. 7: "bar"
9..12: "baz"
Current Line, Previous Line, and Next Line
use line_span::{find_line_range, find_next_line_range, find_prev_line_range}; let text = "foo\nbar\r\nbaz"; // ^ let i = 5; // 'a' in "bar" let curr_range = find_line_range(text, i); let next_range = find_next_line_range(text, i).unwrap(); let prev_range = find_prev_line_range(text, i).unwrap(); assert_eq!(curr_range, 4..7); assert_eq!(&text[curr_range], "bar"); assert_eq!(prev_range, 0..3); assert_eq!(&text[prev_range], "foo"); assert_eq!(next_range, 9..12); assert_eq!(&text[next_range], "baz");
Structs
LineSpan |
|
LineSpanIter | An iterator over |
Traits
LineSpans | Trait which implements |
Functions
find_line_end | Find the end (byte index) of the line, which |
find_line_range | Find the start and end (byte index) of the line, which |
find_line_start | Find the start (byte index) of the line, which |
find_next_line_end | Find the end (byte index) of the next line, the line after the one which |
find_next_line_range | Find the start and end (byte index) of the next line, the line after the one which |
find_next_line_start | Find the start (byte index) of the next line, the line after the one which |
find_prev_line_end | Find the end (byte index) of the previous line, the line before the one which |
find_prev_line_range | Find the start and end (byte index) of the previous line, the line before the one which |
find_prev_line_start | Find the start (byte index) of the previous line, the line before the one which |