|
| 1 | +use serde::Serialize; |
| 2 | + |
| 3 | +/// A byte offset within a text document. |
| 4 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] |
| 5 | +pub struct ByteOffset(pub u32); |
| 6 | + |
| 7 | +/// A line and column position within a text document. |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 9 | +pub struct LineCol(pub (u32, u32)); |
| 10 | + |
| 11 | +impl LineCol { |
| 12 | + #[must_use] |
| 13 | + pub fn line(&self) -> u32 { |
| 14 | + self.0 .0 |
| 15 | + } |
| 16 | + |
| 17 | + #[must_use] |
| 18 | + pub fn column(&self) -> u32 { |
| 19 | + self.0 .1 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] |
| 24 | +pub struct Span { |
| 25 | + pub start: u32, |
| 26 | + pub length: u32, |
| 27 | +} |
| 28 | + |
| 29 | +impl Span { |
| 30 | + #[must_use] |
| 31 | + pub fn new(start: u32, length: u32) -> Self { |
| 32 | + Self { start, length } |
| 33 | + } |
| 34 | + |
| 35 | + #[must_use] |
| 36 | + pub fn start_offset(&self) -> ByteOffset { |
| 37 | + ByteOffset(self.start) |
| 38 | + } |
| 39 | + |
| 40 | + #[must_use] |
| 41 | + pub fn end_offset(&self) -> ByteOffset { |
| 42 | + ByteOffset(self.start.saturating_add(self.length)) |
| 43 | + } |
| 44 | + |
| 45 | + /// Convert this span to start and end line/column positions using the given line index. |
| 46 | + #[must_use] |
| 47 | + pub fn to_line_col(&self, line_index: &LineIndex) -> (LineCol, LineCol) { |
| 48 | + let start = line_index.to_line_col(self.start_offset()); |
| 49 | + let end = line_index.to_line_col(self.end_offset()); |
| 50 | + (start, end) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 55 | +pub struct LineIndex(Vec<u32>); |
| 56 | + |
| 57 | +impl LineIndex { |
| 58 | + #[must_use] |
| 59 | + pub fn from_text(text: &str) -> Self { |
| 60 | + let mut starts = Vec::with_capacity(256); |
| 61 | + starts.push(0); |
| 62 | + |
| 63 | + let bytes = text.as_bytes(); |
| 64 | + let mut i = 0; |
| 65 | + while i < bytes.len() { |
| 66 | + match bytes[i] { |
| 67 | + b'\n' => { |
| 68 | + // LF - Unix style line ending |
| 69 | + starts.push(u32::try_from(i + 1).unwrap_or_default()); |
| 70 | + i += 1; |
| 71 | + } |
| 72 | + b'\r' => { |
| 73 | + // CR - check if followed by LF for Windows style |
| 74 | + if i + 1 < bytes.len() && bytes[i + 1] == b'\n' { |
| 75 | + // CRLF - Windows style line ending |
| 76 | + starts.push(u32::try_from(i + 2).unwrap_or_default()); |
| 77 | + i += 2; |
| 78 | + } else { |
| 79 | + // Just CR - old Mac style line ending |
| 80 | + starts.push(u32::try_from(i + 1).unwrap_or_default()); |
| 81 | + i += 1; |
| 82 | + } |
| 83 | + } |
| 84 | + _ => i += 1, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + LineIndex(starts) |
| 89 | + } |
| 90 | + |
| 91 | + #[must_use] |
| 92 | + pub fn to_line_col(&self, offset: ByteOffset) -> LineCol { |
| 93 | + if self.0.is_empty() { |
| 94 | + return LineCol((0, 0)); |
| 95 | + } |
| 96 | + |
| 97 | + let line = match self.0.binary_search(&offset.0) { |
| 98 | + Ok(exact) => exact, |
| 99 | + Err(0) => 0, |
| 100 | + Err(next) => next - 1, |
| 101 | + }; |
| 102 | + |
| 103 | + let line_start = self.0[line]; |
| 104 | + let column = offset.0.saturating_sub(line_start); |
| 105 | + |
| 106 | + LineCol((u32::try_from(line).unwrap_or_default(), column)) |
| 107 | + } |
| 108 | + |
| 109 | + #[must_use] |
| 110 | + pub fn line_start(&self, line: u32) -> Option<u32> { |
| 111 | + self.0.get(line as usize).copied() |
| 112 | + } |
| 113 | + |
| 114 | + #[must_use] |
| 115 | + pub fn lines(&self) -> &[u32] { |
| 116 | + &self.0 |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(test)] |
| 121 | +mod tests { |
| 122 | + use super::*; |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_line_index_unix_endings() { |
| 126 | + let text = "line1\nline2\nline3"; |
| 127 | + let index = LineIndex::from_text(text); |
| 128 | + assert_eq!(index.lines(), &[0, 6, 12]); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_line_index_windows_endings() { |
| 133 | + let text = "line1\r\nline2\r\nline3"; |
| 134 | + let index = LineIndex::from_text(text); |
| 135 | + // After "line1\r\n" (7 bytes), next line starts at byte 7 |
| 136 | + // After "line2\r\n" (7 bytes), next line starts at byte 14 |
| 137 | + assert_eq!(index.lines(), &[0, 7, 14]); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_line_index_mixed_endings() { |
| 142 | + let text = "line1\nline2\r\nline3\rline4"; |
| 143 | + let index = LineIndex::from_text(text); |
| 144 | + // "line1\n" -> next at 6 |
| 145 | + // "line2\r\n" -> next at 13 |
| 146 | + // "line3\r" -> next at 19 |
| 147 | + assert_eq!(index.lines(), &[0, 6, 13, 19]); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_line_index_empty() { |
| 152 | + let text = ""; |
| 153 | + let index = LineIndex::from_text(text); |
| 154 | + assert_eq!(index.lines(), &[0]); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_to_line_col_with_crlf() { |
| 159 | + let text = "hello\r\nworld"; |
| 160 | + let index = LineIndex::from_text(text); |
| 161 | + |
| 162 | + // "hello" is 5 bytes, then \r\n, so "world" starts at byte 7 |
| 163 | + assert_eq!(index.to_line_col(ByteOffset(0)), LineCol((0, 0))); |
| 164 | + assert_eq!(index.to_line_col(ByteOffset(7)), LineCol((1, 0))); |
| 165 | + assert_eq!(index.to_line_col(ByteOffset(8)), LineCol((1, 1))); |
| 166 | + } |
| 167 | +} |
0 commit comments