Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/revlog/patch.rs @ 45600:b68b19104d16
hg-core: renaming of `Chunk` offset methods (D8958#inline-15002 followup)
Differential Revision: https://phab.mercurial-scm.org/D9105
author | Antoine cezar<acezar@chwitlabs.fr> |
---|---|
date | Mon, 28 Sep 2020 14:33:52 +0200 |
parents | cee7a8e37e9c |
children | e01e84e5e426 |
comparison
equal
deleted
inserted
replaced
45599:cee7a8e37e9c | 45600:b68b19104d16 |
---|---|
20 impl Chunk<'_> { | 20 impl Chunk<'_> { |
21 /// Adjusted start of the chunk to replace. | 21 /// Adjusted start of the chunk to replace. |
22 /// | 22 /// |
23 /// The offset, taking into account the growth/shrinkage of data | 23 /// The offset, taking into account the growth/shrinkage of data |
24 /// induced by previously applied chunks. | 24 /// induced by previously applied chunks. |
25 fn start_offseted_by(&self, offset: i32) -> u32 { | 25 fn start_offset_by(&self, offset: i32) -> u32 { |
26 let start = self.start as i32 + offset; | 26 let start = self.start as i32 + offset; |
27 assert!(start >= 0, "negative chunk start should never happen"); | 27 assert!(start >= 0, "negative chunk start should never happen"); |
28 start as u32 | 28 start as u32 |
29 } | 29 } |
30 | 30 |
31 /// Adjusted end of the chunk to replace. | 31 /// Adjusted end of the chunk to replace. |
32 /// | 32 /// |
33 /// The offset, taking into account the growth/shrinkage of data | 33 /// The offset, taking into account the growth/shrinkage of data |
34 /// induced by previously applied chunks. | 34 /// induced by previously applied chunks. |
35 fn end_offseted_by(&self, offset: i32) -> u32 { | 35 fn end_offset_by(&self, offset: i32) -> u32 { |
36 self.start_offseted_by(offset) + self.data.len() as u32 | 36 self.start_offset_by(offset) + self.data.len() as u32 |
37 } | 37 } |
38 | 38 |
39 /// Length of the replaced chunk. | 39 /// Length of the replaced chunk. |
40 fn replaced_len(&self) -> u32 { | 40 fn replaced_len(&self) -> u32 { |
41 self.end - self.start | 41 self.end - self.start |
120 // until they start after the end of the current chunk. | 120 // until they start after the end of the current chunk. |
121 for Chunk { start, end, data } in other.chunks.iter() { | 121 for Chunk { start, end, data } in other.chunks.iter() { |
122 // Add chunks of `self` that start before this chunk of `other` | 122 // Add chunks of `self` that start before this chunk of `other` |
123 // without overlap. | 123 // without overlap. |
124 while pos < self.chunks.len() | 124 while pos < self.chunks.len() |
125 && self.chunks[pos].end_offseted_by(offset) <= *start | 125 && self.chunks[pos].end_offset_by(offset) <= *start |
126 { | 126 { |
127 let first = self.chunks[pos].clone(); | 127 let first = self.chunks[pos].clone(); |
128 offset += first.len_diff(); | 128 offset += first.len_diff(); |
129 chunks.push(first); | 129 chunks.push(first); |
130 pos += 1; | 130 pos += 1; |
133 // The current chunk of `self` starts before this chunk of `other` | 133 // The current chunk of `self` starts before this chunk of `other` |
134 // with overlap. | 134 // with overlap. |
135 // The left-most part of data is added as an insertion chunk. | 135 // The left-most part of data is added as an insertion chunk. |
136 // The right-most part data is kept in the chunk. | 136 // The right-most part data is kept in the chunk. |
137 if pos < self.chunks.len() | 137 if pos < self.chunks.len() |
138 && self.chunks[pos].start_offseted_by(offset) < *start | 138 && self.chunks[pos].start_offset_by(offset) < *start |
139 { | 139 { |
140 let first = &mut self.chunks[pos]; | 140 let first = &mut self.chunks[pos]; |
141 | 141 |
142 let (data_left, data_right) = first.data.split_at( | 142 let (data_left, data_right) = first.data.split_at( |
143 (*start - first.start_offseted_by(offset)) as usize, | 143 (*start - first.start_offset_by(offset)) as usize, |
144 ); | 144 ); |
145 let left = Chunk { | 145 let left = Chunk { |
146 start: first.start, | 146 start: first.start, |
147 end: first.start, | 147 end: first.start, |
148 data: data_left, | 148 data: data_left, |
168 let mut next_offset = offset; | 168 let mut next_offset = offset; |
169 | 169 |
170 // Discard the chunks of `self` that are totally overridden | 170 // Discard the chunks of `self` that are totally overridden |
171 // by the current chunk of `other` | 171 // by the current chunk of `other` |
172 while pos < self.chunks.len() | 172 while pos < self.chunks.len() |
173 && self.chunks[pos].end_offseted_by(next_offset) <= *end | 173 && self.chunks[pos].end_offset_by(next_offset) <= *end |
174 { | 174 { |
175 let first = &self.chunks[pos]; | 175 let first = &self.chunks[pos]; |
176 next_offset += first.len_diff(); | 176 next_offset += first.len_diff(); |
177 pos += 1; | 177 pos += 1; |
178 } | 178 } |
179 | 179 |
180 // Truncate the left-most part of chunk of `self` that overlaps | 180 // Truncate the left-most part of chunk of `self` that overlaps |
181 // the current chunk of `other`. | 181 // the current chunk of `other`. |
182 if pos < self.chunks.len() | 182 if pos < self.chunks.len() |
183 && self.chunks[pos].start_offseted_by(next_offset) < *end | 183 && self.chunks[pos].start_offset_by(next_offset) < *end |
184 { | 184 { |
185 let first = &mut self.chunks[pos]; | 185 let first = &mut self.chunks[pos]; |
186 | 186 |
187 let how_much_to_discard = | 187 let how_much_to_discard = |
188 *end - first.start_offseted_by(next_offset); | 188 *end - first.start_offset_by(next_offset); |
189 | 189 |
190 first.data = &first.data[(how_much_to_discard as usize)..]; | 190 first.data = &first.data[(how_much_to_discard as usize)..]; |
191 | 191 |
192 next_offset += how_much_to_discard as i32; | 192 next_offset += how_much_to_discard as i32; |
193 } | 193 } |