Skip to content

Commit 52afdf0

Browse files
author
Neil Fraser
committed
Fix issue with line-mode speedup (all language).
Lua not affected. Python 3 unique in having a larger bailout.
1 parent 5869131 commit 52afdf0

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

objectivec/DiffMatchPatchCFUtilities.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,18 @@ CFStringRef diff_linesToCharsMungeCFStringCreate(CFStringRef text, CFMutableArra
480480
}*/
481481

482482
line = diff_CFStringCreateJavaSubstring(text, lineStart, lineEnd + 1);
483-
lineStart = lineEnd + 1;
484483

485484
if (CFDictionaryContainsKey(lineHash, line)) {
486485
CFDictionaryGetValueIfPresent(lineHash, line, (const void **)&hashNumber);
487486
CFNumberGetValue(hashNumber, kCFNumberCFIndexType, &hash);
488487
const UniChar hashChar = (UniChar)hash;
489488
CFStringAppendCharacters(chars, &hashChar, 1);
490489
} else {
490+
if (CFArrayGetCount(lineArray) == 65535) {
491+
// Bail out at 65535 because char 65536 == char 0.
492+
line = diff_CFStringCreateJavaSubstring(text, lineStart, textLength);
493+
lineEnd = textLength;
494+
}
491495
CFArrayAppendValue(lineArray, line);
492496
hash = CFArrayGetCount(lineArray) - 1;
493497
hashNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &hash);
@@ -497,6 +501,7 @@ CFStringRef diff_linesToCharsMungeCFStringCreate(CFStringRef text, CFMutableArra
497501
const UniChar hashChar = (UniChar)hash;
498502
CFStringAppendCharacters(chars, &hashChar, 1);
499503
}
504+
lineStart = lineEnd + 1;
500505

501506
CFRelease(line);
502507
}

objectivec/Tests/DiffMatchPatchTest.m

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ - (void)test_diff_linesToCharsTest {
172172
NSMutableString *lines = [NSMutableString string];
173173
NSMutableString *chars = [NSMutableString string];
174174
NSString *currentLine;
175-
for (unichar x = 1; x < n + 1; x++) {
176-
currentLine = [NSString stringWithFormat:@"%d\n", (int)x];
175+
for (unichar i = 1; i < n + 1; i++) {
176+
currentLine = [NSString stringWithFormat:@"%d\n", (int)i];
177177
[tmpVector addObject:currentLine];
178178
[lines appendString:currentLine];
179-
[chars appendString:[NSString stringWithFormat:@"%C", x]];
179+
[chars appendString:[NSString stringWithFormat:@"%C", i]];
180180
}
181181
XCTAssertEqual((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
182182
XCTAssertEqual((NSUInteger)n, chars.length, @"More than 256 #2.");
@@ -212,11 +212,11 @@ - (void)test_diff_charsToLinesTest {
212212
NSMutableString *lines = [NSMutableString string];
213213
NSMutableString *chars = [NSMutableString string];
214214
NSString *currentLine;
215-
for (unichar x = 1; x < n + 1; x++) {
216-
currentLine = [NSString stringWithFormat:@"%d\n", (int)x];
215+
for (unichar i = 1; i < n + 1; i++) {
216+
currentLine = [NSString stringWithFormat:@"%d\n", (int)i];
217217
[tmpVector addObject:currentLine];
218218
[lines appendString:currentLine];
219-
[chars appendString:[NSString stringWithFormat:@"%C", x]];
219+
[chars appendString:[NSString stringWithFormat:@"%C", i]];
220220
}
221221
XCTAssertEqual((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
222222
XCTAssertEqual((NSUInteger)n, chars.length, @"More than 256 #2.");
@@ -225,6 +225,19 @@ - (void)test_diff_charsToLinesTest {
225225
[dmp diff_chars:diffs toLines:tmpVector];
226226
XCTAssertEqualObjects([NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:lines]], diffs, @"More than 256 #3.");
227227

228+
// More than 65536 to verify any 16-bit limitation.
229+
lines = [NSMutableString string];
230+
for (int i = 1; i < 66000; i++) {
231+
currentLine = [NSString stringWithFormat:@"%d\n", i];
232+
[lines appendString:currentLine];
233+
}
234+
NSArray *result;
235+
result = [dmp diff_linesToCharsForFirstString:lines andSecondString:@""];
236+
diffs = [NSArray arrayWithObject:[Diff diffWithOperation:DIFF_INSERT andText:result[0]]];
237+
[dmp diff_chars:diffs toLines:result[2]];
238+
Diff *myDiff = diffs.firstObject;
239+
XCTAssertEqualObjects(lines, myDiff.text, @"More than 65536.");
240+
228241
[dmp release];
229242
}
230243

@@ -859,7 +872,7 @@ - (void)test_diff_mainTest {
859872
NSMutableString *aMutable = [NSMutableString stringWithString:a];
860873
NSMutableString *bMutable = [NSMutableString stringWithString:b];
861874
// Increase the text lengths by 1024 times to ensure a timeout.
862-
for (int x = 0; x < 10; x++) {
875+
for (int i = 0; i < 10; i++) {
863876
[aMutable appendString:aMutable];
864877
[bMutable appendString:bMutable];
865878
}

0 commit comments

Comments
 (0)