Skip to content

Rust helper library for converting file offsets into line and column numbers

License

Notifications You must be signed in to change notification settings

Wilfred/line-numbers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rust helper library for converting file offsets into line and column numbers

Resources

License

Stars

Watchers

Forks

Packages

No packages published