# Crate lineindex

Expand description

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"));``````

## Structs

• An index value is a tuple of the byte index and character index for a character in the string.
• This type holds a reference to an string and an index of both the byte and character ranges for lines within the string. Both of these values are immutable, a change to the original string will require construction of a new indexed string.
• This is a simplified version of `std::ops::RangeInclusive` where each end of the range is an `Index` structure.