#numbers #quickly #project

line-numbers

Find line numbers in strings by byte offsets, quickly

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

#228 in Text processing

Download history 242/week @ 2024-12-08 783/week @ 2024-12-15 291/week @ 2024-12-22 687/week @ 2024-12-29 507/week @ 2025-01-05 372/week @ 2025-01-12 556/week @ 2025-01-19 318/week @ 2025-01-26 456/week @ 2025-02-02 874/week @ 2025-02-09 441/week @ 2025-02-16 410/week @ 2025-02-23 334/week @ 2025-03-02 330/week @ 2025-03-09 400/week @ 2025-03-16 292/week @ 2025-03-23

1,397 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