Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 177 lines (145 sloc) 7.805 kb
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
1 /*
2 * Diff Match and Patch
3 *
4 * Copyright 2010 geheimwerk.de.
5 * http://code.google.com/p/google-diff-match-patch/
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 * Author: fraser@google.com (Neil Fraser)
20 * ObjC port: jan@geheimwerk.de (Jan Weiß)
21 */
22
23 #import <Foundation/Foundation.h>
24
25 /*
26 * Functions for diff, match and patch.
27 * Computes the difference between two texts to create a patch.
28 * Applies the patch onto another text, allowing for errors.
29 */
30
31 /*
32 * The data structure representing a diff is an NSMutableArray of Diff objects:
33 * {Diff(Operation.DIFF_DELETE, "Hello"),
34 * Diff(Operation.DIFF_INSERT, "Goodbye"),
35 * Diff(Operation.DIFF_EQUAL, " world.")}
36 * which means: delete "Hello", add "Goodbye" and keep " world."
37 */
38
39 typedef enum {
40 DIFF_DELETE = 1,
41 DIFF_INSERT = 2,
42 DIFF_EQUAL = 3
43 } Operation;
44
45
46 /*
47 * Class representing one diff operation.
48 */
49 @interface Diff : NSObject <NSCopying> {
50 Operation operation; // One of: DIFF_INSERT, DIFF_DELETE or DIFF_EQUAL.
51 NSString *text; // The text associated with this diff operation.
52 }
53
54 @property (nonatomic, assign) Operation operation;
55 @property (nonatomic, copy) NSString *text;
56
57 + (id)diffWithOperation:(Operation)anOperation andText:(NSString *)aText;
58
59 - (id)initWithOperation:(Operation)anOperation andText:(NSString *)aText;
60
61 @end
62
63 /*
64 * Class representing one patch operation.
65 */
66 @interface Patch : NSObject <NSCopying> {
67 NSMutableArray *diffs;
68 NSUInteger start1;
69 NSUInteger start2;
70 NSUInteger length1;
71 NSUInteger length2;
72 }
73
74 @property (nonatomic, retain) NSMutableArray *diffs;
75 @property (nonatomic, assign) NSUInteger start1;
76 @property (nonatomic, assign) NSUInteger start2;
77 @property (nonatomic, assign) NSUInteger length1;
78 @property (nonatomic, assign) NSUInteger length2;
79
80 @end
81
82
83 /*
84 * Class containing the diff, match and patch methods.
85 * Also Contains the behaviour settings.
86 */
87 @interface DiffMatchPatch : NSObject {
88 // Number of seconds to map a diff before giving up (0 for infinity).
89 NSTimeInterval Diff_Timeout;
90
91 // Cost of an empty edit operation in terms of edit characters.
92 NSUInteger Diff_EditCost;
93
94 // At what point is no match declared (0.0 = perfection, 1.0 = very loose).
95 double Match_Threshold;
96
97 // How far to search for a match (0 = exact location, 1000+ = broad match).
98 // A match this many characters away from the expected location will add
99 // 1.0 to the score (0.0 is a perfect match).
100 NSInteger Match_Distance;
101
102 // When deleting a large block of text (over ~64 characters), how close
103 // does the contents have to match the expected contents. (0.0 =
104 // perfection, 1.0 = very loose). Note that Match_Threshold controls
105 // how closely the end points of a delete need to match.
106 float Patch_DeleteThreshold;
107
108 // Chunk size for context length.
109 uint16_t Patch_Margin;
110
111 // The number of bits in an int.
112 NSUInteger Match_MaxBits;
113 }
114
115 @property (nonatomic, assign) NSTimeInterval Diff_Timeout;
116 @property (nonatomic, assign) NSUInteger Diff_EditCost;
117 @property (nonatomic, assign) double Match_Threshold;
118 @property (nonatomic, assign) NSInteger Match_Distance;
119 @property (nonatomic, assign) float Patch_DeleteThreshold;
120 @property (nonatomic, assign) uint16_t Patch_Margin;
121
122 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2;
123 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines;
124 - (NSUInteger)diff_commonPrefixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
125 - (NSUInteger)diff_commonSuffixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
126 - (void)diff_cleanupSemantic:(NSMutableArray *)diffs;
127 - (void)diff_cleanupSemanticLossless:(NSMutableArray *)diffs;
128 - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
129 - (void)diff_cleanupMerge:(NSMutableArray *)diffs;
130 - (NSUInteger)diff_xIndexIn:(NSMutableArray *)diffs location:(NSUInteger) loc;
131 - (NSString *)diff_prettyHtml:(NSMutableArray *)diffs;
132 - (NSString *)diff_text1:(NSMutableArray *)diffs;
133 - (NSString *)diff_text2:(NSMutableArray *)diffs;
134 - (NSUInteger)diff_levenshtein:(NSMutableArray *)diffs;
135 - (NSString *)diff_toDelta:(NSMutableArray *)diffs;
136 - (NSMutableArray *)diff_fromDeltaWithText:(NSString *)text1 andDelta:(NSString *)delta error:(NSError **)error;
137
138 - (NSUInteger)match_mainForText:(NSString *)text pattern:(NSString *)pattern near:(NSUInteger)loc;
139 - (NSMutableDictionary *)match_alphabet:(NSString *)pattern;
140
141 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andNewString:(NSString *)text2;
142 - (NSMutableArray *)patch_makeFromDiffs:(NSMutableArray *)diffs;
143 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 newString:(NSString *)text2 diffs:(NSMutableArray *)diffs;
144 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andDiffs:(NSMutableArray *)diffs;
145 - (NSMutableArray *)patch_deepCopy:(NSArray *)patches; // Copy rule applies!
146 - (NSArray *)patch_apply:(NSArray *)sourcePatches toString:(NSString *)text;
147 - (NSString *)patch_addPadding:(NSMutableArray *)patches;
148 - (void)patch_splitMax:(NSMutableArray *)patches;
149 - (NSString *)patch_toText:(NSMutableArray *)patches;
150 - (NSMutableArray *)patch_fromText:(NSString *)textline error:(NSError **)error;
151
152 @end
153
154
155 @interface DiffMatchPatch (PrivateMethods)
156
157 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
158 - (NSMutableArray *)diff_computeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
159 - (NSMutableArray *)diff_lineModeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
160 - (NSArray *)diff_linesToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text1;
2f0066bd »
2011-04-27 Improved infrastructure for word mode support.
161 - (NSArray *)diff_wordsToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text1;
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
162 - (NSString *)diff_linesToCharsMungeOfText:(NSString *)text lineArray:(NSMutableArray *)lineArray lineHash:(NSMutableDictionary *)lineHash;
2f0066bd »
2011-04-27 Improved infrastructure for word mode support.
163 - (NSString *)diff_wordsToCharsMungeOfText:(NSString *)text wordArray:(NSMutableArray *)wordArray wordHash:(NSMutableDictionary *)wordHash;
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
164 - (void)diff_chars:(NSArray *)diffs toLines:(NSMutableArray *)lineArray;
165 - (NSMutableArray *)diff_bisectOfOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
166 - (NSMutableArray *)diff_bisectSplitOfOldString:(NSString *)text1 andNewString:(NSString *)text2 x:(NSUInteger)x y:(NSUInteger)y deadline:(NSTimeInterval)deadline;
167 - (NSUInteger)diff_commonOverlapOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
168 - (NSArray *)diff_halfMatchOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
169 - (NSArray *)diff_halfMatchIOfLongString:(NSString *)longtext andShortString:(NSString *)shorttext;
170 - (NSInteger)diff_cleanupSemanticScoreOfFirstString:(NSString *)one andSecondString:(NSString *)two;
171
172 - (NSUInteger)match_bitapOfText:(NSString *)text andPattern:(NSString *)pattern near:(NSUInteger)loc;
173 - (double)match_bitapScoreForErrorCount:(NSUInteger)e location:(NSUInteger)x near:(NSUInteger)loc pattern:(NSString *)pattern;
174
175 - (void)patch_addContextToPatch:(Patch *)patch sourceText:(NSString *)text;
176
177 @end
Something went wrong with that request. Please try again.