12 releases
| new 0.2.4 | Jan 12, 2026 |
|---|---|
| 0.2.3 | Jan 11, 2026 |
| 0.2.0 | Sep 4, 2025 |
| 0.1.6 | Jun 3, 2025 |
| 0.1.1 | Nov 6, 2024 |
#69 in No standard library
628 downloads per month
Used in 2 crates
(via rowan-peg)
43KB
808 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
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::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));
Character index to line number:
use line_column::char_line_column;
assert_eq!(char_line_column("", 0), (1, 1));
assert_eq!(char_line_column("a", 0), (1, 1));
assert_eq!(char_line_column("a", 1), (1, 2));
assert_eq!(char_line_column("ab", 1), (1, 2));
assert_eq!(char_line_column("😀\n", 1), (1, 2));
assert_eq!(char_line_column("😀\n", 2), (2, 1));
assert_eq!(char_line_column("😀\n❓", 2), (2, 1));
assert_eq!(char_line_column("😀\n❓", 2), (2, 1));
Line number to byte index:
use line_column::index;
assert_eq!(index("", 1, 1), 0);
assert_eq!(index("a", 1, 1), 0);
assert_eq!(index("a", 1, 2), 1);
assert_eq!(index("a\n", 1, 2), 1);
assert_eq!(index("a\n", 2, 1), 2);
assert_eq!(index("a\nx", 2, 2), 3);
assert_eq!(index("你好\n世界", 1, 2), 3); // byte index
assert_eq!(index("你好\n世界", 1, 3), 6);
assert_eq!(index("你好\n世界", 2, 1), 7);
Line number to character index:
use line_column::char_index;
assert_eq!(char_index("", 1, 1), 0);
assert_eq!(char_index("a", 1, 1), 0);
assert_eq!(char_index("你好\n世界", 1, 2), 1);
assert_eq!(char_index("你好\n世界", 1, 3), 2);
assert_eq!(char_index("你好\n世界", 2, 1), 3);
The end of string is considered a character:
use line_column::*;
assert_eq!(index("", 1, 1), 0);
assert_eq!(index("a", 1, 2), 1);
assert_eq!(char_index("", 1, 1), 0);
assert_eq!(char_index("a", 1, 2), 1);
assert_eq!(line_column("", 0), (1, 1));
assert_eq!(line_column("a", 1), (1, 2));
assert_eq!(char_line_column("", 0), (1, 1));
assert_eq!(char_line_column("a", 1), (1, 2));