Crates.io | line-numbers |
lib.rs | line-numbers |
version | 0.3.0 |
source | src |
created_at | 2023-08-06 05:36:59.22472 |
updated_at | 2023-11-19 13:01:12.262193 |
description | Find line numbers in strings by byte offsets, quickly |
homepage | |
repository | https://github.com/wilfred/line-numbers |
max_upload_size | |
id | 936938 |
size | 14,335 |
line-numbers is a Rust crate for efficiently finding the line number of a string offset.
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()]
);