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

#336 in Text processing

Download history 639/week @ 2024-01-02 537/week @ 2024-01-09 282/week @ 2024-01-16 428/week @ 2024-01-23 589/week @ 2024-01-30 413/week @ 2024-02-06 641/week @ 2024-02-13 375/week @ 2024-02-20 445/week @ 2024-02-27 682/week @ 2024-03-05 478/week @ 2024-03-12 1251/week @ 2024-03-19 462/week @ 2024-03-26 872/week @ 2024-04-02 395/week @ 2024-04-09 268/week @ 2024-04-16

2,165 downloads per month
Used in 3 crates

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