chore: clippy warnings
This commit is contained in:
@@ -8,7 +8,7 @@ pub struct LineNumbers {
|
||||
impl LineNumbers {
|
||||
pub fn new(src: &str) -> Self {
|
||||
Self {
|
||||
length: src.len() as usize,
|
||||
length: src.len(),
|
||||
line_starts: std::iter::once(0)
|
||||
.chain(src.match_indices('\n').map(|(i, _)| i + 1))
|
||||
.collect(),
|
||||
@@ -19,20 +19,14 @@ impl LineNumbers {
|
||||
pub fn line_number(&self, byte_index: usize) -> usize {
|
||||
self.line_starts
|
||||
.binary_search(&byte_index)
|
||||
.unwrap_or_else(|next_line| next_line - 1) as usize
|
||||
.unwrap_or_else(|next_line| next_line - 1)
|
||||
+ 1
|
||||
}
|
||||
|
||||
// TODO: handle unicode characters that may be more than 1 byte in width
|
||||
pub fn line_and_column_number(&self, byte_index: usize) -> LineColumn {
|
||||
let line = self.line_number(byte_index);
|
||||
let column = byte_index
|
||||
- self
|
||||
.line_starts
|
||||
.get(line as usize - 1)
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
+ 1;
|
||||
let column = byte_index - self.line_starts.get(line - 1).copied().unwrap_or_default() + 1;
|
||||
LineColumn { line, column }
|
||||
}
|
||||
|
||||
@@ -40,7 +34,7 @@ impl LineNumbers {
|
||||
/// 0 indexed line and character to byte index
|
||||
#[allow(dead_code)]
|
||||
pub fn byte_index(&self, line: usize, character: usize) -> usize {
|
||||
match self.line_starts.get((line) as usize) {
|
||||
match self.line_starts.get(line) {
|
||||
Some(line_index) => *line_index + character,
|
||||
None => self.length,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user