https://github.com/theberkin/line-col-rs
Rust crate for calculating line/column positions within a string
https://github.com/theberkin/line-col-rs
line-counter lookup parsing rust string
Last synced: about 2 months ago
JSON representation
Rust crate for calculating line/column positions within a string
- Host: GitHub
- URL: https://github.com/theberkin/line-col-rs
- Owner: TheBerkin
- License: mit
- Created: 2020-07-13T01:13:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-11T23:27:09.000Z (almost 5 years ago)
- Last Synced: 2025-05-05T21:50:33.072Z (about 2 months ago)
- Topics: line-counter, lookup, parsing, rust, string
- Language: Rust
- Homepage: https://crates.io/crates/line-col
- Size: 11.7 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# line-col
[](https://crates.io/crates/line-col)
[](https://docs.rs/line-col)
[](https://github.com/TheBerkin/line-col-rs/blob/master/LICENSE)
A simple crate for calculating 1-based line/col numbers for a string slice.
## Example
```rust
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)); // (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