docs/generated/contracts.mdgenerated from code
Reference · 1 min read
lines
Contracts: lines.
Read as
Source: crates/lvcs-doc/src/lines.rs.
1/// Names the code points `start..end` of `text`.2///3/// A range runs from the line holding `start` to the line holding4/// `end - 1`. An empty range names the line5/// holding the character after it, at the text's end the last line, and in6/// an empty text line 1. `None` when the range ends past the text or7/// before it starts.8#[must_use]9pub fn name(text: &str, start: usize, end: usize) -> Option<LineName> {10 let chars: Vec<char> = text.chars().collect();11 name_chars(&chars, &mut Numbering::new(&chars), start, end)12}1314/// Names `spans` of `text` (code points) as a reader is shown them.15///16/// They are sorted, with spans that touch or overlap (`a.end >= b.start`) joined, and each17/// joined run named as [`name`] does. A run that ends past the text is left18/// out.19#[must_use]20pub fn names(text: &str, spans: &[Range<usize>]) -> Vec<LineName> {21 let chars: Vec<char> = text.chars().collect();22 let mut sorted = spans.to_vec();23 sorted.sort_unstable_by_key(|span| (span.start, span.end));24 let mut runs: Vec<Range<usize>> = Vec::with_capacity(sorted.len());25 for span in sorted {26 match runs.last_mut() {27 Some(run) if run.end >= span.start => run.end = run.end.max(span.end),28 _ => runs.push(span),29 }30 }31 // The runs are in order, so their line numbers come from one pass.32 let mut numbering = Numbering::new(&chars);33 runs.into_iter()34 .filter_map(|run| name_chars(&chars, &mut numbering, run.start, run.end))35 .collect()36}