Skip to content

Commit

Permalink
Adding code changes from SVN revision 101: Fix warnings in latest ver…
Browse files Browse the repository at this point in the history
…sion of ObjectiveC.
  • Loading branch information
JanX2 committed Jan 15, 2012
1 parent dfa476f commit 75873ef
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 113 deletions.
177 changes: 89 additions & 88 deletions DiffMatchPatch.m
Expand Up @@ -1120,11 +1120,11 @@ - (void)diff_cleanupMerge:(NSMutableArray *)diffs;
}
// Delete the offending records and add the merged ones.
if (count_delete == 0) {
splice(diffs, thisPointer - count_delete - count_insert,
splice(diffs, thisPointer - count_insert,
count_delete + count_insert,
[NSMutableArray arrayWithObject:[Diff diffWithOperation:DIFF_INSERT andText:text_insert]]);
} else if (count_insert == 0) {
splice(diffs, thisPointer - count_delete - count_insert,
splice(diffs, thisPointer - count_delete,
count_delete + count_insert,
[NSMutableArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:text_delete]]);
} else {
Expand Down Expand Up @@ -1304,8 +1304,8 @@ - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
BOOL changes = NO;
// Stack of indices where equalities are found.
CFMutableArrayRef equalities = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
// Always equal to [diffs objectAtIndex:equalitiesLastValue].text
NSString *lastequality = @"";
// Always equal to equalities.lastObject.text
NSString *lastequality = nil;
CFIndex thisPointer = 0; // Index of current position.
// Is there an insertion operation before the last equality.
BOOL pre_ins = NO;
Expand All @@ -1330,7 +1330,7 @@ - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
} else {
// Not a candidate, and can never become one.
CFArrayRemoveAllValues(equalities);
lastequality = @"";
lastequality = nil;
}
post_ins = post_del = NO;
} else { // An insertion or deletion.
Expand All @@ -1347,7 +1347,7 @@ - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
* <ins>A</del>X<ins>C</ins><del>D</del>
* <ins>A</ins><del>B</del>X<del>C</del>
*/
if ((lastequality.length != 0)
if ((lastequality != nil)
&& ((pre_ins && pre_del && post_ins && post_del)
|| ((lastequality.length < Diff_EditCost / 2)
&& ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0) + (post_ins ? 1 : 0)
Expand All @@ -1362,7 +1362,7 @@ - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
diffToChange.operation = DIFF_INSERT;

diff_CFArrayRemoveLastValue(equalities); // Throw away the equality we just deleted.
lastequality = @"";
lastequality = nil;
if (pre_ins && pre_del) {
// No changes made which could affect previous entry, keep going.
post_ins = post_del = YES;
Expand Down Expand Up @@ -2462,98 +2462,99 @@ - (void)patch_splitMax:(NSMutableArray *)patches;
{
NSUInteger patch_size = Match_MaxBits;
for (NSUInteger x = 0; x < patches.count; x++) {
if (((Patch *)[patches objectAtIndex:x]).length1 > patch_size) {
Patch *bigpatch = [[patches objectAtIndex:x] retain];
// Remove the big old patch.
splice(patches, x--, 1, nil);
NSUInteger start1 = bigpatch.start1;
NSUInteger start2 = bigpatch.start2;
NSString *precontext = @"";
while (bigpatch.diffs.count != 0) {
// Create one of several smaller patches.
Patch *patch = [[Patch new] autorelease];
BOOL empty = YES;
patch.start1 = start1 - precontext.length;
patch.start2 = start2 - precontext.length;
if (precontext.length != 0) {
patch.length1 = patch.length2 = precontext.length;
[patch.diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:precontext]];
}
while (bigpatch.diffs.count != 0
&& patch.length1 < patch_size - self.Patch_Margin) {
Operation diff_type = ((Diff *)[bigpatch.diffs objectAtIndex:0]).operation;
NSString *diff_text = ((Diff *)[bigpatch.diffs objectAtIndex:0]).text;
if (diff_type == DIFF_INSERT) {
// Insertions are harmless.
if (((Patch *)[patches objectAtIndex:x]).length1 <= patch_size) {
continue;
}
Patch *bigpatch = [[patches objectAtIndex:x] retain];
// Remove the big old patch.
splice(patches, x--, 1, nil);
NSUInteger start1 = bigpatch.start1;
NSUInteger start2 = bigpatch.start2;
NSString *precontext = @"";
while (bigpatch.diffs.count != 0) {
// Create one of several smaller patches.
Patch *patch = [[Patch new] autorelease];
BOOL empty = YES;
patch.start1 = start1 - precontext.length;
patch.start2 = start2 - precontext.length;
if (precontext.length != 0) {
patch.length1 = patch.length2 = precontext.length;
[patch.diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:precontext]];
}
while (bigpatch.diffs.count != 0
&& patch.length1 < patch_size - self.Patch_Margin) {
Operation diff_type = ((Diff *)[bigpatch.diffs objectAtIndex:0]).operation;
NSString *diff_text = ((Diff *)[bigpatch.diffs objectAtIndex:0]).text;
if (diff_type == DIFF_INSERT) {
// Insertions are harmless.
patch.length2 += diff_text.length;
start2 += diff_text.length;
[patch.diffs addObject:[bigpatch.diffs objectAtIndex:0]];
[bigpatch.diffs removeObjectAtIndex:0];
empty = NO;
} else if (diff_type == DIFF_DELETE && patch.diffs.count == 1
&& ((Diff *)[patch.diffs objectAtIndex:0]).operation == DIFF_EQUAL
&& diff_text.length > 2 * patch_size) {
// This is a large deletion. Let it pass in one chunk.
patch.length1 += diff_text.length;
start1 += diff_text.length;
empty = NO;
[patch.diffs addObject:[Diff diffWithOperation:diff_type andText:diff_text]];
[bigpatch.diffs removeObjectAtIndex:0];
} else {
// Deletion or equality. Only take as much as we can stomach.
diff_text = [diff_text substringWithRange:NSMakeRange(0,
MIN(diff_text.length,
(patch_size - patch.length1 - Patch_Margin)))];
patch.length1 += diff_text.length;
start1 += diff_text.length;
if (diff_type == DIFF_EQUAL) {
patch.length2 += diff_text.length;
start2 += diff_text.length;
[patch.diffs addObject:[bigpatch.diffs objectAtIndex:0]];
[bigpatch.diffs removeObjectAtIndex:0];
empty = NO;
} else if (diff_type == DIFF_DELETE && patch.diffs.count == 1
&& ((Diff *)[patch.diffs objectAtIndex:0]).operation == DIFF_EQUAL
&& diff_text.length > 2 * patch_size) {
// This is a large deletion. Let it pass in one chunk.
patch.length1 += diff_text.length;
start1 += diff_text.length;
} else {
empty = NO;
[patch.diffs addObject:[Diff diffWithOperation:diff_type andText:diff_text]];
}
[patch.diffs addObject:[Diff diffWithOperation:diff_type andText:diff_text]];
if (diff_text == ((Diff *)[bigpatch.diffs objectAtIndex:0]).text) {
[bigpatch.diffs removeObjectAtIndex:0];
} else {
// Deletion or equality. Only take as much as we can stomach.
diff_text = [diff_text substringWithRange:NSMakeRange(0,
MIN(diff_text.length,
(patch_size - patch.length1 - Patch_Margin)))];
patch.length1 += diff_text.length;
start1 += diff_text.length;
if (diff_type == DIFF_EQUAL) {
patch.length2 += diff_text.length;
start2 += diff_text.length;
} else {
empty = NO;
}
[patch.diffs addObject:[Diff diffWithOperation:diff_type andText:diff_text]];
if (diff_text == ((Diff *)[bigpatch.diffs objectAtIndex:0]).text) {
[bigpatch.diffs removeObjectAtIndex:0];
} else {
Diff *firstDiff = [bigpatch.diffs objectAtIndex:0];
firstDiff.text = [firstDiff.text substringFromIndex:diff_text.length];
}
Diff *firstDiff = [bigpatch.diffs objectAtIndex:0];
firstDiff.text = [firstDiff.text substringFromIndex:diff_text.length];
}
}
// Compute the head context for the next patch.
precontext = [self diff_text2:patch.diffs];
precontext = [precontext substringFromIndex:MAX_OF_CONST_AND_DIFF(0, precontext.length, Patch_Margin)];

NSString *postcontext = nil;
// Append the end context for this patch.
if ([self diff_text1:bigpatch.diffs].length > Patch_Margin) {
postcontext = [[self diff_text1:bigpatch.diffs]
substringToIndex:Patch_Margin];
}
// Compute the head context for the next patch.
precontext = [self diff_text2:patch.diffs];
precontext = [precontext substringFromIndex:MAX_OF_CONST_AND_DIFF(0, precontext.length, Patch_Margin)];

NSString *postcontext = nil;
// Append the end context for this patch.
if ([self diff_text1:bigpatch.diffs].length > Patch_Margin) {
postcontext = [[self diff_text1:bigpatch.diffs]
substringToIndex:Patch_Margin];
} else {
postcontext = [self diff_text1:bigpatch.diffs];
}

if (postcontext.length != 0) {
patch.length1 += postcontext.length;
patch.length2 += postcontext.length;
if (patch.diffs.count != 0
&& ((Diff *)[patch.diffs objectAtIndex:(patch.diffs.count - 1)]).operation
== DIFF_EQUAL) {
Diff *lastDiff = [patch.diffs lastObject];
lastDiff.text = [lastDiff.text stringByAppendingString:postcontext];
} else {
postcontext = [self diff_text1:bigpatch.diffs];
}

if (postcontext.length != 0) {
patch.length1 += postcontext.length;
patch.length2 += postcontext.length;
if (patch.diffs.count != 0
&& ((Diff *)[patch.diffs objectAtIndex:(patch.diffs.count - 1)]).operation
== DIFF_EQUAL) {
Diff *lastDiff = [patch.diffs lastObject];
lastDiff.text = [lastDiff.text stringByAppendingString:postcontext];
} else {
[patch.diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:postcontext]];
}
}
if (!empty) {
splice(patches, ++x, 0, [NSMutableArray arrayWithObject:patch]);
[patch.diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:postcontext]];
}
}

[bigpatch release];

if (!empty) {
splice(patches, ++x, 0, [NSMutableArray arrayWithObject:patch]);
}
}

[bigpatch release];

}
}

Expand Down
50 changes: 25 additions & 25 deletions Tests/DiffMatchPatchTest.m
Expand Up @@ -139,34 +139,34 @@ - (void)test_diff_linesToCharsTest {
NSArray *result;

// Convert lines down to characters.
NSMutableArray *tmpVector = [NSMutableArray array]; // array of NSString objects
NSMutableArray *tmpVector = [NSMutableArray array]; // Array of NSString objects.
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\n"];
[tmpVector addObject:@"beta\n"];
result = [dmp diff_linesToCharsForFirstString:@"alpha\nbeta\nalpha\n" andSecondString:@"beta\nalpha\nbeta\n"];
STAssertEqualObjects(@"\001\002\001", [result objectAtIndex:0], @"Convert lines down to characters #1");
STAssertEqualObjects(@"\002\001\002", [result objectAtIndex:1], @"Convert lines down to characters #2");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Convert lines down to characters #3");

STAssertEqualObjects(@"\001\002\001", [result objectAtIndex:0], @"Shared lines #1.");
STAssertEqualObjects(@"\002\001\002", [result objectAtIndex:1], @"Shared lines #2.");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Shared lines #3.");
[tmpVector removeAllObjects];
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\r\n"];
[tmpVector addObject:@"beta\r\n"];
[tmpVector addObject:@"\r\n"];
result = [dmp diff_linesToCharsForFirstString:@"" andSecondString:@"alpha\r\nbeta\r\n\r\n\r\n"];
STAssertEqualObjects(@"", [result objectAtIndex:0], @"Convert lines down to characters #4");
STAssertEqualObjects(@"\001\002\003\003", [result objectAtIndex:1], @"Convert lines down to characters #5");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Convert lines down to characters #6");

STAssertEqualObjects(@"", [result objectAtIndex:0], @"Empty string and blank lines #1.");
STAssertEqualObjects(@"\001\002\003\003", [result objectAtIndex:1], @"Empty string and blank lines #2.");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Empty string and blank lines #3.");
[tmpVector removeAllObjects];
[tmpVector addObject:@""];
[tmpVector addObject:@"a"];
[tmpVector addObject:@"b"];
result = [dmp diff_linesToCharsForFirstString:@"a" andSecondString:@"b"];
STAssertEqualObjects(@"\001", [result objectAtIndex:0], @"Convert lines down to characters #7");
STAssertEqualObjects(@"\002", [result objectAtIndex:1], @"Convert lines down to characters #8");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Convert lines down to characters #9");

STAssertEqualObjects(@"\001", [result objectAtIndex:0], @"No linebreaks #1.");
STAssertEqualObjects(@"\002", [result objectAtIndex:1], @"No linebreaks #2.");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"No linebreaks #3.");
// More than 256 to reveal any 8-bit limitations.
unichar n = 300;
[tmpVector removeAllObjects];
Expand All @@ -179,14 +179,14 @@ - (void)test_diff_linesToCharsTest {
[lines appendString:currentLine];
[chars appendString:[NSString stringWithFormat:@"%C", x]];
}
STAssertEquals((NSUInteger)n, tmpVector.count, @"Convert lines down to characters #10");
STAssertEquals((NSUInteger)n, chars.length, @"Convert lines down to characters #11");
STAssertEquals((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
STAssertEquals((NSUInteger)n, chars.length, @"More than 256 #2.");
[tmpVector insertObject:@"" atIndex:0];
result = [dmp diff_linesToCharsForFirstString:lines andSecondString:@""];
STAssertEqualObjects(chars, [result objectAtIndex:0], @"Convert lines down to characters #12");
STAssertEqualObjects(@"", [result objectAtIndex:1], @"Convert lines down to characters #13");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"Convert lines down to characters #14");

STAssertEqualObjects(chars, [result objectAtIndex:0], @"More than 256 #3.");
STAssertEqualObjects(@"", [result objectAtIndex:1], @"More than 256 #4.");
STAssertEqualObjects(tmpVector, (NSArray *)[result objectAtIndex:2], @"More than 256 #5.");
[dmp release];
}

Expand All @@ -195,7 +195,7 @@ - (void)test_diff_wordsToCharsTest {
NSArray *result;

// Convert words down to characters.
NSMutableArray *tmpVector = [NSMutableArray array]; // array of NSString objects
NSMutableArray *tmpVector = [NSMutableArray array]; // Array of NSString objects.
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha"];
[tmpVector addObject:@" "];
Expand Down Expand Up @@ -265,15 +265,15 @@ - (void)test_diff_charsToLinesTest {
NSArray *diffs = [NSArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"\001\002\001"],
[Diff diffWithOperation:DIFF_INSERT andText:@"\002\001\002"], nil];
NSMutableArray *tmpVector = [NSMutableArray array]; // array of NSString objects
NSMutableArray *tmpVector = [NSMutableArray array]; // Array of NSString objects.
[tmpVector addObject:@""];
[tmpVector addObject:@"alpha\n"];
[tmpVector addObject:@"beta\n"];
[dmp diff_chars:diffs toLines:tmpVector];
NSArray *expectedResult = [NSArray arrayWithObjects:
[Diff diffWithOperation:DIFF_EQUAL andText:@"alpha\nbeta\nalpha\n"],
[Diff diffWithOperation:DIFF_INSERT andText:@"beta\nalpha\nbeta\n"], nil];
STAssertEqualObjects(expectedResult, diffs, @"Convert chars up to lines #1");
STAssertEqualObjects(expectedResult, diffs, @"Shared lines.");

// More than 256 to reveal any 8-bit limitations.
unichar n = 300;
Expand All @@ -287,12 +287,12 @@ - (void)test_diff_charsToLinesTest {
[lines appendString:currentLine];
[chars appendString:[NSString stringWithFormat:@"%C", x]];
}
STAssertEquals((NSUInteger)n, tmpVector.count, @"Convert chars up to lines #2");
STAssertEquals((NSUInteger)n, chars.length, @"Convert chars up to lines #3");
STAssertEquals((NSUInteger)n, tmpVector.count, @"More than 256 #1.");
STAssertEquals((NSUInteger)n, chars.length, @"More than 256 #2.");
[tmpVector insertObject:@"" atIndex:0];
diffs = [NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:chars]];
[dmp diff_chars:diffs toLines:tmpVector];
STAssertEqualObjects([NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:lines]], diffs, @"Convert chars up to lines #4");
STAssertEqualObjects([NSArray arrayWithObject:[Diff diffWithOperation:DIFF_DELETE andText:lines]], diffs, @"More than 256 #3.");

[dmp release];
}
Expand Down

0 comments on commit 75873ef

Please sign in to comment.