#str #line #column #parsing #lookup

line-col

Convert string slice indices into line/column numbers

4 releases

0.2.1 Aug 11, 2020
0.2.0 Jul 18, 2020
0.1.1 Jul 13, 2020
0.1.0 Jul 13, 2020

#380 in Algorithms

Download history 752/week @ 2021-10-01 765/week @ 2021-10-08 925/week @ 2021-10-15 628/week @ 2021-10-22 426/week @ 2021-10-29 410/week @ 2021-11-05 228/week @ 2021-11-12 805/week @ 2021-11-19 1059/week @ 2021-11-26 659/week @ 2021-12-03 425/week @ 2021-12-10 534/week @ 2021-12-17 839/week @ 2021-12-24 79/week @ 2021-12-31

993 downloads per month
Used in 6 crates (3 directly)

MIT license

10KB
110 lines

line-col

Crates.io version shield Docs.rs shield Crates.io license shield Crates.io downloads shield

A simple crate for calculating 1-based line/col numbers for a string slice.

Example

use line_col::LineColLookup;

let text = "One\nTwo";
let lookup = LineColLookup::new(text);
assert_eq!(lookup.get(0), (1, 1)); // 'O' (line 1, col 1)
assert_eq!(lookup.get(1), (1, 2)); // 'n' (line 1, col 2)
assert_eq!(lookup.get(2), (1, 3)); // 'e' (line 1, col 3)
assert_eq!(lookup.get(4), (2, 1)); // 'T' (line 2, col 1)
assert_eq!(lookup.get(5), (2, 2)); // 'w' (line 2, col 2)
assert_eq!(lookup.get(6), (2, 3)); // 'o' (line 2, col 3)
assert_eq!(lookup.get(7), (2, 4)); // <end> (line 2, col 4)

Column calculation methods

This crate offers two methods for calculating the column number.

The LineColLookup::get method counts the number of bytes from the start of the line (plus one). However, this does not account for grapheme clusters (e.g. complex accented characters, emoji, etc.) If you would like to calculate the column based on the number of grapheme clusters instead, enable the grapheme-clusters feature and use LineColLookup::get_by_cluster.

Changelog

0.2.1

  • LineColLookup now defers line head table generation until first lookup

0.2.0

  • Move cluster-specific LineColLookup::get implementation into its own method, LineColLookup::get_by_cluster
  • Remove unnecessary feature filters on some tests

0.1.1

  • Fixed documentation typos
  • Use extended grapheme clustering

0.1.0

Initial version

Dependencies

~120KB