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