5 unstable releases

0.3.0 Nov 19, 2023
0.2.2 Aug 26, 2023
0.2.1 Aug 6, 2023
0.2.0 Aug 6, 2023
0.1.0 Aug 6, 2023

#479 in Text processing

Download history 476/week @ 2024-03-13 1316/week @ 2024-03-20 545/week @ 2024-03-27 720/week @ 2024-04-03 369/week @ 2024-04-10 328/week @ 2024-04-17 514/week @ 2024-04-24 367/week @ 2024-05-01 789/week @ 2024-05-08 749/week @ 2024-05-15 434/week @ 2024-05-22 388/week @ 2024-05-29 311/week @ 2024-06-05 313/week @ 2024-06-12 251/week @ 2024-06-19 332/week @ 2024-06-26

1,291 downloads per month
Used in 6 crates (4 directly)

MIT license

10KB
199 lines

line-numbers crates.io codecov.io

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 = line_positions.from_offset(offset);
println!(
    "Offset {} is on line {}, which has the text {:?}.",
    offset,
    line_num.display(),
    s_lines[line_num.as_usize()]
);

Similar Projects

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

lib.rs:

Efficiently find line numbers and line spans within a string.

use line_numbers::LinePositions;

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 = line_positions.from_offset(offset);
println!(
    "Offset {} is on line {}, which has the text {:?}.",
    offset,
    line_num.display(),
    s_lines[line_num.as_usize()]
);

No runtime deps