1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//! 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<u32> 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<SingleLineSpan> {
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<SingleLineSpan> {
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);
}
}