line-numbers-0.3.0/.cargo_vcs_info.json0000644000000001360000000000100134340ustar { "git": { "sha1": "02da1b03a247ef0f27dcd86ba5aa44e54f86f0e7" }, "path_in_vcs": "" }line-numbers-0.3.0/.github/workflows/coverage.yml000064400000000000000000000011441046102023000201370ustar 00000000000000name: Coverage on: [pull_request, push] jobs: coverage: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Install Rust run: rustup toolchain install stable --component llvm-tools-preview - name: Install cargo-llvm-cov uses: taiki-e/install-action@cargo-llvm-cov - name: Generate code coverage run: cargo +stable llvm-cov --all-features --workspace --lcov --output-path lcov.info - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: files: lcov.info fail_ci_if_error: false line-numbers-0.3.0/.github/workflows/release.yml000064400000000000000000000004121046102023000177610ustar 00000000000000name: Release on: push: tags: - "[0-9]+.*" jobs: push_crates_io: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - uses: katyo/publish-crates@v1 with: registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }} line-numbers-0.3.0/.github/workflows/test.yml000064400000000000000000000017571046102023000173350ustar 00000000000000on: [push, pull_request] name: Continuous integration jobs: test_linux: name: Test runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.58.0 - run: cargo test test_mac: name: Test macOS runs-on: macos-latest steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.58.0 - run: cargo test test_windows: name: Test Windows runs-on: windows-latest steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.58.0 - run: cargo test package: name: Check Linux Packaging runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.58.0 - run: cargo package --allow-dirty fmt: name: Rustfmt runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@1.58.0 - run: rustup component add rustfmt - run: cargo fmt --all -- --check line-numbers-0.3.0/.gitignore000064400000000000000000000000241046102023000142100ustar 00000000000000/target /Cargo.lock line-numbers-0.3.0/CHANGELOG.md000064400000000000000000000011571046102023000140410ustar 00000000000000# 0.3.0 (unreleased) Breaking change: Renamed `from_offsets` to `from_region`, and `from_offsets_relative_to` to `from_region_relative_to`. # 0.2.2 (released 26th August 2023) Documented panic behaviour and improved panic messages. # 0.2.1 (released 6th August 2023) Fixed explanations in the README. # 0.2.0 (released 6th August 2023) Replaced `LineNumber::one_indexed()` with `LineNumber::display()`, as one-indexed lines are only really for human consumption. Added function `NewlinePositions::from_offset()`. Renamed `NewlinePositions` to `LinePositions`. # 0.1.0 (released 5th August 2023) Initial release. line-numbers-0.3.0/Cargo.lock0000644000000002340000000000100114060ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "line-numbers" version = "0.3.0" line-numbers-0.3.0/Cargo.toml0000644000000014610000000000100114340ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.58.0" name = "line-numbers" version = "0.3.0" authors = ["Wilfred Hughes "] description = "Find line numbers in strings by byte offsets, quickly" readme = "README.md" categories = ["text-processing"] license = "MIT" repository = "https://github.com/wilfred/line-numbers" [dependencies] line-numbers-0.3.0/Cargo.toml.orig000064400000000000000000000005111046102023000151100ustar 00000000000000[package] name = "line-numbers" description = "Find line numbers in strings by byte offsets, quickly" version = "0.3.0" edition = "2021" repository = "https://github.com/wilfred/line-numbers" license = "MIT" authors = ["Wilfred Hughes "] rust-version = "1.58.0" categories = ["text-processing"] [dependencies] line-numbers-0.3.0/LICENSE000064400000000000000000000020571046102023000132350ustar 00000000000000MIT License Copyright (c) 2023 Wilfred Hughes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. line-numbers-0.3.0/README.md000064400000000000000000000017401046102023000135050ustar 00000000000000# 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. ```rust 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](https://crates.io/crates/line-span) solves a similar problem, but scans the whole string every time. line-numbers-0.3.0/examples/simple.rs000064400000000000000000000006371046102023000157070ustar 00000000000000use line_numbers::LinePositions; fn main() { 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()] ); } line-numbers-0.3.0/justfile000064400000000000000000000004071046102023000137750ustar 00000000000000default: @just --list release: #!/bin/bash set -ex VERSION=$(cargo metadata --format-version=1 | jq -r '.packages | .[] | select(.name == "line-numbers") | .version') git tag $VERSION git push --tags cargo set-version --bump patch line-numbers-0.3.0/src/lib.rs000064400000000000000000000167661046102023000141470ustar 00000000000000//! Efficiently find line numbers and line spans within a string. //! //! ```rust //! 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()] //! ); //! ``` // The `from_offset*` methods on NewlinePositions are sensible names, // and the docs clippy cites: // https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv // don't actually have an opinion on `from_foo` names. #![allow(clippy::wrong_self_convention)] use std::cmp::Ordering; use std::fmt; /// A distinct number type for line numbers, to prevent confusion with /// other numerical data. /// /// Zero-indexed internally. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct LineNumber(pub u32); impl LineNumber { pub fn display(self) -> String { format!("{}", self.0 + 1) } pub fn as_usize(self) -> usize { self.0 as usize } } impl fmt::Debug for LineNumber { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "LineNumber: {} (zero-indexed: {})", self.display(), self.0 ) } } impl From for LineNumber { fn from(number: u32) -> Self { Self(number) } } /// A range within a single line of a string. #[derive(Debug, PartialEq, Clone, Copy, Eq, PartialOrd, Ord, Hash)] pub struct SingleLineSpan { /// All zero-indexed. pub line: LineNumber, pub start_col: u32, pub end_col: u32, } /// A struct for efficiently converting absolute string positions to /// line-relative positions. #[derive(Debug)] pub struct LinePositions { /// A vector of the start and end positions of all the lines in a /// string. Positions include the newline character itself. positions: Vec<(usize, usize)>, } impl From<&str> for LinePositions { fn from(s: &str) -> Self { let mut line_start = 0; let mut positions = vec![]; for line in s.split('\n') { let line_end = line_start + line.len() + "\n".len(); // TODO: this assumes lines terminate with \n, not \r\n. positions.push((line_start, line_end - 1)); line_start = line_end; } LinePositions { positions } } } impl LinePositions { /// Return the line number containing this `offset`. /// /// # Panics /// /// Panics if `offset` is out of bounds. pub fn from_offset(&self, offset: usize) -> LineNumber { if let Some((_, s_end)) = self.positions.last() { assert!( offset <= *s_end, "Offset {} is out of bounds for a string of length {}", offset, s_end ); } let idx = self.positions.binary_search_by(|(line_start, line_end)| { if *line_end < offset { return Ordering::Less; } if *line_start > offset { return Ordering::Greater; } Ordering::Equal }); LineNumber::from(idx.expect("line should be present") as u32) } /// Convert this region into line spans. If the region includes a /// newline, the vec will contain multiple items. /// /// # Panics /// /// Panics if `region_start` or `region_end` are out of bounds. pub fn from_region(&self, region_start: usize, region_end: usize) -> Vec { assert!(region_start <= region_end); let first_idx = self.from_offset(region_start); let last_idx = self.from_offset(region_end); let mut res = vec![]; for idx in first_idx.0..=last_idx.0 { let (line_start, line_end) = self.positions[idx as usize]; res.push(SingleLineSpan { line: idx.into(), start_col: if line_start > region_start { 0 } else { region_start - line_start } as u32, end_col: if region_end < line_end { region_end - line_start } else { line_end - line_start } as u32, }); } res } pub fn from_region_relative_to( &self, start: SingleLineSpan, region_start: usize, region_end: usize, ) -> Vec { assert!(region_start <= region_end); let mut res = vec![]; for pos in self.from_region(region_start, region_end) { if pos.line.0 == 0 { res.push(SingleLineSpan { line: start.line, start_col: start.start_col + pos.start_col, end_col: start.start_col + pos.end_col, }); } else { res.push(SingleLineSpan { line: (start.line.0 + pos.line.0).into(), start_col: pos.start_col, end_col: pos.end_col, }); } } res } } #[cfg(test)] mod tests { use super::*; #[test] fn test_display_one_indexed() { let ln = LineNumber(0); assert_eq!(ln.display(), "1"); } #[test] fn from_region_first_line() { let newline_positions: LinePositions = "foo".into(); let line_spans = newline_positions.from_region(1, 3); assert_eq!( line_spans, vec![SingleLineSpan { line: 0.into(), start_col: 1, end_col: 3 }] ); } #[test] fn from_region_first_char() { let newline_positions: LinePositions = "foo".into(); let line_spans = newline_positions.from_region(0, 0); assert_eq!( line_spans, vec![SingleLineSpan { line: 0.into(), start_col: 0, end_col: 0 }] ); } #[test] fn from_region_split_over_multiple_lines() { let newline_positions: LinePositions = "foo\nbar\nbaz\naaaaaaaaaaa".into(); let line_spans = newline_positions.from_region(5, 10); assert_eq!( line_spans, vec![ SingleLineSpan { line: 1.into(), start_col: 1, end_col: 3 }, SingleLineSpan { line: 2.into(), start_col: 0, end_col: 2 } ] ); } #[test] fn from_region_relative_to() { let newline_positions: LinePositions = "foo\nbar".into(); let pos = SingleLineSpan { line: 1.into(), start_col: 1, end_col: 1, }; let line_spans = newline_positions.from_region_relative_to(pos, 1, 2); assert_eq!( line_spans, vec![SingleLineSpan { line: 1.into(), start_col: 2, end_col: 3 }] ); } #[test] #[should_panic(expected = "out of bounds for a string")] fn test_from_offset_out_of_bounds() { let newline_positions: LinePositions = "foo".into(); let _ = newline_positions.from_offset(4); } }