Simple calculate lines and columns of str index
Use LF (0x0A) to split newline, also compatible with CRLF (0x0D 0x0A)
Newline char line number is current line
For index and char_index:
- When index exceeds the string length, return string length
- When
columnexceeds the newline char, return the index of the newline char - When
columnby zero, return previous char index
NOTE: This crate is lightweight and simple calculations. For high-performance and frequent calculations, please use line-index crate.
Examples
Byte index to line number:
use line_column;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Character index to line number:
use char_line_column;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Line number to byte index:
use index;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!; // byte index
assert_eq!;
assert_eq!;
Line number to character index:
use char_index;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The end of string is considered a character:
use *;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Features
span: Out of the boxSpanfor storing source code and text range.sync:Spanuses sync structs