1#![deny(unsafe_code)]
7#![warn(rust_2018_idioms)]
8#![warn(missing_docs)]
9
10#[derive(Clone, Debug)]
12pub struct LineIndex {
13 line_starts: Vec<usize>,
15 text_len: usize,
17}
18
19impl LineIndex {
20 #[must_use]
22 pub fn new(text: &str) -> Self {
23 let mut line_starts = vec![0];
24 for (idx, ch) in text.char_indices() {
25 if ch == '\n' {
26 line_starts.push(idx + 1);
27 }
28 }
29 Self { line_starts, text_len: text.len() }
30 }
31
32 #[must_use]
34 pub fn byte_to_position(&self, byte: usize) -> (usize, usize) {
35 let line = self.line_starts.binary_search(&byte).unwrap_or_else(|i| i.saturating_sub(1));
36 let column = byte - self.line_starts[line];
37 (line, column)
38 }
39
40 #[must_use]
46 pub fn position_to_byte(&self, line: usize, column: usize) -> Option<usize> {
47 let start = *self.line_starts.get(line)?;
48 let line_end = self
52 .line_starts
53 .get(line + 1)
54 .map_or(self.text_len, |next_start| next_start.saturating_sub(1));
55 let max_column = line_end.saturating_sub(start);
56
57 if column > max_column {
58 return None;
59 }
60
61 Some(start + column)
62 }
63
64 #[must_use]
71 pub fn position_to_byte_checked(&self, line: usize, column: usize) -> Option<usize> {
72 let start = *self.line_starts.get(line)?;
73 let line_end = self
76 .line_starts
77 .get(line + 1)
78 .map_or(self.text_len, |next_start| next_start.saturating_sub(1));
79 let max_column = line_end.saturating_sub(start);
80
81 if column > max_column {
82 return None;
83 }
84
85 Some(start + column)
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn empty_string_has_one_line() {
95 let idx = LineIndex::new("");
96 assert_eq!(idx.byte_to_position(0), (0, 0));
97 assert_eq!(idx.position_to_byte(0, 0), Some(0));
98 assert_eq!(idx.position_to_byte(1, 0), None);
99 }
100
101 #[test]
102 fn single_line_no_newline() {
103 let idx = LineIndex::new("hello");
104 assert_eq!(idx.byte_to_position(0), (0, 0));
105 assert_eq!(idx.byte_to_position(4), (0, 4));
106 assert_eq!(idx.position_to_byte(0, 0), Some(0));
107 assert_eq!(idx.position_to_byte(0, 4), Some(4));
108 assert_eq!(idx.position_to_byte(0, 5), Some(5));
109 assert_eq!(idx.position_to_byte(0, 6), None);
110 }
111
112 #[test]
113 fn two_lines_byte_to_position() {
114 let idx = LineIndex::new("ab\ncd");
116 assert_eq!(idx.byte_to_position(0), (0, 0));
117 assert_eq!(idx.byte_to_position(1), (0, 1));
118 assert_eq!(idx.byte_to_position(2), (0, 2)); assert_eq!(idx.byte_to_position(3), (1, 0));
120 assert_eq!(idx.byte_to_position(4), (1, 1));
121 }
122
123 #[test]
124 fn two_lines_position_to_byte() {
125 let idx = LineIndex::new("ab\ncd");
126 assert_eq!(idx.position_to_byte(0, 0), Some(0));
127 assert_eq!(idx.position_to_byte(0, 2), Some(2)); assert_eq!(idx.position_to_byte(1, 0), Some(3));
129 assert_eq!(idx.position_to_byte(1, 1), Some(4));
130 assert_eq!(idx.position_to_byte(1, 2), Some(5)); assert_eq!(idx.position_to_byte(1, 3), None); assert_eq!(idx.position_to_byte(2, 0), None); }
134
135 #[test]
136 fn position_to_byte_checked_excludes_newline_as_next_line_start() {
137 let idx = LineIndex::new("ab\ncd");
139 assert_eq!(idx.position_to_byte_checked(0, 2), Some(2));
141 assert_eq!(idx.position_to_byte_checked(0, 3), None);
143 assert_eq!(idx.position_to_byte_checked(1, 0), Some(3));
144 assert_eq!(idx.position_to_byte_checked(2, 0), None);
145 }
146
147 #[test]
148 fn trailing_newline_creates_empty_last_line() {
149 let idx = LineIndex::new("foo\n");
151 assert_eq!(idx.byte_to_position(3), (0, 3)); assert_eq!(idx.byte_to_position(4), (1, 0)); assert_eq!(idx.position_to_byte(1, 0), Some(4));
154 }
155
156 #[test]
157 fn multiple_lines_roundtrip() {
158 let text = "line0\nline1\nline2";
159 let idx = LineIndex::new(text);
160 for (byte, _) in text.char_indices() {
161 let (line, col) = idx.byte_to_position(byte);
162 assert_eq!(idx.position_to_byte(line, col), Some(byte));
163 }
164 }
165}