| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use std::sync::Arc; |
| 8 |
|
| 9 |
#[derive(Default, PartialEq, Eq, Hash)] |
| 10 |
struct Inner { |
| 11 |
|
| 12 |
buf: Arc<Vec<u8>>, |
| 13 |
|
| 14 |
ends: Ends, |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#[derive(PartialEq, Eq, Hash)] |
| 22 |
enum Ends { |
| 23 |
Small(Vec<u32>), |
| 24 |
Large(Vec<u64>), |
| 25 |
} |
| 26 |
|
| 27 |
impl Default for Ends { |
| 28 |
fn default() -> Self { |
| 29 |
Ends::Small(Vec::new()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl Ends { |
| 34 |
fn from_u64(ends: Vec<u64>, buf_len: usize) -> Ends { |
| 35 |
if u32::try_from(buf_len).is_ok() { |
| 36 |
Ends::Small(ends.into_iter().map(|e| e as u32).collect()) |
| 37 |
} else { |
| 38 |
Ends::Large(ends) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
fn len(&self) -> usize { |
| 43 |
match self { |
| 44 |
Ends::Small(v) => v.len(), |
| 45 |
Ends::Large(v) => v.len(), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
fn is_empty(&self) -> bool { |
| 50 |
self.len() == 0 |
| 51 |
} |
| 52 |
|
| 53 |
fn get(&self, i: usize) -> Option<usize> { |
| 54 |
match self { |
| 55 |
Ends::Small(v) => v.get(i).map(|&e| e as usize), |
| 56 |
Ends::Large(v) => v.get(i).map(|&e| e as usize), |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
#[derive(Clone, Default, PartialEq, Eq, Hash)] |
| 63 |
pub struct Lines(Arc<Inner>); |
| 64 |
|
| 65 |
impl Lines { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub fn split(content: &[u8]) -> Lines { |
| 70 |
Lines::split_shared(Arc::new(content.to_vec())) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
pub fn split_shared(buf: Arc<Vec<u8>>) -> Lines { |
| 76 |
let content = &buf[..]; |
| 77 |
|
| 78 |
let newlines = memchr::memchr_iter(b'\n', content).count(); |
| 79 |
let unterminated = content.last().is_some_and(|&c| c != b'\n'); |
| 80 |
let n = newlines + usize::from(unterminated); |
| 81 |
let ends_at = memchr::memchr_iter(b'\n', content) |
| 82 |
.map(|i| i + 1) |
| 83 |
.chain(unterminated.then_some(content.len())); |
| 84 |
let ends = if u32::try_from(content.len()).is_ok() { |
| 85 |
let mut v = Vec::with_capacity(n); |
| 86 |
v.extend(ends_at.map(|e| e as u32)); |
| 87 |
Ends::Small(v) |
| 88 |
} else { |
| 89 |
let mut v = Vec::with_capacity(n); |
| 90 |
v.extend(ends_at.map(|e| e as u64)); |
| 91 |
Ends::Large(v) |
| 92 |
}; |
| 93 |
Lines(Arc::new(Inner { buf, ends })) |
| 94 |
} |
| 95 |
|
| 96 |
pub fn len(&self) -> usize { |
| 97 |
self.0.ends.len() |
| 98 |
} |
| 99 |
|
| 100 |
pub fn is_empty(&self) -> bool { |
| 101 |
self.0.ends.is_empty() |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
pub fn get(&self, i: usize) -> Option<&[u8]> { |
| 106 |
let end = self.0.ends.get(i)?; |
| 107 |
let start = if i == 0 { 0 } else { self.0.ends.get(i - 1)? }; |
| 108 |
Some(&self.0.buf[start..end]) |
| 109 |
} |
| 110 |
|
| 111 |
pub fn iter(&self) -> impl ExactSizeIterator<Item = &[u8]> + Clone + '_ { |
| 112 |
(0..self.len()).map(move |i| self.get(i).unwrap()) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
pub fn bytes(&self) -> &[u8] { |
| 117 |
&self.0.buf |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
pub fn is_split_form(&self) -> bool { |
| 125 |
let buf = &self.0.buf; |
| 126 |
let mut start = 0usize; |
| 127 |
for i in 0..self.0.ends.len() { |
| 128 |
let end = self.0.ends.get(i).unwrap_or(start); |
| 129 |
let line = &buf[start..end]; |
| 130 |
if line.is_empty() { |
| 131 |
return false; |
| 132 |
} |
| 133 |
let last = i + 1 == self.0.ends.len(); |
| 134 |
let body = if last && !line.ends_with(b"\n") { |
| 135 |
line |
| 136 |
} else if line.ends_with(b"\n") { |
| 137 |
&line[..line.len() - 1] |
| 138 |
} else { |
| 139 |
return false; |
| 140 |
}; |
| 141 |
if body.contains(&b'\n') { |
| 142 |
return false; |
| 143 |
} |
| 144 |
start = end; |
| 145 |
} |
| 146 |
true |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
pub fn slice(&self, range: std::ops::Range<usize>) -> Lines { |
| 151 |
self.iter().skip(range.start).take(range.len()).collect() |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
pub fn to_vec(&self) -> Vec<Vec<u8>> { |
| 156 |
self.iter().map(<[u8]>::to_vec).collect() |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl std::ops::Index<usize> for Lines { |
| 161 |
type Output = [u8]; |
| 162 |
fn index(&self, i: usize) -> &[u8] { |
| 163 |
self.get(i).expect("line index out of range") |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl<'a> FromIterator<&'a [u8]> for Lines { |
| 168 |
fn from_iter<I: IntoIterator<Item = &'a [u8]>>(iter: I) -> Lines { |
| 169 |
let (mut buf, mut ends) = (Vec::new(), Vec::new()); |
| 170 |
for l in iter { |
| 171 |
buf.extend_from_slice(l); |
| 172 |
ends.push(buf.len() as u64); |
| 173 |
} |
| 174 |
let ends = Ends::from_u64(ends, buf.len()); |
| 175 |
Lines(Arc::new(Inner { |
| 176 |
buf: Arc::new(buf), |
| 177 |
ends, |
| 178 |
})) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl FromIterator<Vec<u8>> for Lines { |
| 183 |
fn from_iter<I: IntoIterator<Item = Vec<u8>>>(iter: I) -> Lines { |
| 184 |
let (mut buf, mut ends) = (Vec::new(), Vec::new()); |
| 185 |
for l in iter { |
| 186 |
buf.extend_from_slice(&l); |
| 187 |
ends.push(buf.len() as u64); |
| 188 |
} |
| 189 |
let ends = Ends::from_u64(ends, buf.len()); |
| 190 |
Lines(Arc::new(Inner { |
| 191 |
buf: Arc::new(buf), |
| 192 |
ends, |
| 193 |
})) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
impl From<Vec<Vec<u8>>> for Lines { |
| 198 |
fn from(v: Vec<Vec<u8>>) -> Lines { |
| 199 |
v.into_iter().collect() |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl std::fmt::Debug for Lines { |
| 204 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 205 |
f.debug_list() |
| 206 |
.entries(self.iter().map(String::from_utf8_lossy)) |
| 207 |
.finish() |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 213 |
pub struct LineRef { |
| 214 |
lines: Lines, |
| 215 |
index: u32, |
| 216 |
} |
| 217 |
|
| 218 |
impl LineRef { |
| 219 |
pub fn new(lines: Lines, index: u32) -> LineRef { |
| 220 |
LineRef { lines, index } |
| 221 |
} |
| 222 |
|
| 223 |
pub fn bytes(&self) -> &[u8] { |
| 224 |
&self.lines[self.index as usize] |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
#[cfg(test)] |
| 229 |
mod tests { |
| 230 |
use super::*; |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn lines_split_index_and_join_as_the_line_rule_says() { |
| 234 |
for text in [&b""[..], b"a\n", b"a\nb", b"a\n\nb\n", b"\n", b"no end"] { |
| 235 |
let l = Lines::split(text); |
| 236 |
let want = crate::diff::split_lines(text); |
| 237 |
assert_eq!(l.len(), want.len(), "{text:?}"); |
| 238 |
assert_eq!(l.iter().collect::<Vec<_>>(), want); |
| 239 |
assert_eq!(l.bytes(), text); |
| 240 |
let v: Lines = want.iter().copied().collect(); |
| 241 |
assert_eq!(v, l); |
| 242 |
assert_eq!(Lines::from(l.to_vec()), l); |
| 243 |
} |
| 244 |
let l = Lines::split(b"a\nb\nc\n"); |
| 245 |
assert!(l.is_split_form()); |
| 246 |
assert!(!Lines::from(vec![b"a".to_vec(), b"b\n".to_vec()]).is_split_form()); |
| 247 |
assert!(!Lines::from(vec![b"a\nb\n".to_vec()]).is_split_form()); |
| 248 |
assert!(!Lines::from(vec![Vec::new()]).is_split_form()); |
| 249 |
assert!(Lines::from(vec![b"a\n".to_vec(), b"end".to_vec()]).is_split_form()); |
| 250 |
assert_eq!(&l[1], b"b\n"); |
| 251 |
assert_eq!( |
| 252 |
l.slice(1..3).to_vec(), |
| 253 |
vec![b"b\n".to_vec(), b"c\n".to_vec()] |
| 254 |
); |
| 255 |
assert_eq!(LineRef::new(l.clone(), 2).bytes(), b"c\n"); |
| 256 |
} |
| 257 |
} |