diff_match_patch_native 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +12 -0
- data/README.rdoc +73 -0
- data/Rakefile +28 -0
- data/ext/diff_match_patch/_dmp.cpp +287 -0
- data/ext/diff_match_patch/_dmp.h +16 -0
- data/ext/diff_match_patch/diff_match_patch.cpp +10 -0
- data/ext/diff_match_patch/extconf.rb +8 -0
- data/lib/diff_match_patch/diff_match_patch.bundle +0 -0
- data/lib/diff_match_patch.rb +18 -0
- data/src/include/diff_match_patch-stl/diff_match_patch.h +2545 -0
- data/test/test_diff_patch_match.rb +278 -0
- metadata +99 -0
@@ -0,0 +1,2545 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2008 Google Inc. All Rights Reserved.
|
3
|
+
* Author: fraser@google.com (Neil Fraser)
|
4
|
+
* Author: mikeslemmer@gmail.com (Mike Slemmer)
|
5
|
+
* Author: snhere@gmail.com (Sergey Nozhenko)
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
* Diff Match and Patch
|
20
|
+
* http://code.google.com/p/google-diff-match-patch/
|
21
|
+
*/
|
22
|
+
|
23
|
+
#ifndef DIFF_MATCH_PATCH_H
|
24
|
+
#define DIFF_MATCH_PATCH_H
|
25
|
+
|
26
|
+
#include <limits>
|
27
|
+
#include <list>
|
28
|
+
#include <map>
|
29
|
+
#include <vector>
|
30
|
+
#include <time.h>
|
31
|
+
|
32
|
+
|
33
|
+
/*
|
34
|
+
* Functions for diff, match and patch.
|
35
|
+
* Computes the difference between two texts to create a patch.
|
36
|
+
* Applies the patch onto another text, allowing for errors.
|
37
|
+
*
|
38
|
+
* @author fraser@google.com (Neil Fraser)
|
39
|
+
*
|
40
|
+
* Qt/C++ port by mikeslemmer@gmail.com (Mike Slemmer):
|
41
|
+
*
|
42
|
+
* STL-only port by snhere@gmail.com (Sergey Nozhenko)
|
43
|
+
*
|
44
|
+
* Here is a trivial sample program which works properly when linked with this
|
45
|
+
* library:
|
46
|
+
*
|
47
|
+
|
48
|
+
#include "diff_match_patch.h"
|
49
|
+
#include <string>
|
50
|
+
using namespace std;
|
51
|
+
int main(int argc, char **argv) {
|
52
|
+
diff_match_patch<wstring> dmp;
|
53
|
+
wstring str1 = L"First string in diff");
|
54
|
+
wstring str2 = L"Second string in diff");
|
55
|
+
|
56
|
+
wstring strPatch = dmp.patch_toText(dmp.patch_make(str1, str2));
|
57
|
+
pair<wstring, vector<bool> > out
|
58
|
+
= dmp.patch_apply(dmp.patch_fromText(strPatch), str1);
|
59
|
+
wstring strResult = out.first;
|
60
|
+
|
61
|
+
// here, strResult will equal str2 above.
|
62
|
+
return 0;
|
63
|
+
}
|
64
|
+
|
65
|
+
*/
|
66
|
+
|
67
|
+
// Character type dependencies
|
68
|
+
template <class char_t> struct diff_match_patch_traits {};
|
69
|
+
|
70
|
+
/**
|
71
|
+
* Class containing the diff, match and patch methods.
|
72
|
+
* Also contains the behaviour settings.
|
73
|
+
*/
|
74
|
+
template <class stringT, class traits = diff_match_patch_traits<typename stringT::value_type> >
|
75
|
+
class diff_match_patch {
|
76
|
+
public:
|
77
|
+
/**
|
78
|
+
* String and character types
|
79
|
+
*/
|
80
|
+
typedef stringT string_t;
|
81
|
+
typedef typename string_t::value_type char_t;
|
82
|
+
|
83
|
+
/**-
|
84
|
+
* The data structure representing a diff is a Linked list of Diff objects:
|
85
|
+
* {Diff(Operation.DELETE, "Hello"), Diff(Operation.INSERT, "Goodbye"),
|
86
|
+
* Diff(Operation.EQUAL, " world.")}
|
87
|
+
* which means: delete "Hello", add "Goodbye" and keep " world."
|
88
|
+
*/
|
89
|
+
enum Operation {
|
90
|
+
DELETE, INSERT, EQUAL
|
91
|
+
};
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Class representing one diff operation.
|
95
|
+
*/
|
96
|
+
class Diff {
|
97
|
+
public:
|
98
|
+
Operation operation;
|
99
|
+
// One of: INSERT, DELETE or EQUAL.
|
100
|
+
|
101
|
+
string_t text;
|
102
|
+
// The text associated with this diff operation.
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Constructor. Initializes the diff with the provided values.
|
106
|
+
* @param operation One of INSERT, DELETE or EQUAL.
|
107
|
+
* @param text The text being applied.
|
108
|
+
*/
|
109
|
+
Diff(Operation _operation, const string_t &_text) : operation(_operation), text(_text) {}
|
110
|
+
Diff() {}
|
111
|
+
|
112
|
+
/**
|
113
|
+
* Display a human-readable version of this Diff.
|
114
|
+
* @return text version.
|
115
|
+
*/
|
116
|
+
string_t toString() const {
|
117
|
+
string_t prettyText = text;
|
118
|
+
// Replace linebreaks with Pilcrow signs.
|
119
|
+
for (typename string_t::iterator i = prettyText.begin(); i != prettyText.end(); ++i)
|
120
|
+
if (traits::to_wchar(*i) == L'\n') *i = traits::from_wchar(L'\u00b6');
|
121
|
+
return traits::cs(L"Diff(") + strOperation(operation) + traits::cs(L",\"") + prettyText + traits::cs(L"\")");
|
122
|
+
}
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Is this Diff equivalent to another Diff?
|
126
|
+
* @param d Another Diff to compare against
|
127
|
+
* @return true or false
|
128
|
+
*/
|
129
|
+
bool operator==(const Diff &d) const {
|
130
|
+
return (d.operation == this->operation) && (d.text == this->text);
|
131
|
+
}
|
132
|
+
bool operator!=(const Diff &d) const { return !(operator == (d)); }
|
133
|
+
|
134
|
+
static string_t strOperation(Operation op) {
|
135
|
+
switch (op) {
|
136
|
+
case INSERT:
|
137
|
+
return traits::cs(L"INSERT");
|
138
|
+
case DELETE:
|
139
|
+
return traits::cs(L"DELETE");
|
140
|
+
case EQUAL:
|
141
|
+
return traits::cs(L"EQUAL");
|
142
|
+
}
|
143
|
+
throw string_t(traits::cs(L"Invalid operation."));
|
144
|
+
}
|
145
|
+
};
|
146
|
+
|
147
|
+
typedef std::list<Diff> Diffs;
|
148
|
+
|
149
|
+
|
150
|
+
/**
|
151
|
+
* Class representing one patch operation.
|
152
|
+
*/
|
153
|
+
class Patch {
|
154
|
+
public:
|
155
|
+
Diffs diffs;
|
156
|
+
int start1;
|
157
|
+
int start2;
|
158
|
+
int length1;
|
159
|
+
int length2;
|
160
|
+
|
161
|
+
/**
|
162
|
+
* Constructor. Initializes with an empty list of diffs.
|
163
|
+
*/
|
164
|
+
Patch() : start1(0), start2(0), length1(0), length2(0) {}
|
165
|
+
|
166
|
+
bool isNull() const {
|
167
|
+
return start1 == 0 && start2 == 0 && length1 == 0 && length2 == 0 && diffs.size() == 0;
|
168
|
+
}
|
169
|
+
|
170
|
+
/**
|
171
|
+
* Emmulate GNU diff's format.
|
172
|
+
* Header: @@ -382,8 +481,9 @@
|
173
|
+
* Indicies are printed as 1-based, not 0-based.
|
174
|
+
* @return The GNU diff string
|
175
|
+
*/
|
176
|
+
string_t toString() const {
|
177
|
+
string_t coords1, coords2;
|
178
|
+
if (length1 == 0) {
|
179
|
+
coords1 = to_string(start1) + traits::cs(L",0");
|
180
|
+
} else if (length1 == 1) {
|
181
|
+
coords1 = to_string(start1 + 1);
|
182
|
+
} else {
|
183
|
+
coords1 = to_string(start1 + 1) + traits::from_wchar(L',') + to_string(length1);
|
184
|
+
}
|
185
|
+
if (length2 == 0) {
|
186
|
+
coords2 = to_string(start2) + traits::cs(L",0");
|
187
|
+
} else if (length2 == 1) {
|
188
|
+
coords2 = to_string(start2 + 1);
|
189
|
+
} else {
|
190
|
+
coords2 = to_string(start2 + 1) + traits::from_wchar(L',') + to_string(length2);
|
191
|
+
}
|
192
|
+
string_t text(traits::cs(L"@@ -") + coords1 + traits::cs(L" +") + coords2 + traits::cs(L" @@\n"));
|
193
|
+
// Escape the body of the patch with %xx notation.
|
194
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
195
|
+
switch ((*cur_diff).operation) {
|
196
|
+
case INSERT:
|
197
|
+
text += traits::from_wchar(L'+');
|
198
|
+
break;
|
199
|
+
case DELETE:
|
200
|
+
text += traits::from_wchar(L'-');
|
201
|
+
break;
|
202
|
+
case EQUAL:
|
203
|
+
text += traits::from_wchar(L' ');
|
204
|
+
break;
|
205
|
+
}
|
206
|
+
append_percent_encoded(text, (*cur_diff).text);
|
207
|
+
text += traits::from_wchar(L'\n');
|
208
|
+
}
|
209
|
+
|
210
|
+
return text;
|
211
|
+
}
|
212
|
+
};
|
213
|
+
|
214
|
+
typedef std::list<Patch> Patches;
|
215
|
+
|
216
|
+
friend class diff_match_patch_test;
|
217
|
+
|
218
|
+
public:
|
219
|
+
// Defaults.
|
220
|
+
// Set these on your diff_match_patch instance to override the defaults.
|
221
|
+
|
222
|
+
// Number of seconds to map a diff before giving up (0 for infinity).
|
223
|
+
float Diff_Timeout;
|
224
|
+
// Cost of an empty edit operation in terms of edit characters.
|
225
|
+
short Diff_EditCost;
|
226
|
+
// At what point is no match declared (0.0 = perfection, 1.0 = very loose).
|
227
|
+
float Match_Threshold;
|
228
|
+
// How far to search for a match (0 = exact location, 1000+ = broad match).
|
229
|
+
// A match this many characters away from the expected location will add
|
230
|
+
// 1.0 to the score (0.0 is a perfect match).
|
231
|
+
int Match_Distance;
|
232
|
+
// When deleting a large block of text (over ~64 characters), how close does
|
233
|
+
// the contents have to match the expected contents. (0.0 = perfection,
|
234
|
+
// 1.0 = very loose). Note that Match_Threshold controls how closely the
|
235
|
+
// end points of a delete need to match.
|
236
|
+
float Patch_DeleteThreshold;
|
237
|
+
// Chunk size for context length.
|
238
|
+
short Patch_Margin;
|
239
|
+
|
240
|
+
// The number of bits in an int.
|
241
|
+
short Match_MaxBits;
|
242
|
+
|
243
|
+
|
244
|
+
public:
|
245
|
+
|
246
|
+
diff_match_patch() :
|
247
|
+
Diff_Timeout(1.0f),
|
248
|
+
Diff_EditCost(4),
|
249
|
+
Match_Threshold(0.5f),
|
250
|
+
Match_Distance(1000),
|
251
|
+
Patch_DeleteThreshold(0.5f),
|
252
|
+
Patch_Margin(4),
|
253
|
+
Match_MaxBits(32) {
|
254
|
+
}
|
255
|
+
|
256
|
+
// DIFF FUNCTIONS
|
257
|
+
|
258
|
+
/**
|
259
|
+
* Find the differences between two texts.
|
260
|
+
* @param text1 Old string to be diffed.
|
261
|
+
* @param text2 New string to be diffed.
|
262
|
+
* @param checklines Speedup flag. If false, then don't run a
|
263
|
+
* line-level diff first to identify the changed areas.
|
264
|
+
* If true, then run a faster slightly less optimal diff.
|
265
|
+
* Most of the time checklines is wanted, so default to true.
|
266
|
+
* @return Linked List of Diff objects.
|
267
|
+
*/
|
268
|
+
Diffs diff_main(const string_t &text1, const string_t &text2, bool checklines = true) const {
|
269
|
+
// Set a deadline by which time the diff must be complete.
|
270
|
+
clock_t deadline;
|
271
|
+
if (Diff_Timeout <= 0) {
|
272
|
+
deadline = std::numeric_limits<clock_t>::max();
|
273
|
+
} else {
|
274
|
+
deadline = clock() + (clock_t)(Diff_Timeout * CLOCKS_PER_SEC);
|
275
|
+
}
|
276
|
+
Diffs diffs;
|
277
|
+
diff_main(text1, text2, checklines, deadline, diffs);
|
278
|
+
return diffs;
|
279
|
+
}
|
280
|
+
|
281
|
+
/**
|
282
|
+
* Find the differences between two texts. Simplifies the problem by
|
283
|
+
* stripping any common prefix or suffix off the texts before diffing.
|
284
|
+
* @param text1 Old string to be diffed.
|
285
|
+
* @param text2 New string to be diffed.
|
286
|
+
* @param checklines Speedup flag. If false, then don't run a
|
287
|
+
* line-level diff first to identify the changed areas.
|
288
|
+
* If true, then run a faster slightly less optimal diff.
|
289
|
+
* @param deadline Time when the diff should be complete by. Used
|
290
|
+
* internally for recursive calls. Users should set DiffTimeout instead.
|
291
|
+
* @param Linked List of Diff objects.
|
292
|
+
*/
|
293
|
+
private:
|
294
|
+
static void diff_main(const string_t &text1, const string_t &text2, bool checklines, clock_t deadline, Diffs& diffs) {
|
295
|
+
diffs.clear();
|
296
|
+
|
297
|
+
// Check for equality (speedup).
|
298
|
+
if (text1 == text2) {
|
299
|
+
if (!text1.empty()) {
|
300
|
+
diffs.push_back(Diff(EQUAL, text1));
|
301
|
+
}
|
302
|
+
}
|
303
|
+
else {
|
304
|
+
// Trim off common prefix (speedup).
|
305
|
+
int commonlength = diff_commonPrefix(text1, text2);
|
306
|
+
const string_t &commonprefix = text1.substr(0, commonlength);
|
307
|
+
string_t textChopped1 = text1.substr(commonlength);
|
308
|
+
string_t textChopped2 = text2.substr(commonlength);
|
309
|
+
|
310
|
+
// Trim off common suffix (speedup).
|
311
|
+
commonlength = diff_commonSuffix(textChopped1, textChopped2);
|
312
|
+
const string_t &commonsuffix = right(textChopped1, commonlength);
|
313
|
+
textChopped1 = textChopped1.substr(0, textChopped1.length() - commonlength);
|
314
|
+
textChopped2 = textChopped2.substr(0, textChopped2.length() - commonlength);
|
315
|
+
|
316
|
+
// Compute the diff on the middle block.
|
317
|
+
diff_compute(textChopped1, textChopped2, checklines, deadline, diffs);
|
318
|
+
|
319
|
+
// Restore the prefix and suffix.
|
320
|
+
if (!commonprefix.empty()) {
|
321
|
+
diffs.push_front(Diff(EQUAL, commonprefix));
|
322
|
+
}
|
323
|
+
if (!commonsuffix.empty()) {
|
324
|
+
diffs.push_back(Diff(EQUAL, commonsuffix));
|
325
|
+
}
|
326
|
+
|
327
|
+
diff_cleanupMerge(diffs);
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
/**
|
332
|
+
* Find the differences between two texts. Assumes that the texts do not
|
333
|
+
* have any common prefix or suffix.
|
334
|
+
* @param text1 Old string to be diffed.
|
335
|
+
* @param text2 New string to be diffed.
|
336
|
+
* @param checklines Speedup flag. If false, then don't run a
|
337
|
+
* line-level diff first to identify the changed areas.
|
338
|
+
* If true, then run a faster slightly less optimal diff.
|
339
|
+
* @param deadline Time when the diff should be complete by.
|
340
|
+
* @param Linked List of Diff objects.
|
341
|
+
*/
|
342
|
+
private:
|
343
|
+
static void diff_compute(string_t text1, string_t text2, bool checklines, clock_t deadline, Diffs& diffs) {
|
344
|
+
if (text1.empty()) {
|
345
|
+
// Just add some text (speedup).
|
346
|
+
diffs.push_back(Diff(INSERT, text2));
|
347
|
+
return;
|
348
|
+
}
|
349
|
+
|
350
|
+
if (text2.empty()) {
|
351
|
+
// Just delete some text (speedup).
|
352
|
+
diffs.push_back(Diff(DELETE, text1));
|
353
|
+
return;
|
354
|
+
}
|
355
|
+
|
356
|
+
{
|
357
|
+
const string_t& longtext = text1.length() > text2.length() ? text1 : text2;
|
358
|
+
const string_t& shorttext = text1.length() > text2.length() ? text2 : text1;
|
359
|
+
const size_t i = longtext.find(shorttext);
|
360
|
+
if (i != string_t::npos) {
|
361
|
+
// Shorter text is inside the longer text (speedup).
|
362
|
+
const Operation op = (text1.length() > text2.length()) ? DELETE : INSERT;
|
363
|
+
diffs.push_back(Diff(op, longtext.substr(0, i)));
|
364
|
+
diffs.push_back(Diff(EQUAL, shorttext));
|
365
|
+
diffs.push_back(Diff(op, safeMid(longtext, i + shorttext.length())));
|
366
|
+
return;
|
367
|
+
}
|
368
|
+
|
369
|
+
if (shorttext.length() == 1) {
|
370
|
+
// Single character string.
|
371
|
+
// After the previous speedup, the character can't be an equality.
|
372
|
+
diffs.push_back(Diff(DELETE, text1));
|
373
|
+
diffs.push_back(Diff(INSERT, text2));
|
374
|
+
return;
|
375
|
+
}
|
376
|
+
// Garbage collect longtext and shorttext by scoping out.
|
377
|
+
}
|
378
|
+
|
379
|
+
// Don't risk returning a non-optimal diff if we have unlimited time.
|
380
|
+
if (deadline != std::numeric_limits<clock_t>::max()) {
|
381
|
+
// Check to see if the problem can be split in two.
|
382
|
+
HalfMatchResult hm;
|
383
|
+
if (diff_halfMatch(text1, text2, hm)) {
|
384
|
+
// A half-match was found, sort out the return data.
|
385
|
+
// Send both pairs off for separate processing.
|
386
|
+
diff_main(hm.text1_a, hm.text2_a, checklines, deadline, diffs);
|
387
|
+
diffs.push_back(Diff(EQUAL, hm.mid_common));
|
388
|
+
Diffs diffs_b;
|
389
|
+
diff_main(hm.text1_b, hm.text2_b, checklines, deadline, diffs_b);
|
390
|
+
diffs.splice(diffs.end(), diffs_b);
|
391
|
+
return;
|
392
|
+
}
|
393
|
+
}
|
394
|
+
|
395
|
+
// Perform a real diff.
|
396
|
+
if (checklines && text1.length() > 100 && text2.length() > 100) {
|
397
|
+
diff_lineMode(text1, text2, deadline, diffs);
|
398
|
+
return;
|
399
|
+
}
|
400
|
+
|
401
|
+
diff_bisect(text1, text2, deadline, diffs);
|
402
|
+
}
|
403
|
+
|
404
|
+
/**
|
405
|
+
* Do a quick line-level diff on both strings, then rediff the parts for
|
406
|
+
* greater accuracy.
|
407
|
+
* This speedup can produce non-minimal diffs.
|
408
|
+
* @param text1 Old string to be diffed.
|
409
|
+
* @param text2 New string to be diffed.
|
410
|
+
* @param deadline Time when the diff should be complete by.
|
411
|
+
* @param Linked List of Diff objects.
|
412
|
+
*/
|
413
|
+
private:
|
414
|
+
static void diff_lineMode(string_t text1, string_t text2, clock_t deadline, Diffs& diffs) {
|
415
|
+
// Scan the text on a line-by-line basis first.
|
416
|
+
Lines linearray;
|
417
|
+
diff_linesToChars(text1, text2, linearray);
|
418
|
+
|
419
|
+
diff_main(text1, text2, false, deadline, diffs);
|
420
|
+
|
421
|
+
// Convert the diff back to original text.
|
422
|
+
diff_charsToLines(diffs, linearray);
|
423
|
+
// Eliminate freak matches (e.g. blank lines)
|
424
|
+
diff_cleanupSemantic(diffs);
|
425
|
+
|
426
|
+
// Rediff any replacement blocks, this time character-by-character.
|
427
|
+
// Add a dummy entry at the end.
|
428
|
+
diffs.push_back(Diff(EQUAL, string_t()));
|
429
|
+
int count_delete = 0;
|
430
|
+
int count_insert = 0;
|
431
|
+
string_t text_delete;
|
432
|
+
string_t text_insert;
|
433
|
+
|
434
|
+
for (typename Diffs::iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
435
|
+
switch ((*cur_diff).operation) {
|
436
|
+
case INSERT:
|
437
|
+
count_insert++;
|
438
|
+
text_insert += (*cur_diff).text;
|
439
|
+
break;
|
440
|
+
case DELETE:
|
441
|
+
count_delete++;
|
442
|
+
text_delete += (*cur_diff).text;
|
443
|
+
break;
|
444
|
+
case EQUAL:
|
445
|
+
// Upon reaching an equality, check for prior redundancies.
|
446
|
+
if (count_delete >= 1 && count_insert >= 1) {
|
447
|
+
// Delete the offending records and add the merged ones.
|
448
|
+
typename Diffs::iterator last = cur_diff;
|
449
|
+
std::advance(cur_diff, -(count_delete + count_insert));
|
450
|
+
cur_diff = diffs.erase(cur_diff, last);
|
451
|
+
|
452
|
+
Diffs new_diffs;
|
453
|
+
diff_main(text_delete, text_insert, false, deadline, new_diffs);
|
454
|
+
diffs.splice(cur_diff++, new_diffs);
|
455
|
+
--cur_diff;
|
456
|
+
}
|
457
|
+
count_insert = 0;
|
458
|
+
count_delete = 0;
|
459
|
+
text_delete.clear();
|
460
|
+
text_insert.clear();
|
461
|
+
break;
|
462
|
+
}
|
463
|
+
}
|
464
|
+
diffs.pop_back(); // Remove the dummy entry at the end.
|
465
|
+
}
|
466
|
+
|
467
|
+
/**
|
468
|
+
* Find the 'middle snake' of a diff, split the problem in two
|
469
|
+
* and return the recursively constructed diff.
|
470
|
+
* See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
|
471
|
+
* @param text1 Old string to be diffed.
|
472
|
+
* @param text2 New string to be diffed.
|
473
|
+
* @return Linked List of Diff objects.
|
474
|
+
*/
|
475
|
+
protected:
|
476
|
+
static Diffs diff_bisect(const string_t &text1, const string_t &text2, clock_t deadline) {
|
477
|
+
Diffs diffs;
|
478
|
+
diff_bisect(text1, text2, deadline, diffs);
|
479
|
+
return diffs;
|
480
|
+
}
|
481
|
+
private:
|
482
|
+
static void diff_bisect(const string_t &text1, const string_t &text2, clock_t deadline, Diffs& diffs) {
|
483
|
+
// Cache the text lengths to prevent multiple calls.
|
484
|
+
const int text1_length = text1.length();
|
485
|
+
const int text2_length = text2.length();
|
486
|
+
const int max_d = (text1_length + text2_length + 1) / 2;
|
487
|
+
const int v_offset = max_d;
|
488
|
+
const int v_length = 2 * max_d;
|
489
|
+
std::vector<int> v1(v_length, -1),
|
490
|
+
v2(v_length, -1);
|
491
|
+
v1[v_offset + 1] = 0;
|
492
|
+
v2[v_offset + 1] = 0;
|
493
|
+
const int delta = text1_length - text2_length;
|
494
|
+
// If the total number of characters is odd, then the front path will
|
495
|
+
// collide with the reverse path.
|
496
|
+
const bool front = (delta % 2 != 0);
|
497
|
+
// Offsets for start and end of k loop.
|
498
|
+
// Prevents mapping of space beyond the grid.
|
499
|
+
int k1start = 0;
|
500
|
+
int k1end = 0;
|
501
|
+
int k2start = 0;
|
502
|
+
int k2end = 0;
|
503
|
+
for (int d = 0; d < max_d; d++) {
|
504
|
+
// Bail out if deadline is reached.
|
505
|
+
if (clock() > deadline) {
|
506
|
+
break;
|
507
|
+
}
|
508
|
+
|
509
|
+
// Walk the front path one step.
|
510
|
+
for (int k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
511
|
+
const int k1_offset = v_offset + k1;
|
512
|
+
int x1;
|
513
|
+
if (k1 == -d || (k1 != d && v1[k1_offset - 1] < v1[k1_offset + 1])) {
|
514
|
+
x1 = v1[k1_offset + 1];
|
515
|
+
} else {
|
516
|
+
x1 = v1[k1_offset - 1] + 1;
|
517
|
+
}
|
518
|
+
int y1 = x1 - k1;
|
519
|
+
while (x1 < text1_length && y1 < text2_length
|
520
|
+
&& text1[x1] == text2[y1]) {
|
521
|
+
x1++;
|
522
|
+
y1++;
|
523
|
+
}
|
524
|
+
v1[k1_offset] = x1;
|
525
|
+
if (x1 > text1_length) {
|
526
|
+
// Ran off the right of the graph.
|
527
|
+
k1end += 2;
|
528
|
+
} else if (y1 > text2_length) {
|
529
|
+
// Ran off the bottom of the graph.
|
530
|
+
k1start += 2;
|
531
|
+
} else if (front) {
|
532
|
+
int k2_offset = v_offset + delta - k1;
|
533
|
+
if (k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] != -1) {
|
534
|
+
// Mirror x2 onto top-left coordinate system.
|
535
|
+
int x2 = text1_length - v2[k2_offset];
|
536
|
+
if (x1 >= x2) {
|
537
|
+
// Overlap detected.
|
538
|
+
diff_bisectSplit(text1, text2, x1, y1, deadline, diffs);
|
539
|
+
return;
|
540
|
+
}
|
541
|
+
}
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
// Walk the reverse path one step.
|
546
|
+
for (int k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
547
|
+
const int k2_offset = v_offset + k2;
|
548
|
+
int x2;
|
549
|
+
if (k2 == -d || (k2 != d && v2[k2_offset - 1] < v2[k2_offset + 1])) {
|
550
|
+
x2 = v2[k2_offset + 1];
|
551
|
+
} else {
|
552
|
+
x2 = v2[k2_offset - 1] + 1;
|
553
|
+
}
|
554
|
+
int y2 = x2 - k2;
|
555
|
+
while (x2 < text1_length && y2 < text2_length
|
556
|
+
&& text1[text1_length - x2 - 1] == text2[text2_length - y2 - 1]) {
|
557
|
+
x2++;
|
558
|
+
y2++;
|
559
|
+
}
|
560
|
+
v2[k2_offset] = x2;
|
561
|
+
if (x2 > text1_length) {
|
562
|
+
// Ran off the left of the graph.
|
563
|
+
k2end += 2;
|
564
|
+
} else if (y2 > text2_length) {
|
565
|
+
// Ran off the top of the graph.
|
566
|
+
k2start += 2;
|
567
|
+
} else if (!front) {
|
568
|
+
int k1_offset = v_offset + delta - k2;
|
569
|
+
if (k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] != -1) {
|
570
|
+
int x1 = v1[k1_offset];
|
571
|
+
int y1 = v_offset + x1 - k1_offset;
|
572
|
+
// Mirror x2 onto top-left coordinate system.
|
573
|
+
x2 = text1_length - x2;
|
574
|
+
if (x1 >= x2) {
|
575
|
+
// Overlap detected.
|
576
|
+
diff_bisectSplit(text1, text2, x1, y1, deadline, diffs);
|
577
|
+
return;
|
578
|
+
}
|
579
|
+
}
|
580
|
+
}
|
581
|
+
}
|
582
|
+
}
|
583
|
+
// Diff took too long and hit the deadline or
|
584
|
+
// number of diffs equals number of characters, no commonality at all.
|
585
|
+
diffs.clear();
|
586
|
+
diffs.push_back(Diff(DELETE, text1));
|
587
|
+
diffs.push_back(Diff(INSERT, text2));
|
588
|
+
}
|
589
|
+
|
590
|
+
/**
|
591
|
+
* Given the location of the 'middle snake', split the diff in two parts
|
592
|
+
* and recurse.
|
593
|
+
* @param text1 Old string to be diffed.
|
594
|
+
* @param text2 New string to be diffed.
|
595
|
+
* @param x Index of split point in text1.
|
596
|
+
* @param y Index of split point in text2.
|
597
|
+
* @param deadline Time at which to bail if not yet complete.
|
598
|
+
* @return LinkedList of Diff objects.
|
599
|
+
*/
|
600
|
+
private:
|
601
|
+
static void diff_bisectSplit(const string_t &text1, const string_t &text2, int x, int y, clock_t deadline, Diffs& diffs) {
|
602
|
+
string_t text1a = text1.substr(0, x);
|
603
|
+
string_t text2a = text2.substr(0, y);
|
604
|
+
string_t text1b = safeMid(text1, x);
|
605
|
+
string_t text2b = safeMid(text2, y);
|
606
|
+
|
607
|
+
// Compute both diffs serially.
|
608
|
+
diff_main(text1a, text2a, false, deadline, diffs);
|
609
|
+
Diffs diffs_b;
|
610
|
+
diff_main(text1b, text2b, false, deadline, diffs_b);
|
611
|
+
diffs.splice(diffs.end(), diffs_b);
|
612
|
+
}
|
613
|
+
|
614
|
+
/**
|
615
|
+
* Split two texts into a list of strings. Reduce the texts to a string of
|
616
|
+
* hashes where each Unicode character represents one line.
|
617
|
+
* @param text1 First string.
|
618
|
+
* @param text2 Second string.
|
619
|
+
* @return Lines object, containing the encoded text1, the
|
620
|
+
* encoded text2 and the List of pointers to unique strings. The zeroth element
|
621
|
+
* of the List of unique strings is intentionally blank.
|
622
|
+
*/
|
623
|
+
protected:
|
624
|
+
struct LinePtr : std::pair<typename string_t::const_pointer, size_t> {
|
625
|
+
LinePtr() {}
|
626
|
+
LinePtr(typename string_t::const_pointer p, size_t n) : std::pair<typename string_t::const_pointer, size_t>(p, n) {}
|
627
|
+
bool operator<(const LinePtr& p) const
|
628
|
+
{ return this->second < p.second? true : this->second > p.second? false : string_t::traits_type::compare(this->first, p.first, this->second) < 0; }
|
629
|
+
};
|
630
|
+
struct Lines : std::vector<LinePtr> { string_t text1, text2; };
|
631
|
+
|
632
|
+
static void diff_linesToChars(string_t &text1, string_t &text2, Lines& lineArray) {
|
633
|
+
std::map<LinePtr, size_t> lineHash;
|
634
|
+
lineArray.text1.swap(text1), lineArray.text2.swap(text2);
|
635
|
+
// e.g. linearray[4] == "Hello\n"
|
636
|
+
// e.g. linehash.get("Hello\n") == 4
|
637
|
+
|
638
|
+
// "\x00" is a valid character, but various debuggers don't like it.
|
639
|
+
// So we'll insert a junk entry to avoid generating a null character.
|
640
|
+
|
641
|
+
text1 = diff_linesToCharsMunge(lineArray.text1, lineHash);
|
642
|
+
text2 = diff_linesToCharsMunge(lineArray.text2, lineHash);
|
643
|
+
|
644
|
+
lineArray.resize(lineHash.size() + 1);
|
645
|
+
for (typename std::map<LinePtr, size_t>::const_iterator i = lineHash.begin(); i != lineHash.end(); ++i)
|
646
|
+
lineArray[(*i).second] = (*i).first;
|
647
|
+
}
|
648
|
+
|
649
|
+
/**
|
650
|
+
* Split a text into a list of pointers to strings. Reduce the texts to a string of
|
651
|
+
* hashes where each Unicode character represents one line.
|
652
|
+
* @param text String to encode.
|
653
|
+
* @param lineHash Map of string pointers to indices.
|
654
|
+
* @return Encoded string.
|
655
|
+
*/
|
656
|
+
private:
|
657
|
+
static string_t diff_linesToCharsMunge(const string_t &text, std::map<LinePtr, size_t> &lineHash) {
|
658
|
+
string_t chars;
|
659
|
+
// Walk the text, pulling out a substring for each line.
|
660
|
+
// text.split('\n') would would temporarily double our memory footprint.
|
661
|
+
// Modifying text would create many large strings to garbage collect.
|
662
|
+
typename string_t::size_type lineLen;
|
663
|
+
for (typename string_t::const_pointer lineStart = text.c_str(), textEnd = lineStart + text.size(); lineStart < textEnd; lineStart += lineLen + 1) {
|
664
|
+
lineLen = next_token(text, traits::from_wchar(L'\n'), lineStart);
|
665
|
+
if (lineStart + lineLen == textEnd) --lineLen;
|
666
|
+
chars += (char_t)(*lineHash.insert(std::make_pair(LinePtr(lineStart, lineLen + 1), lineHash.size() + 1)).first).second;
|
667
|
+
}
|
668
|
+
return chars;
|
669
|
+
}
|
670
|
+
|
671
|
+
/**
|
672
|
+
* Rehydrate the text in a diff from a string of line hashes to real lines of
|
673
|
+
* text.
|
674
|
+
* @param diffs LinkedList of Diff objects.
|
675
|
+
* @param lineArray List of pointers to unique strings.
|
676
|
+
*/
|
677
|
+
private:
|
678
|
+
static void diff_charsToLines(Diffs &diffs, const Lines& lineArray) {
|
679
|
+
for (typename Diffs::iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
680
|
+
string_t text;
|
681
|
+
for (int y = 0; y < (int)(*cur_diff).text.length(); y++) {
|
682
|
+
const LinePtr& lp = lineArray[static_cast<size_t>((*cur_diff).text[y])];
|
683
|
+
text.append(lp.first, lp.second);
|
684
|
+
}
|
685
|
+
(*cur_diff).text.swap(text);
|
686
|
+
}
|
687
|
+
}
|
688
|
+
|
689
|
+
/**
|
690
|
+
* Determine the common prefix of two strings.
|
691
|
+
* @param text1 First string.
|
692
|
+
* @param text2 Second string.
|
693
|
+
* @return The number of characters common to the start of each string.
|
694
|
+
*/
|
695
|
+
public:
|
696
|
+
static int diff_commonPrefix(const string_t &text1, const string_t &text2) {
|
697
|
+
// Performance analysis: http://neil.fraser.name/news/2007/10/09/
|
698
|
+
const int n = std::min(text1.length(), text2.length());
|
699
|
+
for (int i = 0; i < n; i++) {
|
700
|
+
if (text1[i] != text2[i]) {
|
701
|
+
return i;
|
702
|
+
}
|
703
|
+
}
|
704
|
+
return n;
|
705
|
+
}
|
706
|
+
|
707
|
+
/**
|
708
|
+
* Determine the common suffix of two strings.
|
709
|
+
* @param text1 First string.
|
710
|
+
* @param text2 Second string.
|
711
|
+
* @return The number of characters common to the end of each string.
|
712
|
+
*/
|
713
|
+
public:
|
714
|
+
static int diff_commonSuffix(const string_t &text1, const string_t &text2) {
|
715
|
+
// Performance analysis: http://neil.fraser.name/news/2007/10/09/
|
716
|
+
const int text1_length = text1.length();
|
717
|
+
const int text2_length = text2.length();
|
718
|
+
const int n = std::min(text1_length, text2_length);
|
719
|
+
for (int i = 1; i <= n; i++) {
|
720
|
+
if (text1[text1_length - i] != text2[text2_length - i]) {
|
721
|
+
return i - 1;
|
722
|
+
}
|
723
|
+
}
|
724
|
+
return n;
|
725
|
+
}
|
726
|
+
|
727
|
+
/**
|
728
|
+
* Determine if the suffix of one string is the prefix of another.
|
729
|
+
* @param text1 First string.
|
730
|
+
* @param text2 Second string.
|
731
|
+
* @return The number of characters common to the end of the first
|
732
|
+
* string and the start of the second string.
|
733
|
+
*/
|
734
|
+
protected:
|
735
|
+
static int diff_commonOverlap(const string_t &text1, const string_t &text2) {
|
736
|
+
// Cache the text lengths to prevent multiple calls.
|
737
|
+
const int text1_length = text1.length();
|
738
|
+
const int text2_length = text2.length();
|
739
|
+
// Eliminate the null case.
|
740
|
+
if (text1_length == 0 || text2_length == 0) {
|
741
|
+
return 0;
|
742
|
+
}
|
743
|
+
// Truncate the longer string.
|
744
|
+
string_t text1_trunc = text1;
|
745
|
+
string_t text2_trunc = text2;
|
746
|
+
if (text1_length > text2_length) {
|
747
|
+
text1_trunc = right(text1, text2_length);
|
748
|
+
} else if (text1_length < text2_length) {
|
749
|
+
text2_trunc = text2.substr(0, text1_length);
|
750
|
+
}
|
751
|
+
const int text_length = std::min(text1_length, text2_length);
|
752
|
+
// Quick check for the worst case.
|
753
|
+
if (text1_trunc == text2_trunc) {
|
754
|
+
return text_length;
|
755
|
+
}
|
756
|
+
|
757
|
+
// Start by looking for a single character match
|
758
|
+
// and increase length until no match is found.
|
759
|
+
// Performance analysis: http://neil.fraser.name/news/2010/11/04/
|
760
|
+
int best = 0;
|
761
|
+
int length = 1;
|
762
|
+
while (true) {
|
763
|
+
string_t pattern = right(text1_trunc, length);
|
764
|
+
size_t found = text2_trunc.find(pattern);
|
765
|
+
if (found == string_t::npos) {
|
766
|
+
return best;
|
767
|
+
}
|
768
|
+
length += found;
|
769
|
+
if (found == 0 || right(text1_trunc, length) == right(text2_trunc, length)) {
|
770
|
+
best = length;
|
771
|
+
length++;
|
772
|
+
}
|
773
|
+
}
|
774
|
+
}
|
775
|
+
|
776
|
+
/**
|
777
|
+
* Do the two texts share a substring which is at least half the length of
|
778
|
+
* the longer text?
|
779
|
+
* This speedup can produce non-minimal diffs.
|
780
|
+
* @param text1 First string.
|
781
|
+
* @param text2 Second string.
|
782
|
+
* @param HalfMatchResult object, containing the prefix of text1, the
|
783
|
+
* suffix of text1, the prefix of text2, the suffix of text2 and the
|
784
|
+
* common middle.
|
785
|
+
* @return Boolean true if there was a match, false otherwise.
|
786
|
+
*/
|
787
|
+
protected:
|
788
|
+
struct HalfMatchResult {
|
789
|
+
string_t text1_a, text1_b, text2_a, text2_b, mid_common;
|
790
|
+
void swap(HalfMatchResult& hm) {
|
791
|
+
text1_a.swap(hm.text1_a), text1_b.swap(hm.text1_b), text2_a.swap(hm.text2_a), text2_b.swap(hm.text2_b), mid_common.swap(hm.mid_common);
|
792
|
+
}
|
793
|
+
};
|
794
|
+
|
795
|
+
static bool diff_halfMatch(const string_t &text1, const string_t &text2, HalfMatchResult& hm) {
|
796
|
+
const string_t longtext = text1.length() > text2.length() ? text1 : text2;
|
797
|
+
const string_t shorttext = text1.length() > text2.length() ? text2 : text1;
|
798
|
+
if (longtext.length() < 4 || shorttext.length() * 2 < longtext.length()) {
|
799
|
+
return false; // Pointless.
|
800
|
+
}
|
801
|
+
|
802
|
+
HalfMatchResult res1, res2;
|
803
|
+
// First check if the second quarter is the seed for a half-match.
|
804
|
+
bool hm1 = diff_halfMatchI(longtext, shorttext,
|
805
|
+
(longtext.length() + 3) / 4, res1);
|
806
|
+
// Check again based on the third quarter.
|
807
|
+
bool hm2 = diff_halfMatchI(longtext, shorttext,
|
808
|
+
(longtext.length() + 1) / 2, res2);
|
809
|
+
if (!hm1 && !hm2) {
|
810
|
+
return false;
|
811
|
+
} else if (!hm2) {
|
812
|
+
hm.swap(res1);
|
813
|
+
} else if (!hm1) {
|
814
|
+
hm.swap(res2);
|
815
|
+
} else {
|
816
|
+
// Both matched. Select the longest.
|
817
|
+
hm.swap(res1.mid_common.length() > res2.mid_common.length() ? res1 : res2);
|
818
|
+
}
|
819
|
+
|
820
|
+
// A half-match was found, sort out the return data.
|
821
|
+
if (text1.length() <= text2.length()) {
|
822
|
+
hm.text1_a.swap(hm.text2_a);
|
823
|
+
hm.text1_b.swap(hm.text2_b);
|
824
|
+
}
|
825
|
+
return true;
|
826
|
+
}
|
827
|
+
|
828
|
+
/**
|
829
|
+
* Does a substring of shorttext exist within longtext such that the
|
830
|
+
* substring is at least half the length of longtext?
|
831
|
+
* @param longtext Longer string.
|
832
|
+
* @param shorttext Shorter string.
|
833
|
+
* @param i Start index of quarter length substring within longtext.
|
834
|
+
* @param HalfMatchResult object, containing the prefix of longtext, the
|
835
|
+
* suffix of longtext, the prefix of shorttext, the suffix of shorttext
|
836
|
+
* and the common middle.
|
837
|
+
* @return Boolean true if there was a match, false otherwise.
|
838
|
+
*/
|
839
|
+
private:
|
840
|
+
static bool diff_halfMatchI(const string_t &longtext, const string_t &shorttext, int i, HalfMatchResult& best) {
|
841
|
+
// Start with a 1/4 length substring at position i as a seed.
|
842
|
+
const string_t seed = safeMid(longtext, i, longtext.length() / 4);
|
843
|
+
size_t j = string_t::npos;
|
844
|
+
while ((j = shorttext.find(seed, j + 1)) != string_t::npos) {
|
845
|
+
const int prefixLength = diff_commonPrefix(safeMid(longtext, i),
|
846
|
+
safeMid(shorttext, j));
|
847
|
+
const int suffixLength = diff_commonSuffix(longtext.substr(0, i),
|
848
|
+
shorttext.substr(0, j));
|
849
|
+
if ((int)best.mid_common.length() < suffixLength + prefixLength) {
|
850
|
+
best.mid_common = safeMid(shorttext, j - suffixLength, suffixLength)
|
851
|
+
+ safeMid(shorttext, j, prefixLength);
|
852
|
+
best.text1_a = longtext.substr(0, i - suffixLength);
|
853
|
+
best.text1_b = safeMid(longtext, i + prefixLength);
|
854
|
+
best.text2_a = shorttext.substr(0, j - suffixLength);
|
855
|
+
best.text2_b = safeMid(shorttext, j + prefixLength);
|
856
|
+
}
|
857
|
+
}
|
858
|
+
return best.mid_common.length() * 2 >= longtext.length();
|
859
|
+
}
|
860
|
+
|
861
|
+
/**
|
862
|
+
* Reduce the number of edits by eliminating semantically trivial equalities.
|
863
|
+
* @param diffs LinkedList of Diff objects.
|
864
|
+
*/
|
865
|
+
public:
|
866
|
+
static void diff_cleanupSemantic(Diffs &diffs) {
|
867
|
+
if (diffs.empty()) {
|
868
|
+
return;
|
869
|
+
}
|
870
|
+
bool changes = false;
|
871
|
+
std::vector<typename Diffs::iterator> equalities; // Stack of equalities.
|
872
|
+
string_t lastequality; // Always equal to equalities.lastElement().text
|
873
|
+
typename Diffs::iterator cur_diff;
|
874
|
+
// Number of characters that changed prior to the equality.
|
875
|
+
int length_insertions1 = 0;
|
876
|
+
int length_deletions1 = 0;
|
877
|
+
// Number of characters that changed after the equality.
|
878
|
+
int length_insertions2 = 0;
|
879
|
+
int length_deletions2 = 0;
|
880
|
+
for (cur_diff = diffs.begin(); cur_diff != diffs.end();) {
|
881
|
+
if ((*cur_diff).operation == EQUAL) {
|
882
|
+
// Equality found.
|
883
|
+
equalities.push_back(cur_diff);
|
884
|
+
length_insertions1 = length_insertions2;
|
885
|
+
length_deletions1 = length_deletions2;
|
886
|
+
length_insertions2 = 0;
|
887
|
+
length_deletions2 = 0;
|
888
|
+
lastequality = (*cur_diff).text;
|
889
|
+
} else {
|
890
|
+
// An insertion or deletion.
|
891
|
+
if ((*cur_diff).operation == INSERT) {
|
892
|
+
length_insertions2 += (*cur_diff).text.length();
|
893
|
+
} else {
|
894
|
+
length_deletions2 += (*cur_diff).text.length();
|
895
|
+
}
|
896
|
+
if (!lastequality.empty()
|
897
|
+
&& ((int)lastequality.length()
|
898
|
+
<= std::max(length_insertions1, length_deletions1))
|
899
|
+
&& ((int)lastequality.length()
|
900
|
+
<= std::max(length_insertions2, length_deletions2))) {
|
901
|
+
// printf("Splitting: '%s'\n", qPrintable(lastequality));
|
902
|
+
// Walk back to offending equality.
|
903
|
+
// Change second copy to insert.
|
904
|
+
(*(cur_diff = equalities.back())).operation = INSERT;
|
905
|
+
// Duplicate record.
|
906
|
+
diffs.insert(cur_diff, Diff(DELETE, lastequality));
|
907
|
+
equalities.pop_back(); // Throw away the equality we just deleted.
|
908
|
+
if (!equalities.empty()) {
|
909
|
+
// Throw away the previous equality (it needs to be reevaluated).
|
910
|
+
equalities.pop_back();
|
911
|
+
}
|
912
|
+
length_insertions1 = 0; // Reset the counters.
|
913
|
+
length_deletions1 = 0;
|
914
|
+
length_insertions2 = 0;
|
915
|
+
length_deletions2 = 0;
|
916
|
+
lastequality = string_t();
|
917
|
+
changes = true;
|
918
|
+
|
919
|
+
if (!equalities.empty())
|
920
|
+
// There is a safe equality we can fall back to.
|
921
|
+
cur_diff = equalities.back();
|
922
|
+
else
|
923
|
+
{
|
924
|
+
// There are no previous equalities, walk back to the start.
|
925
|
+
cur_diff = diffs.begin();
|
926
|
+
continue;
|
927
|
+
}
|
928
|
+
}
|
929
|
+
}
|
930
|
+
++cur_diff;
|
931
|
+
}
|
932
|
+
|
933
|
+
// Normalize the diff.
|
934
|
+
if (changes) {
|
935
|
+
diff_cleanupMerge(diffs);
|
936
|
+
}
|
937
|
+
diff_cleanupSemanticLossless(diffs);
|
938
|
+
|
939
|
+
// Find any overlaps between deletions and insertions.
|
940
|
+
// e.g: <del>abcxx</del><ins>xxdef</ins>
|
941
|
+
// -> <del>abc</del>xx<ins>def</ins>
|
942
|
+
if ((cur_diff = diffs.begin()) != diffs.end()) {
|
943
|
+
for (typename Diffs::iterator prev_diff = cur_diff; ++cur_diff != diffs.end(); prev_diff = cur_diff) {
|
944
|
+
if ((*prev_diff).operation == DELETE &&
|
945
|
+
(*cur_diff).operation == INSERT) {
|
946
|
+
string_t deletion = (*prev_diff).text;
|
947
|
+
string_t insertion = (*cur_diff).text;
|
948
|
+
int overlap_length = diff_commonOverlap(deletion, insertion);
|
949
|
+
if (overlap_length != 0) {
|
950
|
+
// Overlap found. Insert an equality and trim the surrounding edits.
|
951
|
+
diffs.insert(cur_diff, Diff(EQUAL, insertion.substr(0, overlap_length)));
|
952
|
+
(*prev_diff).text =
|
953
|
+
deletion.substr(0, deletion.length() - overlap_length);
|
954
|
+
(*cur_diff).text = safeMid(insertion, overlap_length);
|
955
|
+
// diffs.insert inserts the element before the cursor, so there is
|
956
|
+
// no need to step past the new element.
|
957
|
+
}
|
958
|
+
if (++cur_diff == diffs.end()) break;
|
959
|
+
}
|
960
|
+
}
|
961
|
+
}
|
962
|
+
}
|
963
|
+
|
964
|
+
/**
|
965
|
+
* Look for single edits surrounded on both sides by equalities
|
966
|
+
* which can be shifted sideways to align the edit to a word boundary.
|
967
|
+
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
|
968
|
+
* @param diffs LinkedList of Diff objects.
|
969
|
+
*/
|
970
|
+
public:
|
971
|
+
static void diff_cleanupSemanticLossless(Diffs &diffs) {
|
972
|
+
string_t equality1, edit, equality2;
|
973
|
+
string_t commonString;
|
974
|
+
int commonOffset;
|
975
|
+
int score, bestScore;
|
976
|
+
string_t bestEquality1, bestEdit, bestEquality2;
|
977
|
+
// Create a new iterator at the start.
|
978
|
+
typename Diffs::iterator prev_diff = diffs.begin(), cur_diff = prev_diff;
|
979
|
+
if (prev_diff == diffs.end() || ++cur_diff == diffs.end()) return;
|
980
|
+
|
981
|
+
// Intentionally ignore the first and last element (don't need checking).
|
982
|
+
for (typename Diffs::iterator next_diff = cur_diff; ++next_diff != diffs.end(); prev_diff = cur_diff, cur_diff = next_diff) {
|
983
|
+
if ((*prev_diff).operation == EQUAL &&
|
984
|
+
(*next_diff).operation == EQUAL) {
|
985
|
+
// This is a single edit surrounded by equalities.
|
986
|
+
equality1 = (*prev_diff).text;
|
987
|
+
edit = (*cur_diff).text;
|
988
|
+
equality2 = (*next_diff).text;
|
989
|
+
|
990
|
+
// First, shift the edit as far left as possible.
|
991
|
+
commonOffset = diff_commonSuffix(equality1, edit);
|
992
|
+
if (commonOffset != 0) {
|
993
|
+
commonString = safeMid(edit, edit.length() - commonOffset);
|
994
|
+
equality1 = equality1.substr(0, equality1.length() - commonOffset);
|
995
|
+
edit = commonString + edit.substr(0, edit.length() - commonOffset);
|
996
|
+
equality2 = commonString + equality2;
|
997
|
+
}
|
998
|
+
|
999
|
+
// Second, step character by character right, looking for the best fit.
|
1000
|
+
bestEquality1 = equality1;
|
1001
|
+
bestEdit = edit;
|
1002
|
+
bestEquality2 = equality2;
|
1003
|
+
bestScore = diff_cleanupSemanticScore(equality1, edit)
|
1004
|
+
+ diff_cleanupSemanticScore(edit, equality2);
|
1005
|
+
while (!edit.empty() && !equality2.empty()
|
1006
|
+
&& edit[0] == equality2[0]) {
|
1007
|
+
equality1 += edit[0];
|
1008
|
+
edit = safeMid(edit, 1) + equality2[0];
|
1009
|
+
equality2 = safeMid(equality2, 1);
|
1010
|
+
score = diff_cleanupSemanticScore(equality1, edit)
|
1011
|
+
+ diff_cleanupSemanticScore(edit, equality2);
|
1012
|
+
// The >= encourages trailing rather than leading whitespace on edits.
|
1013
|
+
if (score >= bestScore) {
|
1014
|
+
bestScore = score;
|
1015
|
+
bestEquality1 = equality1;
|
1016
|
+
bestEdit = edit;
|
1017
|
+
bestEquality2 = equality2;
|
1018
|
+
}
|
1019
|
+
}
|
1020
|
+
|
1021
|
+
if ((*prev_diff).text != bestEquality1) {
|
1022
|
+
// We have an improvement, save it back to the diff.
|
1023
|
+
if (!bestEquality1.empty()) {
|
1024
|
+
(*prev_diff).text = bestEquality1;
|
1025
|
+
} else {
|
1026
|
+
diffs.erase(prev_diff);
|
1027
|
+
}
|
1028
|
+
(*cur_diff).text = bestEdit;
|
1029
|
+
if (!bestEquality2.empty()) {
|
1030
|
+
(*next_diff).text = bestEquality2;
|
1031
|
+
} else {
|
1032
|
+
diffs.erase(next_diff); // Delete nextDiff.
|
1033
|
+
next_diff = cur_diff;
|
1034
|
+
cur_diff = prev_diff;
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
}
|
1038
|
+
}
|
1039
|
+
}
|
1040
|
+
|
1041
|
+
/**
|
1042
|
+
* Given two strings, compute a score representing whether the internal
|
1043
|
+
* boundary falls on logical boundaries.
|
1044
|
+
* Scores range from 5 (best) to 0 (worst).
|
1045
|
+
* @param one First string.
|
1046
|
+
* @param two Second string.
|
1047
|
+
* @return The score.
|
1048
|
+
*/
|
1049
|
+
private:
|
1050
|
+
static int diff_cleanupSemanticScore(const string_t &one, const string_t &two) {
|
1051
|
+
if (one.empty() || two.empty()) {
|
1052
|
+
// Edges are the best.
|
1053
|
+
return 10;
|
1054
|
+
}
|
1055
|
+
|
1056
|
+
// Each port of this function behaves slightly differently due to
|
1057
|
+
// subtle differences in each language's definition of things like
|
1058
|
+
// 'whitespace'. Since this function's purpose is largely cosmetic,
|
1059
|
+
// the choice has been made to use each language's native features
|
1060
|
+
// rather than force total conformity.
|
1061
|
+
int score = 0;
|
1062
|
+
// One point for non-alphanumeric.
|
1063
|
+
if (!traits::is_alnum(one[one.length() - 1]) || !traits::is_alnum(two[0])) {
|
1064
|
+
score++;
|
1065
|
+
// Two points for whitespace.
|
1066
|
+
if (traits::is_space(one[one.length() - 1]) || traits::is_space(two[0])) {
|
1067
|
+
score++;
|
1068
|
+
// Three points for line breaks.
|
1069
|
+
|
1070
|
+
typename string_t::const_pointer p1 = one.c_str() + one.length() - 1, p2 = two.c_str();
|
1071
|
+
if (is_control(*p1) || is_control(*p2)) {
|
1072
|
+
score++;
|
1073
|
+
// Four points for blank lines.
|
1074
|
+
if (traits::to_wchar(*p1) == L'\n' && p1 != one.c_str() && (traits::to_wchar(*(p1 - 1)) == L'\n'
|
1075
|
+
|| traits::to_wchar(*(p1 - 1)) == L'\r' && p1 - 1 != one.c_str() && traits::to_wchar(*(p1 - 2)) == L'\n')) {
|
1076
|
+
score++;
|
1077
|
+
}
|
1078
|
+
else {
|
1079
|
+
p1 = p2 + two.length();
|
1080
|
+
if (traits::to_wchar(*p2) == L'\r') ++p2;
|
1081
|
+
if (p2 != p1 && traits::to_wchar(*p2) == L'\n') {
|
1082
|
+
if (++p2 != p1 && traits::to_wchar(*p2) == L'\r') ++p2;
|
1083
|
+
if (p2 != p1 && traits::to_wchar(*p2) == L'\n') score++;
|
1084
|
+
}
|
1085
|
+
}
|
1086
|
+
}
|
1087
|
+
}
|
1088
|
+
}
|
1089
|
+
return score;
|
1090
|
+
}
|
1091
|
+
/**
|
1092
|
+
* Reduce the number of edits by eliminating operationally trivial equalities.
|
1093
|
+
* @param diffs LinkedList of Diff objects.
|
1094
|
+
*/
|
1095
|
+
public:
|
1096
|
+
void diff_cleanupEfficiency(Diffs &diffs) const {
|
1097
|
+
if (diffs.empty()) {
|
1098
|
+
return;
|
1099
|
+
}
|
1100
|
+
bool changes = false;
|
1101
|
+
std::vector<typename Diffs::iterator> equalities; // Stack of equalities.
|
1102
|
+
string_t lastequality; // Always equal to equalities.lastElement().text
|
1103
|
+
// Is there an insertion operation before the last equality.
|
1104
|
+
bool pre_ins = false;
|
1105
|
+
// Is there a deletion operation before the last equality.
|
1106
|
+
bool pre_del = false;
|
1107
|
+
// Is there an insertion operation after the last equality.
|
1108
|
+
bool post_ins = false;
|
1109
|
+
// Is there a deletion operation after the last equality.
|
1110
|
+
bool post_del = false;
|
1111
|
+
|
1112
|
+
for (typename Diffs::iterator cur_diff = diffs.begin(); cur_diff != diffs.end();) {
|
1113
|
+
if ((*cur_diff).operation == EQUAL) {
|
1114
|
+
// Equality found.
|
1115
|
+
if ((int)(*cur_diff).text.length() < Diff_EditCost && (post_ins || post_del)) {
|
1116
|
+
// Candidate found.
|
1117
|
+
equalities.push_back(cur_diff);
|
1118
|
+
pre_ins = post_ins;
|
1119
|
+
pre_del = post_del;
|
1120
|
+
lastequality = (*cur_diff).text;
|
1121
|
+
} else {
|
1122
|
+
// Not a candidate, and can never become one.
|
1123
|
+
equalities.clear();
|
1124
|
+
lastequality.clear();
|
1125
|
+
}
|
1126
|
+
post_ins = post_del = false;
|
1127
|
+
} else {
|
1128
|
+
// An insertion or deletion.
|
1129
|
+
if ((*cur_diff).operation == DELETE) {
|
1130
|
+
post_del = true;
|
1131
|
+
} else {
|
1132
|
+
post_ins = true;
|
1133
|
+
}
|
1134
|
+
/*
|
1135
|
+
* Five types to be split:
|
1136
|
+
* <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del>
|
1137
|
+
* <ins>A</ins>X<ins>C</ins><del>D</del>
|
1138
|
+
* <ins>A</ins><del>B</del>X<ins>C</ins>
|
1139
|
+
* <ins>A</del>X<ins>C</ins><del>D</del>
|
1140
|
+
* <ins>A</ins><del>B</del>X<del>C</del>
|
1141
|
+
*/
|
1142
|
+
if (!lastequality.empty()
|
1143
|
+
&& ((pre_ins && pre_del && post_ins && post_del)
|
1144
|
+
|| (((int)lastequality.length() < Diff_EditCost / 2)
|
1145
|
+
&& ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0)
|
1146
|
+
+ (post_ins ? 1 : 0) + (post_del ? 1 : 0)) == 3))) {
|
1147
|
+
// printf("Splitting: '%s'\n", qPrintable(lastequality));
|
1148
|
+
// Walk back to offending equality.
|
1149
|
+
// Change second copy to insert.
|
1150
|
+
(*(cur_diff = equalities.back())).operation = INSERT;
|
1151
|
+
// Duplicate record.
|
1152
|
+
diffs.insert(cur_diff, Diff(DELETE, lastequality));
|
1153
|
+
equalities.pop_back(); // Throw away the equality we just deleted.
|
1154
|
+
lastequality.clear();
|
1155
|
+
changes = true;
|
1156
|
+
if (pre_ins && pre_del) {
|
1157
|
+
// No changes made which could affect previous entry, keep going.
|
1158
|
+
post_ins = post_del = true;
|
1159
|
+
equalities.clear();
|
1160
|
+
} else {
|
1161
|
+
if (!equalities.empty()) {
|
1162
|
+
// Throw away the previous equality (it needs to be reevaluated).
|
1163
|
+
equalities.pop_back();
|
1164
|
+
}
|
1165
|
+
post_ins = post_del = false;
|
1166
|
+
if (!equalities.empty())
|
1167
|
+
// There is a safe equality we can fall back to.
|
1168
|
+
cur_diff = equalities.back();
|
1169
|
+
else
|
1170
|
+
{
|
1171
|
+
// There are no previous equalities, walk back to the start.
|
1172
|
+
cur_diff = diffs.begin();
|
1173
|
+
continue;
|
1174
|
+
}
|
1175
|
+
}
|
1176
|
+
}
|
1177
|
+
}
|
1178
|
+
++cur_diff;
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
if (changes) {
|
1182
|
+
diff_cleanupMerge(diffs);
|
1183
|
+
}
|
1184
|
+
}
|
1185
|
+
|
1186
|
+
/**
|
1187
|
+
* Reorder and merge like edit sections. Merge equalities.
|
1188
|
+
* Any edit section can move as long as it doesn't cross an equality.
|
1189
|
+
* @param diffs LinkedList of Diff objects.
|
1190
|
+
*/
|
1191
|
+
public:
|
1192
|
+
static void diff_cleanupMerge(Diffs &diffs) {
|
1193
|
+
diffs.push_back(Diff(EQUAL, string_t())); // Add a dummy entry at the end.
|
1194
|
+
typename Diffs::iterator prev_diff, cur_diff;
|
1195
|
+
int count_delete = 0;
|
1196
|
+
int count_insert = 0;
|
1197
|
+
string_t text_delete;
|
1198
|
+
string_t text_insert;
|
1199
|
+
Diff *prevEqual = NULL;
|
1200
|
+
int commonlength;
|
1201
|
+
for (cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1202
|
+
switch ((*cur_diff).operation) {
|
1203
|
+
case INSERT:
|
1204
|
+
count_insert++;
|
1205
|
+
text_insert += (*cur_diff).text;
|
1206
|
+
prevEqual = NULL;
|
1207
|
+
break;
|
1208
|
+
case DELETE:
|
1209
|
+
count_delete++;
|
1210
|
+
text_delete += (*cur_diff).text;
|
1211
|
+
prevEqual = NULL;
|
1212
|
+
break;
|
1213
|
+
case EQUAL:
|
1214
|
+
if (count_delete + count_insert > 1) {
|
1215
|
+
// Delete the offending records.
|
1216
|
+
prev_diff = cur_diff;
|
1217
|
+
std::advance(prev_diff, -(count_delete + count_insert));
|
1218
|
+
diffs.erase(prev_diff, cur_diff);
|
1219
|
+
if (count_delete != 0 && count_insert != 0) {
|
1220
|
+
// Factor out any common prefixies.
|
1221
|
+
commonlength = diff_commonPrefix(text_insert, text_delete);
|
1222
|
+
if (commonlength != 0) {
|
1223
|
+
if (cur_diff != diffs.begin()) {
|
1224
|
+
prev_diff = cur_diff;
|
1225
|
+
if ((*--prev_diff).operation != EQUAL) {
|
1226
|
+
throw string_t(traits::cs(L"Previous diff should have been an equality."));
|
1227
|
+
}
|
1228
|
+
(*prev_diff).text += text_insert.substr(0, commonlength);
|
1229
|
+
} else {
|
1230
|
+
diffs.insert(cur_diff, Diff(EQUAL, text_insert.substr(0, commonlength)));
|
1231
|
+
}
|
1232
|
+
text_insert = safeMid(text_insert, commonlength);
|
1233
|
+
text_delete = safeMid(text_delete, commonlength);
|
1234
|
+
}
|
1235
|
+
// Factor out any common suffixies.
|
1236
|
+
commonlength = diff_commonSuffix(text_insert, text_delete);
|
1237
|
+
if (commonlength != 0) {
|
1238
|
+
(*cur_diff).text = safeMid(text_insert, text_insert.length()
|
1239
|
+
- commonlength) + (*cur_diff).text;
|
1240
|
+
text_insert = text_insert.substr(0, text_insert.length()
|
1241
|
+
- commonlength);
|
1242
|
+
text_delete = text_delete.substr(0, text_delete.length()
|
1243
|
+
- commonlength);
|
1244
|
+
}
|
1245
|
+
}
|
1246
|
+
// Insert the merged records.
|
1247
|
+
if (!text_delete.empty()) {
|
1248
|
+
diffs.insert(cur_diff, Diff(DELETE, text_delete));
|
1249
|
+
}
|
1250
|
+
if (!text_insert.empty()) {
|
1251
|
+
diffs.insert(cur_diff, Diff(INSERT, text_insert));
|
1252
|
+
}
|
1253
|
+
} else if (prevEqual != NULL) {
|
1254
|
+
// Merge this equality with the previous one.
|
1255
|
+
prevEqual->text += (*cur_diff).text;
|
1256
|
+
diffs.erase(cur_diff--);
|
1257
|
+
}
|
1258
|
+
|
1259
|
+
count_insert = 0;
|
1260
|
+
count_delete = 0;
|
1261
|
+
text_delete.clear();
|
1262
|
+
text_insert.clear();
|
1263
|
+
prevEqual = &*cur_diff;
|
1264
|
+
break;
|
1265
|
+
}
|
1266
|
+
|
1267
|
+
}
|
1268
|
+
if (diffs.back().text.empty()) {
|
1269
|
+
diffs.pop_back(); // Remove the dummy entry at the end.
|
1270
|
+
}
|
1271
|
+
|
1272
|
+
/*
|
1273
|
+
* Second pass: look for single edits surrounded on both sides by equalities
|
1274
|
+
* which can be shifted sideways to eliminate an equality.
|
1275
|
+
* e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
|
1276
|
+
*/
|
1277
|
+
bool changes = false;
|
1278
|
+
// Create a new iterator at the start.
|
1279
|
+
// (As opposed to walking the current one back.)
|
1280
|
+
prev_diff = cur_diff = diffs.begin();
|
1281
|
+
if (prev_diff != diffs.end() && ++cur_diff != diffs.end()) {
|
1282
|
+
// Intentionally ignore the first and last element (don't need checking).
|
1283
|
+
for (typename Diffs::iterator next_diff = cur_diff; ++next_diff != diffs.end(); prev_diff = cur_diff, cur_diff = next_diff) {
|
1284
|
+
if ((*prev_diff).operation == EQUAL &&
|
1285
|
+
(*next_diff).operation == EQUAL) {
|
1286
|
+
// This is a single edit surrounded by equalities.
|
1287
|
+
if ((*cur_diff).text.size() >= (*prev_diff).text.size() &&
|
1288
|
+
(*cur_diff).text.compare((*cur_diff).text.size() - (*prev_diff).text.size(), (*prev_diff).text.size(), (*prev_diff).text) == 0) {
|
1289
|
+
// Shift the edit over the previous equality.
|
1290
|
+
(*cur_diff).text = (*prev_diff).text
|
1291
|
+
+ (*cur_diff).text.substr(0, (*cur_diff).text.length()
|
1292
|
+
- (*prev_diff).text.length());
|
1293
|
+
(*next_diff).text = (*prev_diff).text + (*next_diff).text;
|
1294
|
+
diffs.erase(prev_diff);
|
1295
|
+
cur_diff = next_diff;
|
1296
|
+
changes = true;
|
1297
|
+
if (++next_diff == diffs.end()) break;
|
1298
|
+
} else if ((*cur_diff).text.size() >= (*next_diff).text.size() && (*cur_diff).text.compare(0, (*next_diff).text.size(), (*next_diff).text) == 0) {
|
1299
|
+
// Shift the edit over the next equality.
|
1300
|
+
(*prev_diff).text += (*next_diff).text;
|
1301
|
+
(*cur_diff).text = safeMid((*cur_diff).text, (*next_diff).text.length())
|
1302
|
+
+ (*next_diff).text;
|
1303
|
+
next_diff = diffs.erase(next_diff); // Delete nextDiff.
|
1304
|
+
changes = true;
|
1305
|
+
if (next_diff == diffs.end()) break;
|
1306
|
+
}
|
1307
|
+
}
|
1308
|
+
}
|
1309
|
+
}
|
1310
|
+
// If shifts were made, the diff needs reordering and another shift sweep.
|
1311
|
+
if (changes) {
|
1312
|
+
diff_cleanupMerge(diffs);
|
1313
|
+
}
|
1314
|
+
}
|
1315
|
+
|
1316
|
+
/**
|
1317
|
+
* loc is a location in text1, compute and return the equivalent location in
|
1318
|
+
* text2.
|
1319
|
+
* e.g. "The cat" vs "The big cat", 1->1, 5->8
|
1320
|
+
* @param diffs LinkedList of Diff objects.
|
1321
|
+
* @param loc Location within text1.
|
1322
|
+
* @return Location within text2.
|
1323
|
+
*/
|
1324
|
+
public:
|
1325
|
+
static int diff_xIndex(const Diffs &diffs, int loc) {
|
1326
|
+
int chars1 = 0;
|
1327
|
+
int chars2 = 0;
|
1328
|
+
int last_chars1 = 0;
|
1329
|
+
int last_chars2 = 0;
|
1330
|
+
typename Diffs::const_iterator last_diff = diffs.end(), cur_diff;
|
1331
|
+
for (cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1332
|
+
if ((*cur_diff).operation != INSERT) {
|
1333
|
+
// Equality or deletion.
|
1334
|
+
chars1 += (*cur_diff).text.length();
|
1335
|
+
}
|
1336
|
+
if ((*cur_diff).operation != DELETE) {
|
1337
|
+
// Equality or insertion.
|
1338
|
+
chars2 += (*cur_diff).text.length();
|
1339
|
+
}
|
1340
|
+
if (chars1 > loc) {
|
1341
|
+
// Overshot the location.
|
1342
|
+
last_diff = cur_diff;
|
1343
|
+
break;
|
1344
|
+
}
|
1345
|
+
last_chars1 = chars1;
|
1346
|
+
last_chars2 = chars2;
|
1347
|
+
}
|
1348
|
+
if (last_diff != diffs.end() && (*last_diff).operation == DELETE) {
|
1349
|
+
// The location was deleted.
|
1350
|
+
return last_chars2;
|
1351
|
+
}
|
1352
|
+
// Add the remaining character length.
|
1353
|
+
return last_chars2 + (loc - last_chars1);
|
1354
|
+
}
|
1355
|
+
|
1356
|
+
/**
|
1357
|
+
* Convert a Diff list into a pretty HTML report.
|
1358
|
+
* @param diffs LinkedList of Diff objects.
|
1359
|
+
* @return HTML representation.
|
1360
|
+
*/
|
1361
|
+
public:
|
1362
|
+
static string_t diff_prettyHtml(const Diffs &diffs) {
|
1363
|
+
string_t html;
|
1364
|
+
string_t text;
|
1365
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1366
|
+
typename string_t::size_type n = (*cur_diff).text.size();
|
1367
|
+
typename string_t::const_pointer p, end;
|
1368
|
+
for (p = (*cur_diff).text.c_str(), end = p + n; p != end; ++p)
|
1369
|
+
switch (traits::to_wchar(*p)) {
|
1370
|
+
case L'&': n += 4; break;
|
1371
|
+
case L'<':
|
1372
|
+
case L'>': n += 3; break;
|
1373
|
+
case L'\n': n += 9; break;
|
1374
|
+
}
|
1375
|
+
if (n == (*cur_diff).text.size())
|
1376
|
+
text = (*cur_diff).text;
|
1377
|
+
else {
|
1378
|
+
text.clear();
|
1379
|
+
text.reserve(n);
|
1380
|
+
for (p = (*cur_diff).text.c_str(); p != end; ++p)
|
1381
|
+
switch (traits::to_wchar(*p)) {
|
1382
|
+
case L'&': text += traits::cs(L"&"); break;
|
1383
|
+
case L'<': text += traits::cs(L"<"); break;
|
1384
|
+
case L'>': text += traits::cs(L">"); break;
|
1385
|
+
case L'\n': text += traits::cs(L"¶<br>"); break;
|
1386
|
+
default: text += *p;
|
1387
|
+
}
|
1388
|
+
}
|
1389
|
+
switch ((*cur_diff).operation) {
|
1390
|
+
case INSERT:
|
1391
|
+
html += traits::cs(L"<ins style=\"background:#e6ffe6;\">") + text + traits::cs(L"</ins>");
|
1392
|
+
break;
|
1393
|
+
case DELETE:
|
1394
|
+
html += traits::cs(L"<del style=\"background:#ffe6e6;\">") + text + traits::cs(L"</del>");
|
1395
|
+
break;
|
1396
|
+
case EQUAL:
|
1397
|
+
html += traits::cs(L"<span>") + text + traits::cs(L"</span>");
|
1398
|
+
break;
|
1399
|
+
}
|
1400
|
+
}
|
1401
|
+
return html;
|
1402
|
+
}
|
1403
|
+
|
1404
|
+
/**
|
1405
|
+
* Compute and return the source text (all equalities and deletions).
|
1406
|
+
* @param diffs LinkedList of Diff objects.
|
1407
|
+
* @return Source text.
|
1408
|
+
*/
|
1409
|
+
public:
|
1410
|
+
static string_t diff_text1(const Diffs &diffs) {
|
1411
|
+
string_t text;
|
1412
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1413
|
+
if ((*cur_diff).operation != INSERT) {
|
1414
|
+
text += (*cur_diff).text;
|
1415
|
+
}
|
1416
|
+
}
|
1417
|
+
return text;
|
1418
|
+
}
|
1419
|
+
|
1420
|
+
/**
|
1421
|
+
* Compute and return the destination text (all equalities and insertions).
|
1422
|
+
* @param diffs LinkedList of Diff objects.
|
1423
|
+
* @return Destination text.
|
1424
|
+
*/
|
1425
|
+
public:
|
1426
|
+
static string_t diff_text2(const Diffs &diffs) {
|
1427
|
+
string_t text;
|
1428
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1429
|
+
if ((*cur_diff).operation != DELETE) {
|
1430
|
+
text += (*cur_diff).text;
|
1431
|
+
}
|
1432
|
+
}
|
1433
|
+
return text;
|
1434
|
+
}
|
1435
|
+
|
1436
|
+
/**
|
1437
|
+
* Compute the Levenshtein distance; the number of inserted, deleted or
|
1438
|
+
* substituted characters.
|
1439
|
+
* @param diffs LinkedList of Diff objects.
|
1440
|
+
* @return Number of changes.
|
1441
|
+
*/
|
1442
|
+
public:
|
1443
|
+
static int diff_levenshtein(const Diffs &diffs) {
|
1444
|
+
int levenshtein = 0;
|
1445
|
+
int insertions = 0;
|
1446
|
+
int deletions = 0;
|
1447
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1448
|
+
switch ((*cur_diff).operation) {
|
1449
|
+
case INSERT:
|
1450
|
+
insertions += (*cur_diff).text.length();
|
1451
|
+
break;
|
1452
|
+
case DELETE:
|
1453
|
+
deletions += (*cur_diff).text.length();
|
1454
|
+
break;
|
1455
|
+
case EQUAL:
|
1456
|
+
// A deletion and an insertion is one substitution.
|
1457
|
+
levenshtein += std::max(insertions, deletions);
|
1458
|
+
insertions = 0;
|
1459
|
+
deletions = 0;
|
1460
|
+
break;
|
1461
|
+
}
|
1462
|
+
}
|
1463
|
+
levenshtein += std::max(insertions, deletions);
|
1464
|
+
return levenshtein;
|
1465
|
+
}
|
1466
|
+
|
1467
|
+
/**
|
1468
|
+
* Crush the diff into an encoded string which describes the operations
|
1469
|
+
* required to transform text1 into text2.
|
1470
|
+
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
|
1471
|
+
* Operations are tab-separated. Inserted text is escaped using %xx notation.
|
1472
|
+
* @param diffs Array of diff tuples.
|
1473
|
+
* @return Delta text.
|
1474
|
+
*/
|
1475
|
+
public:
|
1476
|
+
static string_t diff_toDelta(const Diffs &diffs) {
|
1477
|
+
string_t text;
|
1478
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1479
|
+
switch ((*cur_diff).operation) {
|
1480
|
+
case INSERT: {
|
1481
|
+
text += traits::from_wchar(L'+');
|
1482
|
+
append_percent_encoded(text, (*cur_diff).text);
|
1483
|
+
text += traits::from_wchar(L'\t');
|
1484
|
+
break;
|
1485
|
+
}
|
1486
|
+
case DELETE:
|
1487
|
+
text += traits::from_wchar(L'-') + to_string((*cur_diff).text.length()) + traits::from_wchar(L'\t');
|
1488
|
+
break;
|
1489
|
+
case EQUAL:
|
1490
|
+
text += traits::from_wchar(L'=') + to_string((*cur_diff).text.length()) + traits::from_wchar(L'\t');
|
1491
|
+
break;
|
1492
|
+
}
|
1493
|
+
}
|
1494
|
+
if (!text.empty()) {
|
1495
|
+
// Strip off trailing tab character.
|
1496
|
+
text = text.substr(0, text.length() - 1);
|
1497
|
+
}
|
1498
|
+
return text;
|
1499
|
+
}
|
1500
|
+
|
1501
|
+
/**
|
1502
|
+
* Given the original text1, and an encoded string which describes the
|
1503
|
+
* operations required to transform text1 into text2, compute the full diff.
|
1504
|
+
* @param text1 Source string for the diff.
|
1505
|
+
* @param delta Delta text.
|
1506
|
+
* @return Array of diff tuples or null if invalid.
|
1507
|
+
* @throws string_t If invalid input.
|
1508
|
+
*/
|
1509
|
+
public:
|
1510
|
+
static Diffs diff_fromDelta(const string_t &text1, const string_t &delta) {
|
1511
|
+
Diffs diffs;
|
1512
|
+
int pointer = 0; // Cursor in text1
|
1513
|
+
typename string_t::size_type token_len;
|
1514
|
+
for (typename string_t::const_pointer token = delta.c_str(); token - delta.c_str() < (int)delta.length(); token += token_len + 1) {
|
1515
|
+
token_len = next_token(delta, traits::from_wchar(L'\t'), token);
|
1516
|
+
if (token_len == 0) {
|
1517
|
+
// Blank tokens are ok (from a trailing \t).
|
1518
|
+
continue;
|
1519
|
+
}
|
1520
|
+
// Each token begins with a one character parameter which specifies the
|
1521
|
+
// operation of this token (delete, insert, equality).
|
1522
|
+
string_t param(token + 1, token_len - 1);
|
1523
|
+
switch (traits::to_wchar(*token)) {
|
1524
|
+
case L'+':
|
1525
|
+
percent_decode(param);
|
1526
|
+
diffs.push_back(Diff(INSERT, param));
|
1527
|
+
break;
|
1528
|
+
case L'-':
|
1529
|
+
// Fall through.
|
1530
|
+
case L'=': {
|
1531
|
+
int n;
|
1532
|
+
n = to_int(param);
|
1533
|
+
if (n < 0) {
|
1534
|
+
throw string_t(traits::cs(L"Negative number in diff_fromDelta: ") + param);
|
1535
|
+
}
|
1536
|
+
string_t text;
|
1537
|
+
text = safeMid(text1, pointer, n);
|
1538
|
+
pointer += n;
|
1539
|
+
if (traits::to_wchar(*token) == L'=') {
|
1540
|
+
diffs.push_back(Diff(EQUAL, text));
|
1541
|
+
} else {
|
1542
|
+
diffs.push_back(Diff(DELETE, text));
|
1543
|
+
}
|
1544
|
+
break;
|
1545
|
+
}
|
1546
|
+
default:
|
1547
|
+
throw string_t(string_t(traits::cs(L"Invalid diff operation in diff_fromDelta: ")) + *token);
|
1548
|
+
}
|
1549
|
+
}
|
1550
|
+
if (pointer != text1.length()) {
|
1551
|
+
throw string_t(traits::cs(L"Delta length (") + to_string(pointer)
|
1552
|
+
+ traits::cs(L") smaller than source text length (")
|
1553
|
+
+ to_string(text1.length()) + traits::from_wchar(L')'));
|
1554
|
+
}
|
1555
|
+
return diffs;
|
1556
|
+
}
|
1557
|
+
|
1558
|
+
|
1559
|
+
// MATCH FUNCTIONS
|
1560
|
+
|
1561
|
+
|
1562
|
+
/**
|
1563
|
+
* Locate the best instance of 'pattern' in 'text' near 'loc'.
|
1564
|
+
* Returns -1 if no match found.
|
1565
|
+
* @param text The text to search.
|
1566
|
+
* @param pattern The pattern to search for.
|
1567
|
+
* @param loc The location to search around.
|
1568
|
+
* @return Best match index or -1.
|
1569
|
+
*/
|
1570
|
+
public:
|
1571
|
+
int match_main(const string_t &text, const string_t &pattern, int loc) const {
|
1572
|
+
loc = std::max(0, std::min(loc, (int)text.length()));
|
1573
|
+
if (text == pattern) {
|
1574
|
+
// Shortcut (potentially not guaranteed by the algorithm)
|
1575
|
+
return 0;
|
1576
|
+
} else if (text.empty()) {
|
1577
|
+
// Nothing to match.
|
1578
|
+
return -1;
|
1579
|
+
} else if (loc + pattern.length() <= text.length()
|
1580
|
+
&& safeMid(text, loc, pattern.length()) == pattern) {
|
1581
|
+
// Perfect match at the perfect spot! (Includes case of null pattern)
|
1582
|
+
return loc;
|
1583
|
+
} else {
|
1584
|
+
// Do a fuzzy compare.
|
1585
|
+
return match_bitap(text, pattern, loc);
|
1586
|
+
}
|
1587
|
+
}
|
1588
|
+
|
1589
|
+
/**
|
1590
|
+
* Locate the best instance of 'pattern' in 'text' near 'loc' using the
|
1591
|
+
* Bitap algorithm. Returns -1 if no match found.
|
1592
|
+
* @param text The text to search.
|
1593
|
+
* @param pattern The pattern to search for.
|
1594
|
+
* @param loc The location to search around.
|
1595
|
+
* @return Best match index or -1.
|
1596
|
+
*/
|
1597
|
+
protected:
|
1598
|
+
int match_bitap(const string_t &text, const string_t &pattern, int loc) const {
|
1599
|
+
if (!(Match_MaxBits == 0 || (int)pattern.length() <= Match_MaxBits)) {
|
1600
|
+
throw string_t(traits::cs(L"Pattern too long for this application."));
|
1601
|
+
}
|
1602
|
+
|
1603
|
+
// Initialise the alphabet.
|
1604
|
+
std::map<char_t, int> s;
|
1605
|
+
match_alphabet(pattern, s);
|
1606
|
+
|
1607
|
+
// Highest score beyond which we give up.
|
1608
|
+
double score_threshold = Match_Threshold;
|
1609
|
+
// Is there a nearby exact match? (speedup)
|
1610
|
+
size_t best_loc = text.find(pattern, loc);
|
1611
|
+
if (best_loc != string_t::npos) {
|
1612
|
+
score_threshold = std::min(match_bitapScore(0, best_loc, loc, pattern),
|
1613
|
+
score_threshold);
|
1614
|
+
// What about in the other direction? (speedup)
|
1615
|
+
best_loc = text.rfind(pattern, loc + pattern.length());
|
1616
|
+
if (best_loc != string_t::npos) {
|
1617
|
+
score_threshold = std::min(match_bitapScore(0, best_loc, loc, pattern),
|
1618
|
+
score_threshold);
|
1619
|
+
}
|
1620
|
+
}
|
1621
|
+
|
1622
|
+
// Initialise the bit arrays.
|
1623
|
+
int matchmask = 1 << (pattern.length() - 1);
|
1624
|
+
best_loc = -1;
|
1625
|
+
|
1626
|
+
int bin_min, bin_mid;
|
1627
|
+
int bin_max = pattern.length() + text.length();
|
1628
|
+
int *rd;
|
1629
|
+
int *last_rd = NULL;
|
1630
|
+
for (int d = 0; d < (int)pattern.length(); d++) {
|
1631
|
+
// Scan for the best match; each iteration allows for one more error.
|
1632
|
+
// Run a binary search to determine how far from 'loc' we can stray at
|
1633
|
+
// this error level.
|
1634
|
+
bin_min = 0;
|
1635
|
+
bin_mid = bin_max;
|
1636
|
+
while (bin_min < bin_mid) {
|
1637
|
+
if (match_bitapScore(d, loc + bin_mid, loc, pattern)
|
1638
|
+
<= score_threshold) {
|
1639
|
+
bin_min = bin_mid;
|
1640
|
+
} else {
|
1641
|
+
bin_max = bin_mid;
|
1642
|
+
}
|
1643
|
+
bin_mid = (bin_max - bin_min) / 2 + bin_min;
|
1644
|
+
}
|
1645
|
+
// Use the result from this iteration as the maximum for the next.
|
1646
|
+
bin_max = bin_mid;
|
1647
|
+
int start = std::max(1, loc - bin_mid + 1);
|
1648
|
+
int finish = std::min(loc + bin_mid, (int)text.length()) + pattern.length();
|
1649
|
+
|
1650
|
+
rd = new int[finish + 2];
|
1651
|
+
rd[finish + 1] = (1 << d) - 1;
|
1652
|
+
for (int j = finish; j >= start; j--) {
|
1653
|
+
int charMatch;
|
1654
|
+
if ((int)text.length() <= j - 1) {
|
1655
|
+
// Out of range.
|
1656
|
+
charMatch = 0;
|
1657
|
+
} else {
|
1658
|
+
charMatch = s[text[j - 1]];
|
1659
|
+
}
|
1660
|
+
if (d == 0) {
|
1661
|
+
// First pass: exact match.
|
1662
|
+
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
|
1663
|
+
} else {
|
1664
|
+
// Subsequent passes: fuzzy match.
|
1665
|
+
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch
|
1666
|
+
| (((last_rd[j + 1] | last_rd[j]) << 1) | 1)
|
1667
|
+
| last_rd[j + 1];
|
1668
|
+
}
|
1669
|
+
if ((rd[j] & matchmask) != 0) {
|
1670
|
+
double score = match_bitapScore(d, j - 1, loc, pattern);
|
1671
|
+
// This match will almost certainly be better than any existing
|
1672
|
+
// match. But check anyway.
|
1673
|
+
if (score <= score_threshold) {
|
1674
|
+
// Told you so.
|
1675
|
+
score_threshold = score;
|
1676
|
+
best_loc = j - 1;
|
1677
|
+
if ((int)best_loc > loc) {
|
1678
|
+
// When passing loc, don't exceed our current distance from loc.
|
1679
|
+
start = std::max(1, 2 * loc - (int)best_loc);
|
1680
|
+
} else {
|
1681
|
+
// Already passed loc, downhill from here on in.
|
1682
|
+
break;
|
1683
|
+
}
|
1684
|
+
}
|
1685
|
+
}
|
1686
|
+
}
|
1687
|
+
if (match_bitapScore(d + 1, loc, loc, pattern) > score_threshold) {
|
1688
|
+
// No hope for a (better) match at greater error levels.
|
1689
|
+
break;
|
1690
|
+
}
|
1691
|
+
delete [] last_rd;
|
1692
|
+
last_rd = rd;
|
1693
|
+
}
|
1694
|
+
delete [] last_rd;
|
1695
|
+
delete [] rd;
|
1696
|
+
return best_loc;
|
1697
|
+
}
|
1698
|
+
|
1699
|
+
/**
|
1700
|
+
* Compute and return the score for a match with e errors and x location.
|
1701
|
+
* @param e Number of errors in match.
|
1702
|
+
* @param x Location of match.
|
1703
|
+
* @param loc Expected location of match.
|
1704
|
+
* @param pattern Pattern being sought.
|
1705
|
+
* @return Overall score for match (0.0 = good, 1.0 = bad).
|
1706
|
+
*/
|
1707
|
+
private:
|
1708
|
+
double match_bitapScore(int e, int x, int loc, const string_t &pattern) const {
|
1709
|
+
const float accuracy = static_cast<float> (e) / pattern.length();
|
1710
|
+
const int proximity = (loc - x < 0)? (x - loc) : (loc - x);
|
1711
|
+
if (Match_Distance == 0) {
|
1712
|
+
// Dodge divide by zero error.
|
1713
|
+
return proximity == 0 ? accuracy : 1.0;
|
1714
|
+
}
|
1715
|
+
return accuracy + (proximity / static_cast<float> (Match_Distance));
|
1716
|
+
}
|
1717
|
+
|
1718
|
+
/**
|
1719
|
+
* Initialise the alphabet for the Bitap algorithm.
|
1720
|
+
* @param pattern The text to encode.
|
1721
|
+
* @return Hash of character locations.
|
1722
|
+
*/
|
1723
|
+
protected:
|
1724
|
+
static void match_alphabet(const string_t &pattern, std::map<char_t, int>& s) {
|
1725
|
+
// There is no need to initialize map values, since they are zero-initialized by default
|
1726
|
+
for (size_t i = 0; i < pattern.length(); i++)
|
1727
|
+
s[pattern[i]] |= (1 << (pattern.length() - i - 1));
|
1728
|
+
}
|
1729
|
+
|
1730
|
+
|
1731
|
+
// PATCH FUNCTIONS
|
1732
|
+
|
1733
|
+
|
1734
|
+
/**
|
1735
|
+
* Increase the context until it is unique,
|
1736
|
+
* but don't let the pattern expand beyond Match_MaxBits.
|
1737
|
+
* @param patch The patch to grow.
|
1738
|
+
* @param text Source text.
|
1739
|
+
*/
|
1740
|
+
protected:
|
1741
|
+
void patch_addContext(Patch &patch, const string_t &text) const {
|
1742
|
+
if (text.empty()) {
|
1743
|
+
return;
|
1744
|
+
}
|
1745
|
+
string_t pattern = safeMid(text, patch.start2, patch.length1);
|
1746
|
+
int padding = 0;
|
1747
|
+
|
1748
|
+
// Look for the first and last matches of pattern in text. If two different
|
1749
|
+
// matches are found, increase the pattern length.
|
1750
|
+
while (text.find(pattern) != text.rfind(pattern)
|
1751
|
+
&& (int)pattern.length() < Match_MaxBits - Patch_Margin - Patch_Margin) {
|
1752
|
+
padding += Patch_Margin;
|
1753
|
+
pattern = safeMid(text, std::max(0, patch.start2 - padding),
|
1754
|
+
std::min((int)text.length(), patch.start2 + patch.length1 + padding)
|
1755
|
+
- std::max(0, patch.start2 - padding));
|
1756
|
+
}
|
1757
|
+
// Add one chunk for good luck.
|
1758
|
+
padding += Patch_Margin;
|
1759
|
+
|
1760
|
+
// Add the prefix.
|
1761
|
+
string_t prefix = safeMid(text, std::max(0, patch.start2 - padding),
|
1762
|
+
patch.start2 - std::max(0, patch.start2 - padding));
|
1763
|
+
if (!prefix.empty()) {
|
1764
|
+
patch.diffs.push_front(Diff(EQUAL, prefix));
|
1765
|
+
}
|
1766
|
+
// Add the suffix.
|
1767
|
+
string_t suffix = safeMid(text, patch.start2 + patch.length1,
|
1768
|
+
std::min((int)text.length(), patch.start2 + patch.length1 + padding)
|
1769
|
+
- (patch.start2 + patch.length1));
|
1770
|
+
if (!suffix.empty()) {
|
1771
|
+
patch.diffs.push_back(Diff(EQUAL, suffix));
|
1772
|
+
}
|
1773
|
+
|
1774
|
+
// Roll back the start points.
|
1775
|
+
patch.start1 -= prefix.length();
|
1776
|
+
patch.start2 -= prefix.length();
|
1777
|
+
// Extend the lengths.
|
1778
|
+
patch.length1 += prefix.length() + suffix.length();
|
1779
|
+
patch.length2 += prefix.length() + suffix.length();
|
1780
|
+
}
|
1781
|
+
|
1782
|
+
/**
|
1783
|
+
* Compute a list of patches to turn text1 into text2.
|
1784
|
+
* A set of diffs will be computed.
|
1785
|
+
* @param text1 Old text.
|
1786
|
+
* @param text2 New text.
|
1787
|
+
* @return LinkedList of Patch objects.
|
1788
|
+
*/
|
1789
|
+
public:
|
1790
|
+
Patches patch_make(const string_t &text1, const string_t &text2) const {
|
1791
|
+
// No diffs provided, compute our own.
|
1792
|
+
Diffs diffs = diff_main(text1, text2, true);
|
1793
|
+
if (diffs.size() > 2) {
|
1794
|
+
diff_cleanupSemantic(diffs);
|
1795
|
+
diff_cleanupEfficiency(diffs);
|
1796
|
+
}
|
1797
|
+
|
1798
|
+
return patch_make(text1, diffs);
|
1799
|
+
}
|
1800
|
+
|
1801
|
+
/**
|
1802
|
+
* Compute a list of patches to turn text1 into text2.
|
1803
|
+
* text1 will be derived from the provided diffs.
|
1804
|
+
* @param diffs Array of diff tuples for text1 to text2.
|
1805
|
+
* @return LinkedList of Patch objects.
|
1806
|
+
*/
|
1807
|
+
public:
|
1808
|
+
Patches patch_make(const Diffs &diffs) const {
|
1809
|
+
// No origin string provided, compute our own.
|
1810
|
+
return patch_make(diff_text1(diffs), diffs);
|
1811
|
+
}
|
1812
|
+
|
1813
|
+
/**
|
1814
|
+
* Compute a list of patches to turn text1 into text2.
|
1815
|
+
* text2 is ignored, diffs are the delta between text1 and text2.
|
1816
|
+
* @param text1 Old text.
|
1817
|
+
* @param text2 Ignored.
|
1818
|
+
* @param diffs Array of diff tuples for text1 to text2.
|
1819
|
+
* @return LinkedList of Patch objects.
|
1820
|
+
* @deprecated Prefer patch_make(const string_t &text1, const Diffs &diffs).
|
1821
|
+
*/
|
1822
|
+
public:
|
1823
|
+
Patches patch_make(const string_t &text1, const string_t &/*text2*/, const Diffs &diffs) const {
|
1824
|
+
return patch_make(text1, diffs); // text2 is entirely unused.
|
1825
|
+
}
|
1826
|
+
|
1827
|
+
/**
|
1828
|
+
* Compute a list of patches to turn text1 into text2.
|
1829
|
+
* text2 is not provided, diffs are the delta between text1 and text2.
|
1830
|
+
* @param text1 Old text.
|
1831
|
+
* @param diffs Array of diff tuples for text1 to text2.
|
1832
|
+
* @return LinkedList of Patch objects.
|
1833
|
+
*/
|
1834
|
+
public:
|
1835
|
+
Patches patch_make(const string_t &text1, const Diffs &diffs) const {
|
1836
|
+
Patches patches;
|
1837
|
+
if (!diffs.empty()) { // Get rid of the null case.
|
1838
|
+
Patch patch;
|
1839
|
+
int char_count1 = 0; // Number of characters into the text1 string.
|
1840
|
+
int char_count2 = 0; // Number of characters into the text2 string.
|
1841
|
+
// Start with text1 (prepatch_text) and apply the diffs until we arrive at
|
1842
|
+
// text2 (postpatch_text). We recreate the patches one by one to determine
|
1843
|
+
// context info.
|
1844
|
+
string_t prepatch_text = text1;
|
1845
|
+
string_t postpatch_text = text1;
|
1846
|
+
for (typename Diffs::const_iterator cur_diff = diffs.begin(); cur_diff != diffs.end(); ++cur_diff) {
|
1847
|
+
if (patch.diffs.empty() && (*cur_diff).operation != EQUAL) {
|
1848
|
+
// A new patch starts here.
|
1849
|
+
patch.start1 = char_count1;
|
1850
|
+
patch.start2 = char_count2;
|
1851
|
+
}
|
1852
|
+
|
1853
|
+
switch ((*cur_diff).operation) {
|
1854
|
+
case INSERT:
|
1855
|
+
patch.diffs.push_back(*cur_diff);
|
1856
|
+
patch.length2 += (*cur_diff).text.length();
|
1857
|
+
postpatch_text = postpatch_text.substr(0, char_count2)
|
1858
|
+
+ (*cur_diff).text + safeMid(postpatch_text, char_count2);
|
1859
|
+
break;
|
1860
|
+
case DELETE:
|
1861
|
+
patch.length1 += (*cur_diff).text.length();
|
1862
|
+
patch.diffs.push_back(*cur_diff);
|
1863
|
+
postpatch_text = postpatch_text.substr(0, char_count2)
|
1864
|
+
+ safeMid(postpatch_text, char_count2 + (*cur_diff).text.length());
|
1865
|
+
break;
|
1866
|
+
case EQUAL:
|
1867
|
+
if ((int)(*cur_diff).text.length() <= 2 * Patch_Margin
|
1868
|
+
&& !patch.diffs.empty() && !(*cur_diff == diffs.back())) {
|
1869
|
+
// Small equality inside a patch.
|
1870
|
+
patch.diffs.push_back(*cur_diff);
|
1871
|
+
patch.length1 += (*cur_diff).text.length();
|
1872
|
+
patch.length2 += (*cur_diff).text.length();
|
1873
|
+
}
|
1874
|
+
|
1875
|
+
if ((int)(*cur_diff).text.length() >= 2 * Patch_Margin) {
|
1876
|
+
// Time for a new patch.
|
1877
|
+
if (!patch.diffs.empty()) {
|
1878
|
+
patch_addContext(patch, prepatch_text);
|
1879
|
+
patches.push_back(patch);
|
1880
|
+
patch = Patch();
|
1881
|
+
// Unlike Unidiff, our patch lists have a rolling context.
|
1882
|
+
// http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
|
1883
|
+
// Update prepatch text & pos to reflect the application of the
|
1884
|
+
// just completed patch.
|
1885
|
+
prepatch_text = postpatch_text;
|
1886
|
+
char_count1 = char_count2;
|
1887
|
+
}
|
1888
|
+
}
|
1889
|
+
break;
|
1890
|
+
}
|
1891
|
+
|
1892
|
+
// Update the current character count.
|
1893
|
+
if ((*cur_diff).operation != INSERT) {
|
1894
|
+
char_count1 += (*cur_diff).text.length();
|
1895
|
+
}
|
1896
|
+
if ((*cur_diff).operation != DELETE) {
|
1897
|
+
char_count2 += (*cur_diff).text.length();
|
1898
|
+
}
|
1899
|
+
}
|
1900
|
+
// Pick up the leftover patch if not empty.
|
1901
|
+
if (!patch.diffs.empty()) {
|
1902
|
+
patch_addContext(patch, prepatch_text);
|
1903
|
+
patches.push_back(patch);
|
1904
|
+
}
|
1905
|
+
}
|
1906
|
+
return patches;
|
1907
|
+
}
|
1908
|
+
|
1909
|
+
/**
|
1910
|
+
* Given an array of patches, return another array that is identical.
|
1911
|
+
* @param patches Array of patch objects.
|
1912
|
+
* @return Array of patch objects.
|
1913
|
+
*/
|
1914
|
+
public:
|
1915
|
+
Patches patch_deepCopy(const Patches &patches) const { return patches; }
|
1916
|
+
|
1917
|
+
/**
|
1918
|
+
* Merge a set of patches onto the text. Return a patched text, as well
|
1919
|
+
* as an array of true/false values indicating which patches were applied.
|
1920
|
+
* @param patches Array of patch objects.
|
1921
|
+
* @param text Old text.
|
1922
|
+
* @return Two element Object array, containing the new text and an array of
|
1923
|
+
* boolean values.
|
1924
|
+
*/
|
1925
|
+
public:
|
1926
|
+
std::pair<string_t, std::vector<bool> > patch_apply(const Patches &patches, const string_t &text) const
|
1927
|
+
{ std::pair<string_t, std::vector<bool> > res; patch_apply(patches, text, res); return res; }
|
1928
|
+
void patch_apply(const Patches &patches, const string_t &sourceText, std::pair<string_t, std::vector<bool> >& res) const {
|
1929
|
+
if (patches.empty()) {
|
1930
|
+
res.first = sourceText;
|
1931
|
+
res.second.clear();
|
1932
|
+
return;
|
1933
|
+
}
|
1934
|
+
string_t text = sourceText; // Copy to preserve original.
|
1935
|
+
|
1936
|
+
// Deep copy the patches so that no changes are made to originals.
|
1937
|
+
// Patches patchesCopy = patch_deepCopy(patches);
|
1938
|
+
Patches patchesCopy(patches); // Default copy constructor will do it just fine
|
1939
|
+
|
1940
|
+
string_t nullPadding = patch_addPadding(patchesCopy);
|
1941
|
+
text = nullPadding + text + nullPadding;
|
1942
|
+
patch_splitMax(patchesCopy);
|
1943
|
+
|
1944
|
+
int x = 0;
|
1945
|
+
// delta keeps track of the offset between the expected and actual location
|
1946
|
+
// of the previous patch. If there are patches expected at positions 10 and
|
1947
|
+
// 20, but the first patch was found at 12, delta is 2 and the second patch
|
1948
|
+
// has an effective expected position of 22.
|
1949
|
+
int delta = 0;
|
1950
|
+
std::vector<bool>& results = res.second;
|
1951
|
+
results.resize(patchesCopy.size());
|
1952
|
+
string_t text1, text2;
|
1953
|
+
for (typename Patches::const_iterator cur_patch = patchesCopy.begin(); cur_patch != patchesCopy.end(); ++cur_patch) {
|
1954
|
+
int expected_loc = (*cur_patch).start2 + delta;
|
1955
|
+
text1 = diff_text1((*cur_patch).diffs);
|
1956
|
+
int start_loc;
|
1957
|
+
int end_loc = -1;
|
1958
|
+
if ((int)text1.length() > Match_MaxBits) {
|
1959
|
+
// patch_splitMax will only provide an oversized pattern in the case of
|
1960
|
+
// a monster delete.
|
1961
|
+
start_loc = match_main(text, text1.substr(0, Match_MaxBits), expected_loc);
|
1962
|
+
if (start_loc != -1) {
|
1963
|
+
end_loc = match_main(text, right(text1, Match_MaxBits),
|
1964
|
+
expected_loc + text1.length() - Match_MaxBits);
|
1965
|
+
if (end_loc == -1 || start_loc >= end_loc) {
|
1966
|
+
// Can't find valid trailing context. Drop this patch.
|
1967
|
+
start_loc = -1;
|
1968
|
+
}
|
1969
|
+
}
|
1970
|
+
} else {
|
1971
|
+
start_loc = match_main(text, text1, expected_loc);
|
1972
|
+
}
|
1973
|
+
if (start_loc == -1) {
|
1974
|
+
// No match found. :(
|
1975
|
+
results[x] = false;
|
1976
|
+
// Subtract the delta for this failed patch from subsequent patches.
|
1977
|
+
delta -= (*cur_patch).length2 - (*cur_patch).length1;
|
1978
|
+
} else {
|
1979
|
+
// Found a match. :)
|
1980
|
+
results[x] = true;
|
1981
|
+
delta = start_loc - expected_loc;
|
1982
|
+
if (end_loc == -1) {
|
1983
|
+
text2 = safeMid(text, start_loc, text1.length());
|
1984
|
+
} else {
|
1985
|
+
text2 = safeMid(text, start_loc, end_loc + Match_MaxBits - start_loc);
|
1986
|
+
}
|
1987
|
+
if (text1 == text2) {
|
1988
|
+
// Perfect match, just shove the replacement text in.
|
1989
|
+
text = text.substr(0, start_loc) + diff_text2((*cur_patch).diffs) + safeMid(text, start_loc + text1.length());
|
1990
|
+
} else {
|
1991
|
+
// Imperfect match. Run a diff to get a framework of equivalent
|
1992
|
+
// indices.
|
1993
|
+
Diffs diffs = diff_main(text1, text2, false);
|
1994
|
+
if ((int)text1.length() > Match_MaxBits
|
1995
|
+
&& diff_levenshtein(diffs) / static_cast<float> (text1.length())
|
1996
|
+
> Patch_DeleteThreshold) {
|
1997
|
+
// The end points match, but the content is unacceptably bad.
|
1998
|
+
results[x] = false;
|
1999
|
+
} else {
|
2000
|
+
diff_cleanupSemanticLossless(diffs);
|
2001
|
+
int index1 = 0;
|
2002
|
+
for (typename Diffs::const_iterator cur_diff = (*cur_patch).diffs.begin(); cur_diff != (*cur_patch).diffs.end(); ++cur_diff) {
|
2003
|
+
if ((*cur_diff).operation != EQUAL) {
|
2004
|
+
int index2 = diff_xIndex(diffs, index1);
|
2005
|
+
if ((*cur_diff).operation == INSERT) {
|
2006
|
+
// Insertion
|
2007
|
+
text = text.substr(0, start_loc + index2) + (*cur_diff).text
|
2008
|
+
+ safeMid(text, start_loc + index2);
|
2009
|
+
} else if ((*cur_diff).operation == DELETE) {
|
2010
|
+
// Deletion
|
2011
|
+
text = text.substr(0, start_loc + index2)
|
2012
|
+
+ safeMid(text, start_loc + diff_xIndex(diffs,
|
2013
|
+
index1 + (*cur_diff).text.length()));
|
2014
|
+
}
|
2015
|
+
}
|
2016
|
+
if ((*cur_diff).operation != DELETE) {
|
2017
|
+
index1 += (*cur_diff).text.length();
|
2018
|
+
}
|
2019
|
+
}
|
2020
|
+
}
|
2021
|
+
}
|
2022
|
+
}
|
2023
|
+
x++;
|
2024
|
+
}
|
2025
|
+
// Strip the padding off.
|
2026
|
+
res.first = safeMid(text, nullPadding.length(), text.length() - 2 * nullPadding.length());
|
2027
|
+
}
|
2028
|
+
|
2029
|
+
/**
|
2030
|
+
* Add some padding on text start and end so that edges can match something.
|
2031
|
+
* Intended to be called only from within patch_apply.
|
2032
|
+
* @param patches Array of patch objects.
|
2033
|
+
* @return The padding string added to each side.
|
2034
|
+
*/
|
2035
|
+
public:
|
2036
|
+
string_t patch_addPadding(Patches &patches) const {
|
2037
|
+
short paddingLength = Patch_Margin;
|
2038
|
+
string_t nullPadding;
|
2039
|
+
for (short x = 1; x <= paddingLength; x++) {
|
2040
|
+
nullPadding += (char_t)x;
|
2041
|
+
}
|
2042
|
+
|
2043
|
+
// Bump all the patches forward.
|
2044
|
+
for (typename Patches::iterator cur_patch = patches.begin(); cur_patch != patches.end(); ++cur_patch) {
|
2045
|
+
(*cur_patch).start1 += paddingLength;
|
2046
|
+
(*cur_patch).start2 += paddingLength;
|
2047
|
+
}
|
2048
|
+
|
2049
|
+
// Add some padding on start of first diff.
|
2050
|
+
Patch &firstPatch = patches.front();
|
2051
|
+
Diffs &firstPatchDiffs = firstPatch.diffs;
|
2052
|
+
if (firstPatchDiffs.empty() || firstPatchDiffs.front().operation != EQUAL) {
|
2053
|
+
// Add nullPadding equality.
|
2054
|
+
firstPatchDiffs.push_front(Diff(EQUAL, nullPadding));
|
2055
|
+
firstPatch.start1 -= paddingLength; // Should be 0.
|
2056
|
+
firstPatch.start2 -= paddingLength; // Should be 0.
|
2057
|
+
firstPatch.length1 += paddingLength;
|
2058
|
+
firstPatch.length2 += paddingLength;
|
2059
|
+
} else if (paddingLength > (int)firstPatchDiffs.front().text.length()) {
|
2060
|
+
// Grow first equality.
|
2061
|
+
Diff &firstDiff = firstPatchDiffs.front();
|
2062
|
+
int extraLength = paddingLength - firstDiff.text.length();
|
2063
|
+
firstDiff.text = safeMid(nullPadding, firstDiff.text.length(),
|
2064
|
+
paddingLength - firstDiff.text.length()) + firstDiff.text;
|
2065
|
+
firstPatch.start1 -= extraLength;
|
2066
|
+
firstPatch.start2 -= extraLength;
|
2067
|
+
firstPatch.length1 += extraLength;
|
2068
|
+
firstPatch.length2 += extraLength;
|
2069
|
+
}
|
2070
|
+
|
2071
|
+
// Add some padding on end of last diff.
|
2072
|
+
Patch &lastPatch = patches.front();
|
2073
|
+
Diffs &lastPatchDiffs = lastPatch.diffs;
|
2074
|
+
if (lastPatchDiffs.empty() || lastPatchDiffs.back().operation != EQUAL) {
|
2075
|
+
// Add nullPadding equality.
|
2076
|
+
lastPatchDiffs.push_back(Diff(EQUAL, nullPadding));
|
2077
|
+
lastPatch.length1 += paddingLength;
|
2078
|
+
lastPatch.length2 += paddingLength;
|
2079
|
+
} else if (paddingLength > (int)lastPatchDiffs.back().text.length()) {
|
2080
|
+
// Grow last equality.
|
2081
|
+
Diff &lastDiff = lastPatchDiffs.back();
|
2082
|
+
int extraLength = paddingLength - lastDiff.text.length();
|
2083
|
+
lastDiff.text += nullPadding.substr(0, extraLength);
|
2084
|
+
lastPatch.length1 += extraLength;
|
2085
|
+
lastPatch.length2 += extraLength;
|
2086
|
+
}
|
2087
|
+
|
2088
|
+
return nullPadding;
|
2089
|
+
}
|
2090
|
+
|
2091
|
+
/**
|
2092
|
+
* Look through the patches and break up any which are longer than the
|
2093
|
+
* maximum limit of the match algorithm.
|
2094
|
+
* Intended to be called only from within patch_apply.
|
2095
|
+
* @param patches LinkedList of Patch objects.
|
2096
|
+
*/
|
2097
|
+
public:
|
2098
|
+
void patch_splitMax(Patches &patches) const {
|
2099
|
+
short patch_size = Match_MaxBits;
|
2100
|
+
string_t precontext, postcontext;
|
2101
|
+
Patch patch;
|
2102
|
+
int start1, start2;
|
2103
|
+
bool empty;
|
2104
|
+
Operation diff_type;
|
2105
|
+
string_t diff_text;
|
2106
|
+
Patch bigpatch;
|
2107
|
+
|
2108
|
+
for (typename Patches::iterator cur_patch = patches.begin(); cur_patch != patches.end();) {
|
2109
|
+
if ((*cur_patch).length1 <= patch_size) { ++cur_patch; continue; }
|
2110
|
+
bigpatch = *cur_patch;
|
2111
|
+
// Remove the big old patch.
|
2112
|
+
cur_patch = patches.erase(cur_patch);
|
2113
|
+
start1 = bigpatch.start1;
|
2114
|
+
start2 = bigpatch.start2;
|
2115
|
+
precontext.clear();
|
2116
|
+
while (!bigpatch.diffs.empty()) {
|
2117
|
+
// Create one of several smaller patches.
|
2118
|
+
patch = Patch();
|
2119
|
+
empty = true;
|
2120
|
+
patch.start1 = start1 - precontext.length();
|
2121
|
+
patch.start2 = start2 - precontext.length();
|
2122
|
+
if (!precontext.empty()) {
|
2123
|
+
patch.length1 = patch.length2 = precontext.length();
|
2124
|
+
patch.diffs.push_back(Diff(EQUAL, precontext));
|
2125
|
+
}
|
2126
|
+
while (!bigpatch.diffs.empty()
|
2127
|
+
&& patch.length1 < patch_size - Patch_Margin) {
|
2128
|
+
diff_type = bigpatch.diffs.front().operation;
|
2129
|
+
diff_text = bigpatch.diffs.front().text;
|
2130
|
+
if (diff_type == INSERT) {
|
2131
|
+
// Insertions are harmless.
|
2132
|
+
patch.length2 += diff_text.length();
|
2133
|
+
start2 += diff_text.length();
|
2134
|
+
patch.diffs.push_back(bigpatch.diffs.front());
|
2135
|
+
bigpatch.diffs.pop_front();
|
2136
|
+
empty = false;
|
2137
|
+
} else if (diff_type == DELETE && patch.diffs.size() == 1
|
2138
|
+
&& patch.diffs.front().operation == EQUAL
|
2139
|
+
&& (int)diff_text.length() > 2 * patch_size) {
|
2140
|
+
// This is a large deletion. Let it pass in one chunk.
|
2141
|
+
patch.length1 += diff_text.length();
|
2142
|
+
start1 += diff_text.length();
|
2143
|
+
empty = false;
|
2144
|
+
patch.diffs.push_back(Diff(diff_type, diff_text));
|
2145
|
+
bigpatch.diffs.pop_front();
|
2146
|
+
} else {
|
2147
|
+
// Deletion or equality. Only take as much as we can stomach.
|
2148
|
+
diff_text = diff_text.substr(0, std::min((int)diff_text.length(),
|
2149
|
+
patch_size - patch.length1 - Patch_Margin));
|
2150
|
+
patch.length1 += diff_text.length();
|
2151
|
+
start1 += diff_text.length();
|
2152
|
+
if (diff_type == EQUAL) {
|
2153
|
+
patch.length2 += diff_text.length();
|
2154
|
+
start2 += diff_text.length();
|
2155
|
+
} else {
|
2156
|
+
empty = false;
|
2157
|
+
}
|
2158
|
+
patch.diffs.push_back(Diff(diff_type, diff_text));
|
2159
|
+
if (diff_text == bigpatch.diffs.front().text) {
|
2160
|
+
bigpatch.diffs.pop_front();
|
2161
|
+
} else {
|
2162
|
+
bigpatch.diffs.front().text = safeMid(bigpatch.diffs.front().text, diff_text.length());
|
2163
|
+
}
|
2164
|
+
}
|
2165
|
+
}
|
2166
|
+
// Compute the head context for the next patch.
|
2167
|
+
precontext = safeMid(diff_text2(patch.diffs), std::max(0, (int)precontext.length() - Patch_Margin));
|
2168
|
+
// Append the end context for this patch.
|
2169
|
+
postcontext = diff_text1(bigpatch.diffs);
|
2170
|
+
if ((int)postcontext.length() > Patch_Margin) {
|
2171
|
+
postcontext = postcontext.substr(0, Patch_Margin);
|
2172
|
+
}
|
2173
|
+
if (!postcontext.empty()) {
|
2174
|
+
patch.length1 += postcontext.length();
|
2175
|
+
patch.length2 += postcontext.length();
|
2176
|
+
if (!patch.diffs.empty()
|
2177
|
+
&& patch.diffs.back().operation == EQUAL) {
|
2178
|
+
patch.diffs.back().text += postcontext;
|
2179
|
+
} else {
|
2180
|
+
patch.diffs.push_back(Diff(EQUAL, postcontext));
|
2181
|
+
}
|
2182
|
+
}
|
2183
|
+
if (!empty) {
|
2184
|
+
patches.insert(cur_patch, patch);
|
2185
|
+
}
|
2186
|
+
}
|
2187
|
+
}
|
2188
|
+
}
|
2189
|
+
|
2190
|
+
/**
|
2191
|
+
* Take a list of patches and return a textual representation.
|
2192
|
+
* @param patches List of Patch objects.
|
2193
|
+
* @return Text representation of patches.
|
2194
|
+
*/
|
2195
|
+
public:
|
2196
|
+
static string_t patch_toText(const Patches &patches) {
|
2197
|
+
string_t text;
|
2198
|
+
for (typename Patches::const_iterator cur_patch = patches.begin(); cur_patch != patches.end(); ++cur_patch) {
|
2199
|
+
text += (*cur_patch).toString();
|
2200
|
+
}
|
2201
|
+
return text;
|
2202
|
+
}
|
2203
|
+
|
2204
|
+
/**
|
2205
|
+
* Parse a textual representation of patches and return a List of Patch
|
2206
|
+
* objects.
|
2207
|
+
* @param textline Text representation of patches.
|
2208
|
+
* @return List of Patch objects.
|
2209
|
+
* @throws string_t If invalid input.
|
2210
|
+
*/
|
2211
|
+
public:
|
2212
|
+
Patches patch_fromText(const string_t &textline) const {
|
2213
|
+
Patches patches;
|
2214
|
+
if (!textline.empty()) {
|
2215
|
+
char_t sign;
|
2216
|
+
string_t line;
|
2217
|
+
typename string_t::const_pointer text = textline.c_str();
|
2218
|
+
typename string_t::size_type text_len, l;
|
2219
|
+
while (text - textline.c_str() < (int)textline.length()) {
|
2220
|
+
if ((text_len = next_token(textline, traits::from_wchar(L'\n'), text)) == 0) { ++text; continue; }
|
2221
|
+
|
2222
|
+
// A replacement for the regexp "^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$" exact match
|
2223
|
+
string_t start1, length1, start2, length2;
|
2224
|
+
do {
|
2225
|
+
typename string_t::const_pointer t = text;
|
2226
|
+
l = text_len;
|
2227
|
+
if ((l -= 9) > 0 && traits::to_wchar(*t) == L'@' && traits::to_wchar(*++t) == L'@'
|
2228
|
+
&& traits::to_wchar(*++t) == L' ' && traits::to_wchar(*++t) == L'-' && traits::is_digit(*++t)) {
|
2229
|
+
do { start1 += *t; } while (--l > 0 && traits::is_digit(*++t));
|
2230
|
+
if (l > 0 && traits::to_wchar(*t) == L',') ++t, --l;
|
2231
|
+
while (l > 0 && traits::is_digit(*t)) --l, length1 += *t++;
|
2232
|
+
if (l > 0 && traits::to_wchar(*t++) == L' ' && traits::to_wchar(*t++) == L'+' && traits::is_digit(*t)) {
|
2233
|
+
do { start2 += *t; } while (--l >= 0 && traits::is_digit(*++t));
|
2234
|
+
if (l > 0 && traits::to_wchar(*t) == L',') ++t, --l;
|
2235
|
+
while (l > 0 && traits::is_digit(*t)) --l, length2 += *t++;
|
2236
|
+
if (l == 0 && traits::to_wchar(*t++) == L' ' && traits::to_wchar(*t++) == L'@' && traits::to_wchar(*t) == L'@') break; // Success
|
2237
|
+
}
|
2238
|
+
}
|
2239
|
+
throw string_t(traits::cs(L"Invalid patch string: ") + string_t(text, text_len));
|
2240
|
+
} while (false);
|
2241
|
+
|
2242
|
+
Patch patch;
|
2243
|
+
patch.start1 = to_int(start1);
|
2244
|
+
if (length1.empty()) {
|
2245
|
+
patch.start1--;
|
2246
|
+
patch.length1 = 1;
|
2247
|
+
} else if (length1.size() == 1 && traits::to_wchar(length1[0]) == L'0') {
|
2248
|
+
patch.length1 = 0;
|
2249
|
+
} else {
|
2250
|
+
patch.start1--;
|
2251
|
+
patch.length1 = to_int(length1);
|
2252
|
+
}
|
2253
|
+
|
2254
|
+
patch.start2 = to_int(start2);
|
2255
|
+
if (length2.empty()) {
|
2256
|
+
patch.start2--;
|
2257
|
+
patch.length2 = 1;
|
2258
|
+
} else if (length2.size() == 1 && traits::to_wchar(length2[0]) == L'0') {
|
2259
|
+
patch.length2 = 0;
|
2260
|
+
} else {
|
2261
|
+
patch.start2--;
|
2262
|
+
patch.length2 = to_int(length2);
|
2263
|
+
}
|
2264
|
+
|
2265
|
+
for (text += text_len + 1; text - textline.c_str() < (int)textline.length(); text += text_len + 1) {
|
2266
|
+
if ((text_len = next_token(textline, traits::from_wchar(L'\n'), text)) == 0) continue;
|
2267
|
+
|
2268
|
+
sign = *text;
|
2269
|
+
line.assign(text + 1, text_len - 1);
|
2270
|
+
percent_decode(line);
|
2271
|
+
switch (traits::to_wchar(sign)) {
|
2272
|
+
case L'-':
|
2273
|
+
// Deletion.
|
2274
|
+
patch.diffs.push_back(Diff(DELETE, line));
|
2275
|
+
continue;
|
2276
|
+
case L'+':
|
2277
|
+
// Insertion.
|
2278
|
+
patch.diffs.push_back(Diff(INSERT, line));
|
2279
|
+
continue;
|
2280
|
+
case L' ':
|
2281
|
+
// Minor equality.
|
2282
|
+
patch.diffs.push_back(Diff(EQUAL, line));
|
2283
|
+
continue;
|
2284
|
+
case L'@':
|
2285
|
+
// Start of next patch.
|
2286
|
+
break;
|
2287
|
+
default:
|
2288
|
+
// WTF?
|
2289
|
+
throw string_t(traits::cs(L"Invalid patch mode '") + (sign + (traits::cs(L"' in: ") + line)));
|
2290
|
+
}
|
2291
|
+
break;
|
2292
|
+
}
|
2293
|
+
|
2294
|
+
patches.push_back(patch);
|
2295
|
+
}
|
2296
|
+
}
|
2297
|
+
return patches;
|
2298
|
+
}
|
2299
|
+
|
2300
|
+
/**
|
2301
|
+
* A safer version of string_t.mid(pos). This one returns "" instead of
|
2302
|
+
* null when the postion equals the string length.
|
2303
|
+
* @param str String to take a substring from.
|
2304
|
+
* @param pos Position to start the substring from.
|
2305
|
+
* @return Substring.
|
2306
|
+
*/
|
2307
|
+
private:
|
2308
|
+
static inline string_t safeMid(const string_t &str, int pos) {
|
2309
|
+
return ((size_t)pos == str.length()) ? string_t() : str.substr(pos);
|
2310
|
+
}
|
2311
|
+
|
2312
|
+
/**
|
2313
|
+
* A safer version of string_t.mid(pos, len). This one returns "" instead of
|
2314
|
+
* null when the postion equals the string length.
|
2315
|
+
* @param str String to take a substring from.
|
2316
|
+
* @param pos Position to start the substring from.
|
2317
|
+
* @param len Length of substring.
|
2318
|
+
* @return Substring.
|
2319
|
+
*/
|
2320
|
+
private:
|
2321
|
+
static inline string_t safeMid(const string_t &str, int pos, int len) {
|
2322
|
+
return ((size_t)pos == str.length()) ? string_t() : str.substr(pos, len);
|
2323
|
+
}
|
2324
|
+
|
2325
|
+
/**
|
2326
|
+
* Utility functions
|
2327
|
+
*/
|
2328
|
+
private:
|
2329
|
+
static string_t to_string(int n) {
|
2330
|
+
string_t str;
|
2331
|
+
bool negative = false;
|
2332
|
+
size_t l = 0;
|
2333
|
+
if (n < 0) n = -n, ++l, negative = true;
|
2334
|
+
int n_ = n; do { ++l; } while ((n_ /= 10) > 0);
|
2335
|
+
str.resize(l);
|
2336
|
+
typename string_t::iterator s = str.end();
|
2337
|
+
const wchar_t digits[] = L"0123456789";
|
2338
|
+
do { *--s = traits::from_wchar(digits[n % 10]); } while ((n /= 10) > 0);
|
2339
|
+
if (negative) *--s = traits::from_wchar(L'-');
|
2340
|
+
return str;
|
2341
|
+
}
|
2342
|
+
|
2343
|
+
static int to_int(const string_t& str) { return traits::to_int(str.c_str()); }
|
2344
|
+
|
2345
|
+
static bool is_control(char_t c) { switch (traits::to_wchar(c)) { case L'\n': case L'\r': return true; } return false; }
|
2346
|
+
|
2347
|
+
static typename string_t::size_type next_token(const string_t& str, char_t delim, typename string_t::const_pointer off) {
|
2348
|
+
typename string_t::const_pointer p = off, end = str.c_str() + str.length();
|
2349
|
+
for (; p != end; ++p) if (*p == delim) break;
|
2350
|
+
return p - off;
|
2351
|
+
}
|
2352
|
+
|
2353
|
+
static void append_percent_encoded(string_t& s1, const string_t& s2) {
|
2354
|
+
const wchar_t safe_chars[] = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~ !*'();/?:@&=+$,#";
|
2355
|
+
|
2356
|
+
size_t safe[0x100], i;
|
2357
|
+
for (i = 0; i < 0x100; ++i) safe[i] = 0;
|
2358
|
+
for (i = 0; i < sizeof(safe_chars) / sizeof(wchar_t); ++i) safe[safe_chars[i]] = i + 1;
|
2359
|
+
|
2360
|
+
size_t n = 0;
|
2361
|
+
typename traits::utf32_t u;
|
2362
|
+
typename string_t::const_pointer c = s2.c_str(), end = c + s2.length();
|
2363
|
+
while (c != end) {
|
2364
|
+
c = traits::to_utf32(c, end, u);
|
2365
|
+
n += u >= 0x10000? 12 : u >= 0x800? 9 : u >= 0x80? 6 : safe[static_cast<unsigned char>(u)]? 1 : 3;
|
2366
|
+
}
|
2367
|
+
if (n == s2.length())
|
2368
|
+
s1.append(s2);
|
2369
|
+
else {
|
2370
|
+
s1.reserve(s1.size() + n);
|
2371
|
+
// Encode as UTF-8, then escape unsafe characters
|
2372
|
+
unsigned char utf8[4];
|
2373
|
+
for (c = s2.c_str(); c != end;) {
|
2374
|
+
c = traits::to_utf32(c, end, u);
|
2375
|
+
unsigned char* pt = utf8;
|
2376
|
+
if (u < 0x80)
|
2377
|
+
*pt++ = (unsigned char)u;
|
2378
|
+
else if (u < 0x800) {
|
2379
|
+
*pt++ = (unsigned char)((u >> 6) | 0xC0);
|
2380
|
+
*pt++ = (unsigned char)((u & 0x3F) | 0x80);
|
2381
|
+
}
|
2382
|
+
else if (u < 0x10000) {
|
2383
|
+
*pt++ = (unsigned char)((u >> 12) | 0xE0);
|
2384
|
+
*pt++ = (unsigned char)(((u >> 6) & 0x3F) | 0x80);
|
2385
|
+
*pt++ = (unsigned char)((u & 0x3F) | 0x80);
|
2386
|
+
}
|
2387
|
+
else {
|
2388
|
+
*pt++ = (unsigned char)((u >> 18) | 0xF0);
|
2389
|
+
*pt++ = (unsigned char)(((u >> 12) & 0x3F) | 0x80);
|
2390
|
+
*pt++ = (unsigned char)(((u >> 6) & 0x3F) | 0x80);
|
2391
|
+
*pt++ = (unsigned char)((u & 0x3F) | 0x80);
|
2392
|
+
}
|
2393
|
+
|
2394
|
+
for (const unsigned char* p = utf8; p < pt; ++p)
|
2395
|
+
if (safe[*p])
|
2396
|
+
s1 += traits::from_wchar(safe_chars[safe[*p] - 1]);
|
2397
|
+
else {
|
2398
|
+
s1 += traits::from_wchar(L'%');
|
2399
|
+
s1 += traits::from_wchar(safe_chars[(*p & 0xF0) >> 4]);
|
2400
|
+
s1 += traits::from_wchar(safe_chars[*p & 0xF]);
|
2401
|
+
}
|
2402
|
+
}
|
2403
|
+
}
|
2404
|
+
}
|
2405
|
+
|
2406
|
+
static unsigned hex_digit_value(char_t c) {
|
2407
|
+
switch (traits::to_wchar(c))
|
2408
|
+
{
|
2409
|
+
case L'0': return 0;
|
2410
|
+
case L'1': return 1;
|
2411
|
+
case L'2': return 2;
|
2412
|
+
case L'3': return 3;
|
2413
|
+
case L'4': return 4;
|
2414
|
+
case L'5': return 5;
|
2415
|
+
case L'6': return 6;
|
2416
|
+
case L'7': return 7;
|
2417
|
+
case L'8': return 8;
|
2418
|
+
case L'9': return 9;
|
2419
|
+
case L'A': case L'a': return 0xA;
|
2420
|
+
case L'B': case L'b': return 0xB;
|
2421
|
+
case L'C': case L'c': return 0xC;
|
2422
|
+
case L'D': case L'd': return 0xD;
|
2423
|
+
case L'E': case L'e': return 0xE;
|
2424
|
+
case L'F': case L'f': return 0xF;
|
2425
|
+
}
|
2426
|
+
throw string_t(string_t(traits::cs(L"Invalid character: ")) + c);
|
2427
|
+
}
|
2428
|
+
|
2429
|
+
static void percent_decode(string_t& str) {
|
2430
|
+
typename string_t::iterator s2 = str.begin(), s3 = s2, s4 = s2;
|
2431
|
+
for (typename string_t::const_pointer s1 = str.c_str(), end = s1 + str.size(); s1 != end; ++s1, ++s2)
|
2432
|
+
if (traits::to_wchar(*s1) != L'%')
|
2433
|
+
*s2 = *s1;
|
2434
|
+
else {
|
2435
|
+
char_t d1 = *++s1;
|
2436
|
+
*s2 = char_t((hex_digit_value(d1) << 4) + hex_digit_value(*++s1));
|
2437
|
+
}
|
2438
|
+
// Decode UTF-8 string in-place
|
2439
|
+
while (s3 != s2) {
|
2440
|
+
unsigned u = *s3;
|
2441
|
+
if (u < 0x80)
|
2442
|
+
;
|
2443
|
+
else if ((u >> 5) == 6) {
|
2444
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2445
|
+
u = ((u & 0x1F) << 6) + (*s3 & 0x3F);
|
2446
|
+
}
|
2447
|
+
else if ((u >> 4) == 0xE) {
|
2448
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2449
|
+
u = ((u & 0xF) << 12) + ((*s3 & 0x3F) << 6);
|
2450
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2451
|
+
u += *s3 & 0x3F;
|
2452
|
+
}
|
2453
|
+
else if ((u >> 3) == 0x1E) {
|
2454
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2455
|
+
u = ((u & 7) << 18) + ((*s3 & 0x3F) << 12);
|
2456
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2457
|
+
u += (*s3 & 0x3F) << 6;
|
2458
|
+
if (++s3 == s2 || (*s3 & 0xC0) != 0x80) continue;
|
2459
|
+
u += *s3 & 0x3F;
|
2460
|
+
}
|
2461
|
+
else {
|
2462
|
+
++s3;
|
2463
|
+
continue;
|
2464
|
+
}
|
2465
|
+
s4 = traits::from_utf32(u, s4);
|
2466
|
+
++s3;
|
2467
|
+
}
|
2468
|
+
if (s4 != str.end()) str.resize(s4 - str.begin());
|
2469
|
+
}
|
2470
|
+
|
2471
|
+
static string_t right(const string_t& str, typename string_t::size_type n) { return str.substr(str.size() - n); }
|
2472
|
+
};
|
2473
|
+
|
2474
|
+
|
2475
|
+
/**
|
2476
|
+
* Functions dependent on character type
|
2477
|
+
*/
|
2478
|
+
|
2479
|
+
// Unicode helpers
|
2480
|
+
template <class char_t, class utf32_type = unsigned>
|
2481
|
+
struct diff_match_patch_utf32_direct {
|
2482
|
+
typedef utf32_type utf32_t;
|
2483
|
+
template <class iterator> static iterator to_utf32(iterator i, iterator end, utf32_t& u)
|
2484
|
+
{
|
2485
|
+
u = *i++;
|
2486
|
+
return i;
|
2487
|
+
}
|
2488
|
+
template <class iterator> static iterator from_utf32(utf32_t u, iterator o)
|
2489
|
+
{
|
2490
|
+
*o++ = static_cast<char_t>(u);
|
2491
|
+
return o;
|
2492
|
+
}
|
2493
|
+
};
|
2494
|
+
|
2495
|
+
template <class char_t, class utf32_type = unsigned>
|
2496
|
+
struct diff_match_patch_utf32_from_utf16 {
|
2497
|
+
typedef utf32_type utf32_t;
|
2498
|
+
static const unsigned UTF16_SURROGATE_MIN = 0xD800u, UTF16_SURROGATE_MAX = 0xDFFFu, UTF16_HIGH_SURROGATE_MAX = 0xDBFFu, UTF16_LOW_SURROGATE_MIN = 0xDC00u;
|
2499
|
+
static const unsigned UTF16_SURROGATE_OFFSET = (UTF16_SURROGATE_MIN << 10) + UTF16_HIGH_SURROGATE_MAX - 0xFFFFu;
|
2500
|
+
template <class iterator> static iterator to_utf32(iterator i, iterator end, utf32_t& u)
|
2501
|
+
{
|
2502
|
+
u = *i++;
|
2503
|
+
if (UTF16_SURROGATE_MIN <= u && u <= UTF16_HIGH_SURROGATE_MAX && i != end)
|
2504
|
+
u = (u << 10) + *i++ - UTF16_SURROGATE_OFFSET; // Assume it is a UTF-16 surrogate pair
|
2505
|
+
return i;
|
2506
|
+
}
|
2507
|
+
template <class iterator> static iterator from_utf32(utf32_t u, iterator o)
|
2508
|
+
{
|
2509
|
+
if (u > 0xFFFF) { // Encode code points that do not fit in char_t as UTF-16 surrogate pairs
|
2510
|
+
*o++ = static_cast<char_t>((u >> 10) + UTF16_SURROGATE_MIN - (0x10000 >> 10));
|
2511
|
+
*o++ = static_cast<char_t>((u & 0x3FF) + UTF16_LOW_SURROGATE_MIN);
|
2512
|
+
}
|
2513
|
+
else
|
2514
|
+
*o++ = static_cast<char_t>(u);
|
2515
|
+
return o;
|
2516
|
+
}
|
2517
|
+
};
|
2518
|
+
|
2519
|
+
// Specialization of the traits for wchar_t
|
2520
|
+
#include <cwctype>
|
2521
|
+
template <> struct diff_match_patch_traits<wchar_t> : diff_match_patch_utf32_from_utf16<wchar_t> {
|
2522
|
+
static bool is_alnum(wchar_t c) { return std::iswalnum(c)? true : false; }
|
2523
|
+
static bool is_digit(wchar_t c) { return std::iswdigit(c)? true : false; }
|
2524
|
+
static bool is_space(wchar_t c) { return std::iswspace(c)? true : false; }
|
2525
|
+
static int to_int(const wchar_t* s) { return static_cast<int>(std::wcstol(s, NULL, 10)); }
|
2526
|
+
static wchar_t from_wchar(wchar_t c) { return c; }
|
2527
|
+
static wchar_t to_wchar(wchar_t c) { return c; }
|
2528
|
+
static const wchar_t* cs(const wchar_t* s) { return s; }
|
2529
|
+
};
|
2530
|
+
|
2531
|
+
|
2532
|
+
// Possible specialization of the traits for char
|
2533
|
+
#include <cctype>
|
2534
|
+
template <> struct diff_match_patch_traits<char> : diff_match_patch_utf32_direct<char> {
|
2535
|
+
static bool is_alnum(char c) { return std::isalnum(c)? true : false; }
|
2536
|
+
static bool is_digit(char c) { return std::isdigit(c)? true : false; }
|
2537
|
+
static bool is_space(char c) { return std::isspace(c)? true : false; }
|
2538
|
+
static int to_int(const char* s) { return std::atoi(s); }
|
2539
|
+
static char from_wchar(wchar_t c) { return static_cast<char>(c); }
|
2540
|
+
static wchar_t to_wchar(char c) { return static_cast<wchar_t>(c); }
|
2541
|
+
static std::string cs(const wchar_t* s) { return std::string(s, s + wcslen(s)); }
|
2542
|
+
};
|
2543
|
+
|
2544
|
+
|
2545
|
+
#endif // DIFF_MATCH_PATCH_H
|