comparison rust/hg-core/src/revlog/patch.rs @ 45597:d07e4656ff5a

hg-core: use `u32` instead of `i32` in `Chunk` (D8958#inline-15001 followup) Differential Revision: https://phab.mercurial-scm.org/D9102
author Antoine cezar<acezar@chwitlabs.fr>
date Mon, 28 Sep 2020 14:27:04 +0200
parents bb523bff068b
children 412a5827ad02
comparison
equal deleted inserted replaced
45596:bb523bff068b 45597:d07e4656ff5a
8 /// - a replacement when `!data.is_empty() && start < end` 8 /// - a replacement when `!data.is_empty() && start < end`
9 /// - not doing anything when `data.is_empty() && start == end` 9 /// - not doing anything when `data.is_empty() && start == end`
10 #[derive(Debug, Clone)] 10 #[derive(Debug, Clone)]
11 struct Chunk<'a> { 11 struct Chunk<'a> {
12 /// The start position of the chunk of data to replace 12 /// The start position of the chunk of data to replace
13 start: i32, 13 start: u32,
14 /// The end position of the chunk of data to replace (open end interval) 14 /// The end position of the chunk of data to replace (open end interval)
15 end: i32, 15 end: u32,
16 /// The data replacing the chunk 16 /// The data replacing the chunk
17 data: &'a [u8], 17 data: &'a [u8],
18 } 18 }
19 19
20 impl<'a> Chunk<'a> { 20 impl<'a> Chunk<'a> {
21 /// Adjusted start of the chunk to replace. 21 /// Adjusted start of the chunk to replace.
22 /// 22 ///
23 /// Offset allow to take into account the growth/shrinkage of data 23 /// Offset allow to take 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) -> i32 { 25 fn start_offseted_by(&self, offset: i32) -> u32 {
26 self.start + offset 26 let start = self.start as i32 + offset;
27 assert!(start >= 0, "negative chunk start should never happen");
28 start as u32
27 } 29 }
28 30
29 /// Adjusted end of the chunk to replace. 31 /// Adjusted end of the chunk to replace.
30 /// 32 ///
31 /// Offset allow to take into account the growth/shrinkage of data 33 /// Offset allow to take into account the growth/shrinkage of data
32 /// induced by previously applied chunks. 34 /// induced by previously applied chunks.
33 fn end_offseted_by(&self, offset: i32) -> i32 { 35 fn end_offseted_by(&self, offset: i32) -> u32 {
34 self.start_offseted_by(offset) + (self.data.len() as i32) 36 self.start_offseted_by(offset) + self.data.len() as u32
35 } 37 }
36 38
37 /// Length of the replaced chunk. 39 /// Length of the replaced chunk.
38 fn replaced_len(&self) -> i32 { 40 fn replaced_len(&self) -> u32 {
39 self.end - self.start 41 self.end - self.start
40 } 42 }
41 43
42 /// Length difference between the replacing data and the replaced data. 44 /// Length difference between the replacing data and the replaced data.
43 fn len_diff(&self) -> i32 { 45 fn len_diff(&self) -> i32 {
44 (self.data.len() as i32) - self.replaced_len() 46 self.data.len() as i32 - self.replaced_len() as i32
45 } 47 }
46 } 48 }
47 49
48 /// The delta between two revisions data. 50 /// The delta between two revisions data.
49 #[derive(Debug, Clone)] 51 #[derive(Debug, Clone)]
61 /// Create a `PatchList` from bytes. 63 /// Create a `PatchList` from bytes.
62 pub fn new(data: &'a [u8]) -> Self { 64 pub fn new(data: &'a [u8]) -> Self {
63 let mut chunks = vec![]; 65 let mut chunks = vec![];
64 let mut data = data; 66 let mut data = data;
65 while !data.is_empty() { 67 while !data.is_empty() {
66 let start = BigEndian::read_i32(&data[0..]); 68 let start = BigEndian::read_u32(&data[0..]);
67 let end = BigEndian::read_i32(&data[4..]); 69 let end = BigEndian::read_u32(&data[4..]);
68 let len = BigEndian::read_i32(&data[8..]); 70 let len = BigEndian::read_u32(&data[8..]);
69 assert!(0 <= start && start <= end && len >= 0); 71 assert!(start <= end);
70 chunks.push(Chunk { 72 chunks.push(Chunk {
71 start, 73 start,
72 end, 74 end,
73 data: &data[12..12 + (len as usize)], 75 data: &data[12..12 + (len as usize)],
74 }); 76 });
185 let how_much_to_discard = 187 let how_much_to_discard =
186 *end - first.start_offseted_by(next_offset); 188 *end - first.start_offseted_by(next_offset);
187 189
188 first.data = &first.data[(how_much_to_discard as usize)..]; 190 first.data = &first.data[(how_much_to_discard as usize)..];
189 191
190 next_offset += how_much_to_discard; 192 next_offset += how_much_to_discard as i32;
191 } 193 }
192 194
193 // Add the chunk of `other` with adjusted position. 195 // Add the chunk of `other` with adjusted position.
194 chunks.push(Chunk { 196 chunks.push(Chunk {
195 start: *start - offset, 197 start: (*start as i32 - offset) as u32,
196 end: *end - next_offset, 198 end: (*end as i32 - next_offset) as u32,
197 data, 199 data,
198 }); 200 });
199 201
200 // Go back to normal offset tracking for the next `o` chunk 202 // Go back to normal offset tracking for the next `o` chunk
201 offset = next_offset; 203 offset = next_offset;