2 releases
0.1.1 | Jun 24, 2023 |
---|---|
0.1.0 | Jun 24, 2023 |
14KB
219 lines
Crate lineindex
Simple line-indexed string.
Example
TBD
Changes
Version: 0.1.1
- Added an implementation of
AsRef<str>
forIndexedString
. - Added
as_str()
method toIndexedString
. - Added
as_bytes()
method toIndexedString
.
Version: 0.1.0
Initial version.
lib.rs
:
A simple line-indexed string.
Rather than destructively breaking a string into lines, this structure will allow create a vector of byte/character ranges each of which describes a line in the string original string.
Example
Given the following simple string,
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
-----------------------------------------------------------
a a  b b b  c c c c  d d
We get the following set of line index ranges.
| Row | < Byte | < Char | > Byte | > Char | String | |======|========|========|========|========|=========| | 0 | 0 | 0 | 2 | 2 | "aa" | | 1 | 3 | 3 | 6 | 6 | "bbb" | | 2 | 7 | 7 | 11 | 11 | "cccc" | | 3 | 12 | 12 | 13 | 13 | "dd" |
This set of ranges can be used to determine which line a character is on as well as returning the indices for a line or even the text of a line.
use lineindex::IndexedString;
let indexed = IndexedString::from("aa\nbbb\ncccc\ndd");
assert_eq!(indexed.lines(), 4);
assert_eq!(indexed.line_for_byte(4), Some(1));
assert_eq!(indexed.line_for_character(5), Some(1));
assert_eq!(indexed.byte_range_for_line(1), Some(3..=6));
assert_eq!(indexed.character_range_for_line(2), Some(7..=11));
assert_eq!(indexed.line_str(0), Some("aa\n"));