#line #column #col #no-alloc

no-std line-column

Simple calculate lines and columns of str index

2 releases

0.1.1 Nov 6, 2024
0.1.0 Nov 5, 2024

#124 in No standard library

Download history 60/week @ 2024-10-30 190/week @ 2024-11-06

250 downloads per month

MIT license

7KB
117 lines

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

Examples

use line_column::line_column;

assert_eq!(line_column("", 0),       (1, 1));
assert_eq!(line_column("a", 0),      (1, 1));
assert_eq!(line_column("a", 1),      (1, 2));
assert_eq!(line_column("ab", 1),     (1, 2));
assert_eq!(line_column("a\n", 1),    (1, 2));
assert_eq!(line_column("a\n", 2),    (2, 1));
assert_eq!(line_column("a\nb", 2),   (2, 1));
assert_eq!(line_column("a\r\nb", 2), (1, 3));
assert_eq!(line_column("a\r\nb", 3), (2, 1));

No runtime deps