7 unstable releases

0.4.0 Jan 20, 2025
0.3.1 Dec 31, 2024
0.3.0 Nov 19, 2023
0.2.2 Aug 26, 2023
0.1.0 Aug 6, 2023

#183 in Text processing

Download history 278/week @ 2024-11-18 409/week @ 2024-11-25 222/week @ 2024-12-02 291/week @ 2024-12-09 755/week @ 2024-12-16 268/week @ 2024-12-23 738/week @ 2024-12-30 505/week @ 2025-01-06 342/week @ 2025-01-13 578/week @ 2025-01-20 286/week @ 2025-01-27 437/week @ 2025-02-03 898/week @ 2025-02-10 489/week @ 2025-02-17 356/week @ 2025-02-24 319/week @ 2025-03-03

2,080 downloads per month
Used in 6 crates (4 directly)

MIT license

13KB
223 lines

line-numbers crates.io codecov.io docs.rs

line-numbers is a Rust crate for efficiently finding the line number of a string offset.

Usage

Create a LinePositions, then you can find line numbers for an offset.

let s = "foo\nbar\nbaz\n";
let s_lines: Vec<_> = s.lines().collect();

let line_positions = LinePositions::from(s);

let offset = 5;
let (line_num, column) = line_positions.from_offset(offset);

println!(
    "Offset {} is on line {} (column {}), and the text of that line is {:?}.",
    offset,
    line_num.display(),
    column,
    s_lines[line_num.as_usize()]
);

Similar Projects

  • line-span solves a similar problem, but scans the whole string every time.

No runtime deps