1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![deny(missing_docs)]
extern crate crypto;
#[macro_use]
extern crate log;
mod window;
mod hashing;
pub mod string_diff;
use std::collections::HashMap;
use std::io::Read;
use std::slice::Iter;
use std::fmt;
use std::mem;
use std::string::FromUtf8Error;
#[derive(Debug, PartialEq)]
pub struct BlockHashes {
hashes: HashMap<u32, Vec<(usize, [u8; 16])>>,
block_size: usize,
file_size: usize
}
#[derive(PartialEq)]
pub struct Insert {
position: usize,
data: Vec<u8>
}
#[derive(PartialEq)]
pub struct Delete {
position: usize,
len: usize
}
#[derive(Debug, PartialEq)]
pub struct Diff {
inserts: Vec<Insert>,
deletes: Vec<Delete>
}
struct Window<R: Read> {
front: Vec<u8>,
back: Vec<u8>,
block_size: usize,
offset: usize,
bytes_read: usize,
reader: R
}
impl Diff {
#[inline]
pub fn new() -> Diff {
Diff {
inserts: Vec::new(),
deletes: Vec::new()
}
}
fn add_insert(&mut self, position: usize, mut data: Vec<u8>) {
if let Some(tail) = self.inserts.last_mut() {
if tail.position + tail.data.len() == position {
tail.data.append(&mut data);
return;
}
}
self.inserts.push(Insert {
position: position,
data: data
});
}
fn add_delete(&mut self, position: usize, len: usize) {
if let Some(tail) = self.deletes.last_mut() {
if tail.position == position {
tail.len += len;
return;
}
}
self.deletes.push(Delete {
position: position,
len: len
});
}
pub fn inserts(&self) -> Iter<Insert> {
self.inserts.iter()
}
pub fn deletes(&self) -> Iter<Delete> {
self.deletes.iter()
}
pub fn apply_to_string(&self, string: &str) -> Result<String, FromUtf8Error> {
let mut old_bytes = string.bytes();
let mut new_bytes = Vec::new();
let mut index = 0;
for insert in self.inserts() {
while index < insert.position {
new_bytes.push(old_bytes.next().unwrap().clone());
index += 1;
}
new_bytes.append(&mut insert.data.clone());
index += insert.data.len();
}
while let Some(byte) = old_bytes.next() {
new_bytes.push(byte);
}
let old_bytes = mem::replace(&mut new_bytes, Vec::new());
let mut old_bytes = old_bytes.into_iter();
index = 0;
for delete in self.deletes() {
while index < delete.position {
new_bytes.push(old_bytes.next().unwrap());
index += 1;
}
for _ in 0..delete.len {
old_bytes.next();
}
}
while let Some(byte) = old_bytes.next() {
new_bytes.push(byte);
}
String::from_utf8(new_bytes)
}
}
impl fmt::Debug for Insert {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Insert({}, '{}')", self.position, String::from_utf8_lossy(&self.data).replace('\r', "").replace('\n', "\\n"))
}
}
impl fmt::Debug for Delete {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Delete({}, {})", self.position, self.len)
}
}
impl Insert {
#[inline]
pub fn get_position(&self) -> usize {
self.position
}
#[inline]
pub fn get_data(&self) -> &Vec<u8> {
&self.data
}
}
impl Delete {
#[inline]
pub fn get_position(&self) -> usize {
self.position
}
#[inline]
pub fn get_length(&self) -> usize {
self.len
}
}
#[cfg(test)]
mod test {
use super::Diff;
#[test]
fn applying_diff_to_string() {
let string = "Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much. They were the last people you'd expect to be involved in anything strange or mysterious, because they just didn't hold with such nonsense.";
let mut diff = Diff::new();
diff.add_insert(2, vec![115]);
diff.add_insert(37, vec![116, 121]);
diff.add_insert(98, vec![97, 98]);
diff.add_insert(253, vec![109]);
diff.add_delete(35, 1);
diff.add_delete(181, 34);
diff.add_delete(219, 1);
let result = diff.apply_to_string(string).unwrap();
assert_eq!(result, "Mrs. and Mrs. Dursley, of number forty, Privet Drive, were proud to say that they were perfectly abnormal, thank you very much. They were the last people you'd expect to be involved, because they just didn't hold with much nonsense.".to_string());
}
}