| 136 | } |
| 137 | |
| 138 | fn leaves( |
| 139 | &mut self, |
| 140 | ops: &[LeafOp], |
| 141 | range: Option<(ChangePosition, ChangePosition)>, |
| 142 | ) -> Result<()> { |
| 143 | // Match whole authored fragments, not each token against the whole blob. |
| 144 | // Old/deleted text absent from this change goes into a lossless literal pool. |
| 145 | let joined: Vec<u8> = ops |
| 146 | .iter() |
| 147 | .filter_map(LeafOp::content) |
| 148 | .flatten() |
| 149 | .copied() |
| 150 | .collect(); |
| 151 | let hinted = range.and_then(|(a, b)| { |
| 152 | let a = usize::try_from(a.get()).ok()?; |
| 153 | let b = usize::try_from(b.get()).ok()?; |
| 154 | (self.content.get(a..b)? == joined).then_some(a) |
| 155 | }); |
| 156 | let sequential = self |
| 157 | .content |
| 158 | .get(self.s.content_cursor..) |
| 159 | .filter(|rest| rest.starts_with(&joined)) |
| 160 | .map(|_| self.s.content_cursor); |
| 161 | let mut offset = hinted |
| 162 | .or(sequential) |
| 163 | .or_else(|| self.lines.get(joined.as_slice()).copied()); |
| 164 | put(&mut self.c.control, ops.len() as u64); |
| 165 | for op in ops { |
| 166 | match op { |
| 167 | LeafOp::Insert { |
| 168 | after, |
| 169 | kind, |
| 170 | content, |
| 171 | } => { |
| 172 | put(&mut self.c.control, 0); |
| 173 | put(&mut self.c.control, u64::from(after.is_some())); |
| 174 | if let Some(after) = after { |
| 175 | self.leaf(*after); |
| 176 | } |
| 177 | self.c.kinds.extend(postcard::to_allocvec(kind)?); |
| 178 | self.bytes(content, offset); |
| 179 | } |
| 180 | LeafOp::Delete { leaf } => { |
| 181 | put(&mut self.c.control, 1); |
| 182 | self.leaf(*leaf); |
| 183 | } |
| 184 | LeafOp::Replace { leaf, new_content } => { |
| 185 | put(&mut self.c.control, 2); |
| 186 | self.leaf(*leaf); |
| 187 | self.bytes(new_content, offset); |
| 188 | } |
| 189 | LeafOp::Restore { leaf } => { |
| 190 | put(&mut self.c.control, 3); |
| 191 | self.leaf(*leaf); |
| 192 | } |
| 193 | } |
| 194 | if let (Some(at), Some(bytes)) = (&mut offset, op.content()) { |
| 195 | *at += bytes.len(); |