Skip to content

Commit f914d72

Browse files
author
Neil Fraser
committed
Fix issue with line-mode speedup.
Fixes issue google#11 for JavaScript. Other languages to follow.
1 parent e1942d8 commit f914d72

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

javascript/diff_match_patch.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/diff_match_patch_uncompressed.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,22 @@ diff_match_patch.prototype.diff_linesToChars_ = function(text1, text2) {
470470
lineEnd = text.length - 1;
471471
}
472472
var line = text.substring(lineStart, lineEnd + 1);
473-
lineStart = lineEnd + 1;
474473

475474
if (lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) :
476475
(lineHash[line] !== undefined)) {
477476
chars += String.fromCharCode(lineHash[line]);
478477
} else {
478+
if (lineArrayLength == 65535) {
479+
// Bail out at 65535 because
480+
// String.fromCharCode(65536) == String.fromCharCode(0)
481+
line = text.substring(lineStart);
482+
lineEnd = text.length;
483+
}
479484
chars += String.fromCharCode(lineArrayLength);
480485
lineHash[line] = lineArrayLength;
481486
lineArray[lineArrayLength++] = line;
482487
}
488+
lineStart = lineEnd + 1;
483489
}
484490
return chars;
485491
}

javascript/tests/diff_match_patch_test.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ function testDiffLinesToChars() {
174174
var n = 300;
175175
var lineList = [];
176176
var charList = [];
177-
for (var x = 1; x < n + 1; x++) {
178-
lineList[x - 1] = x + '\n';
179-
charList[x - 1] = String.fromCharCode(x);
177+
for (var i = 1; i < n + 1; i++) {
178+
lineList[i - 1] = i + '\n';
179+
charList[i - 1] = String.fromCharCode(i);
180180
}
181181
assertEquals(n, lineList.length);
182182
var lines = lineList.join('');
@@ -196,9 +196,9 @@ function testDiffCharsToLines() {
196196
var n = 300;
197197
var lineList = [];
198198
var charList = [];
199-
for (var x = 1; x < n + 1; x++) {
200-
lineList[x - 1] = x + '\n';
201-
charList[x - 1] = String.fromCharCode(x);
199+
for (var i = 1; i < n + 1; i++) {
200+
lineList[i - 1] = i + '\n';
201+
charList[i - 1] = String.fromCharCode(i);
202202
}
203203
assertEquals(n, lineList.length);
204204
var lines = lineList.join('');
@@ -208,6 +208,17 @@ function testDiffCharsToLines() {
208208
var diffs = [[DIFF_DELETE, chars]];
209209
dmp.diff_charsToLines_(diffs, lineList);
210210
assertEquivalent([[DIFF_DELETE, lines]], diffs);
211+
212+
// More than 65536 to verify any 16-bit limitation.
213+
lineList = [];
214+
for (var i = 0; i < 66000; i++) {
215+
lineList[i] = i + '\n';
216+
}
217+
chars = lineList.join('');
218+
var results = dmp.diff_linesToChars_(chars, '');
219+
diffs = [[DIFF_INSERT, results.chars1]];
220+
dmp.diff_charsToLines_(diffs, results.lineArray);
221+
assertEquals(chars, diffs[0][1]);
211222
}
212223

213224
function testDiffCleanupMerge() {
@@ -560,8 +571,8 @@ function testDiffMain() {
560571
var b = 'I am the very model of a modern major general,\nI\'ve information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n';
561572
// Increase the text lengths by 1024 times to ensure a timeout.
562573
for (var x = 0; x < 10; x++) {
563-
a = a + a;
564-
b = b + b;
574+
a += a;
575+
b += b;
565576
}
566577
var startTime = (new Date()).getTime();
567578
dmp.diff_main(a, b);
@@ -571,11 +582,7 @@ function testDiffMain() {
571582
// Test that we didn't take forever (be forgiving).
572583
// Theoretically this test could fail very occasionally if the
573584
// OS task swaps or locks up for a second at the wrong moment.
574-
// ****
575-
// TODO(fraser): For unknown reasons this is taking 500 ms on Google's
576-
// internal test system. Whereas browsers take 140 ms.
577-
//assertTrue(dmp.Diff_Timeout * 1000 * 2 > endTime - startTime);
578-
// ****
585+
assertTrue(dmp.Diff_Timeout * 1000 * 2 > endTime - startTime);
579586
dmp.Diff_Timeout = 0;
580587

581588
// Test the linemode speedup.
@@ -596,7 +603,7 @@ function testDiffMain() {
596603
var texts_linemode = diff_rebuildtexts(dmp.diff_main(a, b, true));
597604
var texts_textmode = diff_rebuildtexts(dmp.diff_main(a, b, false));
598605
assertEquivalent(texts_textmode, texts_linemode);
599-
606+
600607
// Test null inputs.
601608
try {
602609
dmp.diff_main(null, null);

0 commit comments

Comments
 (0)