Skip to content

Commit

Permalink
Adding code changes from SVN revision 102: Adding complete Dart port …
Browse files Browse the repository at this point in the history
…of Diff Match Patch.

Also misc cleanup of other versions (no functional changes).
  • Loading branch information
JanX2 committed Jan 15, 2012
1 parent 75873ef commit 77498ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DiffMatchPatch.h
Expand Up @@ -106,7 +106,7 @@ typedef enum {
NSInteger Match_Distance;

// When deleting a large block of text (over ~64 characters), how close
// does the contents have to match the expected contents. (0.0 =
// do the contents have to be to match the expected contents. (0.0 =
// perfection, 1.0 = very loose). Note that Match_Threshold controls
// how closely the end points of a delete need to match.
float Patch_DeleteThreshold;
Expand Down
59 changes: 30 additions & 29 deletions DiffMatchPatch.m
Expand Up @@ -494,28 +494,29 @@ - (NSMutableArray *)diff_computeFromOldString:(NSString *)text1
return diffs;
}

NSString *longtext = text1.length > text2.length ? text1 : text2;
NSString *shorttext = text1.length > text2.length ? text2 : text1;
NSUInteger i = [longtext rangeOfString:shorttext].location;
if (i != NSNotFound) {
// Shorter text is inside the longer text (speedup).
Operation op = (text1.length > text2.length) ? DIFF_DELETE : DIFF_INSERT;
[diffs addObject:[Diff diffWithOperation:op andText:[longtext substringToIndex:i]]];
[diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:shorttext]];
[diffs addObject:[Diff diffWithOperation:op andText:[longtext substringFromIndex:(i + shorttext.length)]]];
return diffs;
}

if (shorttext.length == 1) {
// Single character string.
// After the previous speedup, the character can't be an equality.
[diffs addObject:[Diff diffWithOperation:DIFF_DELETE andText:text1]];
[diffs addObject:[Diff diffWithOperation:DIFF_INSERT andText:text2]];
return diffs;
{
// New scope so as to garbage collect longtext and shorttext.
NSString *longtext = text1.length > text2.length ? text1 : text2;
NSString *shorttext = text1.length > text2.length ? text2 : text1;
NSUInteger i = [longtext rangeOfString:shorttext].location;
if (i != NSNotFound) {
// Shorter text is inside the longer text (speedup).
Operation op = (text1.length > text2.length) ? DIFF_DELETE : DIFF_INSERT;
[diffs addObject:[Diff diffWithOperation:op andText:[longtext substringToIndex:i]]];
[diffs addObject:[Diff diffWithOperation:DIFF_EQUAL andText:shorttext]];
[diffs addObject:[Diff diffWithOperation:op andText:[longtext substringFromIndex:(i + shorttext.length)]]];
return diffs;
}

if (shorttext.length == 1) {
// Single character string.
// After the previous speedup, the character can't be an equality.
[diffs addObject:[Diff diffWithOperation:DIFF_DELETE andText:text1]];
[diffs addObject:[Diff diffWithOperation:DIFF_INSERT andText:text2]];
return diffs;
}
}

longtext = shorttext = nil; // Garbage collect: prevent abandoned memory.

// Check to see if the problem can be split in two.
NSArray *hm = [NSMakeCollectable(diff_halfMatchCreate((CFStringRef)text1, (CFStringRef)text2, Diff_Timeout)) autorelease];
if (hm != nil) {
Expand Down Expand Up @@ -1459,7 +1460,7 @@ - (NSString *)diff_text2:(NSMutableArray *)diffs;
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
* Operations are tab-separated. Inserted text is escaped using %xx
* notation.
* @param diffs NSMutableArray of diff tuples.
* @param diffs NSMutableArray of Diff objects.
* @return Delta text.
*/
- (NSString *)diff_toDelta:(NSMutableArray *)diffs;
Expand Down Expand Up @@ -1493,7 +1494,7 @@ - (NSString *)diff_toDelta:(NSMutableArray *)diffs;
* @param text1 Source NSString for the diff.
* @param delta Delta text.
* @param error NSError if invalid input.
* @return NSMutableArray of diff tuples or nil if invalid.
* @return NSMutableArray of Diff objects or nil if invalid.
*/
- (NSMutableArray *)diff_fromDeltaWithText:(NSString *)text1
andDelta:(NSString *)delta
Expand Down Expand Up @@ -2128,7 +2129,7 @@ - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1
/**
* Compute a list of patches to turn text1 into text2.
* text1 will be derived from the provided diffs.
* @param diffs NSMutableArray of diff tuples for text1 to text2.
* @param diffs NSMutableArray of Diff objects for text1 to text2.
* @return NSMutableArray of Patch objects.
*/
- (NSMutableArray *)patch_makeFromDiffs:(NSMutableArray *)diffs;
Expand All @@ -2144,7 +2145,7 @@ - (NSMutableArray *)patch_makeFromDiffs:(NSMutableArray *)diffs;
* text2 is ignored, diffs are the delta between text1 and text2.
* @param text1 Old text
* @param text2 New text
* @param diffs NSMutableArray of diff tuples for text1 to text2.
* @param diffs NSMutableArray of Diff objects for text1 to text2.
* @return NSMutableArray of Patch objects.
* @deprecated Prefer -patch_makeFromOldString:diffs:.
*/
Expand All @@ -2165,7 +2166,7 @@ - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1
* Compute a list of patches to turn text1 into text2.
* text2 is not provided, diffs are the delta between text1 and text2.
* @param text1 Old text.
* @param diffs NSMutableArray of diff tuples for text1 to text2.
* @param diffs NSMutableArray of Diff objects for text1 to text2.
* @return NSMutableArray of Patch objects.
*/
- (NSMutableArray *)patch_makeFromOldString:(NSString *)text1
Expand Down Expand Up @@ -2256,8 +2257,8 @@ - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1

/**
* Given an array of patches, return another array that is identical.
* @param patches NSArray of patch objects.
* @return NSMutableArray of patch objects.
* @param patches NSArray of Patch objects.
* @return NSMutableArray of Patch objects.
*/
- (NSMutableArray *)patch_deepCopiedPatches:(NSArray *)patches;
{
Expand All @@ -2268,7 +2269,7 @@ - (NSMutableArray *)patch_deepCopiedPatches:(NSArray *)patches;
/**
* Merge a set of patches onto the text. Return a patched text, as well
* as an array of YES/NO values indicating which patches were applied.
* @param patches NSMutableArray of patch objects
* @param patches NSMutableArray of Patch objects
* @param text Old text.
* @return Two element NSArray, containing the new text and an array of
* BOOL values.
Expand Down Expand Up @@ -2393,7 +2394,7 @@ - (NSArray *)patch_apply:(NSArray *)sourcePatches
/**
* Add some padding on text start and end so that edges can match something.
* Intended to be called only from within patch_apply.
* @param patches NSMutableArray of patch objects.
* @param patches NSMutableArray of Patch objects.
* @return The padding NSString added to each side.
*/
- (NSString *)patch_addPadding:(NSMutableArray *)patches;
Expand Down

0 comments on commit 77498ff

Please sign in to comment.