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.
This is provided via the trait [LineSpanExt]
, which is
implemented for [str
], and provides the following methods:
Add this to your Cargo.toml
:
toml
[dependencies]
line-span = "0.1"
Release notes are available in the repo at [CHANGELOG.md].
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.Utilities:
str_to_range()
] to get the range of a substring in a string.str_to_range_unchecked()
] unchecked version of [str_to_range()
].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.
Additionally, it also includes the ability to get the end including and excluding the line ending (\n
or \r\n
).
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(), span.asstrwithending(), span.endingstr(), ); } ```
This will output the following:
(Manually aligned for better readability)
text
0.. 3: "foo" "foo\n" "\n"
4.. 7: "bar" "bar\r\n" "\r\n"
9..12: "baz" "baz" ""
```rust use line_span::LineSpanExt;
let text = "foo\nbar\r\nbaz"; // ^ let i = 5; // 'a' in "bar"
let currrange = text.findlinerange(i); let nextrange = text.findnextlinerange(i).unwrap(); let prevrange = text.findprevline_range(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"); ```
Use [str_to_range
] (or [str_to_range_unchecked
]) to get the
range of a substring in a string.
```rust let string1 = "Foo Bar Baz"; let string2 = "Hello World";
let substring = &string1[4..7]; // "Bar"
// Returns Some
as substring
is a part of string1
asserteq!(strto_range(string1, substring), Some(4..7));
// Returns None
as substring
is not a part of string2
asserteq!(strto_range(string2, substring), None);
```