Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 195 lines (162 sloc) 8.767 kB
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
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
2a6e947 @JanX2 Adding high level support for munging sentences, paragraphs and line …
authored
45 typedef enum {
46 DiffWordTokens = 1,
47 DiffParagraphTokens = 2,
48 DiffSentenceTokens = 3,
81921bc @JanX2 Fixing symbol names and options to correctly represent what “line bre…
authored
49 DiffLineBreakDelimiteredTokens = 4
2a6e947 @JanX2 Adding high level support for munging sentences, paragraphs and line …
authored
50 } DiffTokenMode;
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
51
52 /*
53 * Class representing one diff operation.
54 */
55 @interface Diff : NSObject <NSCopying> {
56 Operation operation; // One of: DIFF_INSERT, DIFF_DELETE or DIFF_EQUAL.
57 NSString *text; // The text associated with this diff operation.
58 }
59
60 @property (nonatomic, assign) Operation operation;
61 @property (nonatomic, copy) NSString *text;
62
63 + (id)diffWithOperation:(Operation)anOperation andText:(NSString *)aText;
64
65 - (id)initWithOperation:(Operation)anOperation andText:(NSString *)aText;
66
67 @end
68
69 /*
70 * Class representing one patch operation.
71 */
72 @interface Patch : NSObject <NSCopying> {
73 NSMutableArray *diffs;
74 NSUInteger start1;
75 NSUInteger start2;
76 NSUInteger length1;
77 NSUInteger length2;
78 }
79
80 @property (nonatomic, retain) NSMutableArray *diffs;
81 @property (nonatomic, assign) NSUInteger start1;
82 @property (nonatomic, assign) NSUInteger start2;
83 @property (nonatomic, assign) NSUInteger length1;
84 @property (nonatomic, assign) NSUInteger length2;
85
86 @end
87
55af412 @JanX2 Reverting to less confusing “-patch_deepCopy:”.
authored
88 #ifndef NS_RETURNS_RETAINED
89 #if __has_feature(attribute_ns_returns_retained)
90 #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
91 #else
92 #define NS_RETURNS_RETAINED
93 #endif
94 #endif
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
95
96 /*
97 * Class containing the diff, match and patch methods.
98 * Also Contains the behaviour settings.
99 */
100 @interface DiffMatchPatch : NSObject {
101 // Number of seconds to map a diff before giving up (0 for infinity).
102 NSTimeInterval Diff_Timeout;
103
104 // Cost of an empty edit operation in terms of edit characters.
105 NSUInteger Diff_EditCost;
106
107 // At what point is no match declared (0.0 = perfection, 1.0 = very loose).
108 double Match_Threshold;
109
110 // How far to search for a match (0 = exact location, 1000+ = broad match).
111 // A match this many characters away from the expected location will add
112 // 1.0 to the score (0.0 is a perfect match).
113 NSInteger Match_Distance;
114
115 // When deleting a large block of text (over ~64 characters), how close
77498ff @JanX2 Adding code changes from SVN revision 102: Adding complete Dart port …
authored
116 // do the contents have to be to match the expected contents. (0.0 =
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
117 // perfection, 1.0 = very loose). Note that Match_Threshold controls
118 // how closely the end points of a delete need to match.
119 float Patch_DeleteThreshold;
120
121 // Chunk size for context length.
122 uint16_t Patch_Margin;
123
124 // The number of bits in an int.
125 NSUInteger Match_MaxBits;
126 }
127
128 @property (nonatomic, assign) NSTimeInterval Diff_Timeout;
129 @property (nonatomic, assign) NSUInteger Diff_EditCost;
130 @property (nonatomic, assign) double Match_Threshold;
131 @property (nonatomic, assign) NSInteger Match_Distance;
132 @property (nonatomic, assign) float Patch_DeleteThreshold;
133 @property (nonatomic, assign) uint16_t Patch_Margin;
134
135 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2;
136 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines;
137 - (NSUInteger)diff_commonPrefixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
138 - (NSUInteger)diff_commonSuffixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
139 - (void)diff_cleanupSemantic:(NSMutableArray *)diffs;
140 - (void)diff_cleanupSemanticLossless:(NSMutableArray *)diffs;
141 - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
142 - (void)diff_cleanupMerge:(NSMutableArray *)diffs;
143 - (NSUInteger)diff_xIndexIn:(NSMutableArray *)diffs location:(NSUInteger) loc;
144 - (NSString *)diff_prettyHtml:(NSMutableArray *)diffs;
145 - (NSString *)diff_text1:(NSMutableArray *)diffs;
146 - (NSString *)diff_text2:(NSMutableArray *)diffs;
147 - (NSUInteger)diff_levenshtein:(NSMutableArray *)diffs;
148 - (NSString *)diff_toDelta:(NSMutableArray *)diffs;
149 - (NSMutableArray *)diff_fromDeltaWithText:(NSString *)text1 andDelta:(NSString *)delta error:(NSError **)error;
150
151 - (NSUInteger)match_mainForText:(NSString *)text pattern:(NSString *)pattern near:(NSUInteger)loc;
152 - (NSMutableDictionary *)match_alphabet:(NSString *)pattern;
153
154 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andNewString:(NSString *)text2;
155 - (NSMutableArray *)patch_makeFromDiffs:(NSMutableArray *)diffs;
156 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 newString:(NSString *)text2 diffs:(NSMutableArray *)diffs;
157 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andDiffs:(NSMutableArray *)diffs;
55af412 @JanX2 Reverting to less confusing “-patch_deepCopy:”.
authored
158 - (NSMutableArray *)patch_deepCopy:(NSArray *)patches NS_RETURNS_RETAINED; // Copy rule applies!
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
159 - (NSArray *)patch_apply:(NSArray *)sourcePatches toString:(NSString *)text;
160 - (NSString *)patch_addPadding:(NSMutableArray *)patches;
161 - (void)patch_splitMax:(NSMutableArray *)patches;
162 - (NSString *)patch_toText:(NSMutableArray *)patches;
163 - (NSMutableArray *)patch_fromText:(NSString *)textline error:(NSError **)error;
164
165 @end
166
167
168 @interface DiffMatchPatch (PrivateMethods)
169
170 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
171 - (NSMutableArray *)diff_computeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
172 - (NSMutableArray *)diff_lineModeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
173 - (NSArray *)diff_linesToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text1;
2a6e947 @JanX2 Adding high level support for munging sentences, paragraphs and line …
authored
174 - (NSArray *)diff_tokensToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text2 mode:(DiffTokenMode)mode;
2f0066b @JanX2 Improved infrastructure for word mode support.
authored
175 - (NSArray *)diff_wordsToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text1;
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
176 - (NSString *)diff_linesToCharsMungeOfText:(NSString *)text lineArray:(NSMutableArray *)lineArray lineHash:(NSMutableDictionary *)lineHash;
2f0066b @JanX2 Improved infrastructure for word mode support.
authored
177 - (NSString *)diff_wordsToCharsMungeOfText:(NSString *)text wordArray:(NSMutableArray *)wordArray wordHash:(NSMutableDictionary *)wordHash;
2a6e947 @JanX2 Adding high level support for munging sentences, paragraphs and line …
authored
178 - (NSString *)diff_sentencesToCharsMungeOfText:(NSString *)text sentenceArray:(NSMutableArray *)sentenceArray sentenceHash:(NSMutableDictionary *)sentenceHash;
179 - (NSString *)diff_paragraphsToCharsMungeOfText:(NSString *)text paragraphArray:(NSMutableArray *)paragraphArray paragraphHash:(NSMutableDictionary *)paragraphHash;
81921bc @JanX2 Fixing symbol names and options to correctly represent what “line bre…
authored
180 - (NSString *)diff_lineBreakDelimiteredToCharsMungeOfText:(NSString *)text lineArray:(NSMutableArray *)lineArray lineHash:(NSMutableDictionary *)lineHash;
4d4563c @JanX2 Objective C port of “diff-match-patch” as released in sub-folder “obj…
authored
181 - (void)diff_chars:(NSArray *)diffs toLines:(NSMutableArray *)lineArray;
182 - (NSMutableArray *)diff_bisectOfOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
183 - (NSMutableArray *)diff_bisectSplitOfOldString:(NSString *)text1 andNewString:(NSString *)text2 x:(NSUInteger)x y:(NSUInteger)y deadline:(NSTimeInterval)deadline;
184 - (NSUInteger)diff_commonOverlapOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
185 - (NSArray *)diff_halfMatchOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
186 - (NSArray *)diff_halfMatchIOfLongString:(NSString *)longtext andShortString:(NSString *)shorttext;
187 - (NSInteger)diff_cleanupSemanticScoreOfFirstString:(NSString *)one andSecondString:(NSString *)two;
188
189 - (NSUInteger)match_bitapOfText:(NSString *)text andPattern:(NSString *)pattern near:(NSUInteger)loc;
190 - (double)match_bitapScoreForErrorCount:(NSUInteger)e location:(NSUInteger)x near:(NSUInteger)loc pattern:(NSString *)pattern;
191
192 - (void)patch_addContextToPatch:(Patch *)patch sourceText:(NSString *)text;
193
194 @end
Something went wrong with that request. Please try again.