Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 188 lines (155 sloc) 8.566 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
2a6e947d »
2011-06-10 Adding high level support for munging sentences, paragraphs and line …
45 typedef enum {
46 DiffWordTokens = 1,
47 DiffParagraphTokens = 2,
48 DiffSentenceTokens = 3,
81921bcf »
2011-06-10 Fixing symbol names and options to correctly represent what “line bre…
49 DiffLineBreakDelimiteredTokens = 4
2a6e947d »
2011-06-10 Adding high level support for munging sentences, paragraphs and line …
50 } DiffTokenMode;
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
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
88
89 /*
90 * Class containing the diff, match and patch methods.
91 * Also Contains the behaviour settings.
92 */
93 @interface DiffMatchPatch : NSObject {
94 // Number of seconds to map a diff before giving up (0 for infinity).
95 NSTimeInterval Diff_Timeout;
96
97 // Cost of an empty edit operation in terms of edit characters.
98 NSUInteger Diff_EditCost;
99
100 // At what point is no match declared (0.0 = perfection, 1.0 = very loose).
101 double Match_Threshold;
102
103 // How far to search for a match (0 = exact location, 1000+ = broad match).
104 // A match this many characters away from the expected location will add
105 // 1.0 to the score (0.0 is a perfect match).
106 NSInteger Match_Distance;
107
108 // When deleting a large block of text (over ~64 characters), how close
77498ff9 »
2012-01-15 Adding code changes from SVN revision 102: Adding complete Dart port …
109 // do the contents have to be to match the expected contents. (0.0 =
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
110 // perfection, 1.0 = very loose). Note that Match_Threshold controls
111 // how closely the end points of a delete need to match.
112 float Patch_DeleteThreshold;
113
114 // Chunk size for context length.
115 uint16_t Patch_Margin;
116
117 // The number of bits in an int.
118 NSUInteger Match_MaxBits;
119 }
120
121 @property (nonatomic, assign) NSTimeInterval Diff_Timeout;
122 @property (nonatomic, assign) NSUInteger Diff_EditCost;
123 @property (nonatomic, assign) double Match_Threshold;
124 @property (nonatomic, assign) NSInteger Match_Distance;
125 @property (nonatomic, assign) float Patch_DeleteThreshold;
126 @property (nonatomic, assign) uint16_t Patch_Margin;
127
128 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2;
129 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines;
130 - (NSUInteger)diff_commonPrefixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
131 - (NSUInteger)diff_commonSuffixOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
132 - (void)diff_cleanupSemantic:(NSMutableArray *)diffs;
133 - (void)diff_cleanupSemanticLossless:(NSMutableArray *)diffs;
134 - (void)diff_cleanupEfficiency:(NSMutableArray *)diffs;
135 - (void)diff_cleanupMerge:(NSMutableArray *)diffs;
136 - (NSUInteger)diff_xIndexIn:(NSMutableArray *)diffs location:(NSUInteger) loc;
137 - (NSString *)diff_prettyHtml:(NSMutableArray *)diffs;
138 - (NSString *)diff_text1:(NSMutableArray *)diffs;
139 - (NSString *)diff_text2:(NSMutableArray *)diffs;
140 - (NSUInteger)diff_levenshtein:(NSMutableArray *)diffs;
141 - (NSString *)diff_toDelta:(NSMutableArray *)diffs;
142 - (NSMutableArray *)diff_fromDeltaWithText:(NSString *)text1 andDelta:(NSString *)delta error:(NSError **)error;
143
144 - (NSUInteger)match_mainForText:(NSString *)text pattern:(NSString *)pattern near:(NSUInteger)loc;
145 - (NSMutableDictionary *)match_alphabet:(NSString *)pattern;
146
147 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andNewString:(NSString *)text2;
148 - (NSMutableArray *)patch_makeFromDiffs:(NSMutableArray *)diffs;
149 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 newString:(NSString *)text2 diffs:(NSMutableArray *)diffs;
150 - (NSMutableArray *)patch_makeFromOldString:(NSString *)text1 andDiffs:(NSMutableArray *)diffs;
b695eb3c »
2012-01-03 Fixing conformance to naming convention.
151 - (NSMutableArray *)patch_deepCopiedPatches:(NSArray *)patches; // Copy rule applies!
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
152 - (NSArray *)patch_apply:(NSArray *)sourcePatches toString:(NSString *)text;
153 - (NSString *)patch_addPadding:(NSMutableArray *)patches;
154 - (void)patch_splitMax:(NSMutableArray *)patches;
155 - (NSString *)patch_toText:(NSMutableArray *)patches;
156 - (NSMutableArray *)patch_fromText:(NSString *)textline error:(NSError **)error;
157
158 @end
159
160
161 @interface DiffMatchPatch (PrivateMethods)
162
163 - (NSMutableArray *)diff_mainOfOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
164 - (NSMutableArray *)diff_computeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 checkLines:(BOOL)checklines deadline:(NSTimeInterval)deadline;
165 - (NSMutableArray *)diff_lineModeFromOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
166 - (NSArray *)diff_linesToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text1;
2a6e947d »
2011-06-10 Adding high level support for munging sentences, paragraphs and line …
167 - (NSArray *)diff_tokensToCharsForFirstString:(NSString *)text1 andSecondString:(NSString *)text2 mode:(DiffTokenMode)mode;
2f0066bd »
2011-04-27 Improved infrastructure for word mode support.
168 - (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…
169 - (NSString *)diff_linesToCharsMungeOfText:(NSString *)text lineArray:(NSMutableArray *)lineArray lineHash:(NSMutableDictionary *)lineHash;
2f0066bd »
2011-04-27 Improved infrastructure for word mode support.
170 - (NSString *)diff_wordsToCharsMungeOfText:(NSString *)text wordArray:(NSMutableArray *)wordArray wordHash:(NSMutableDictionary *)wordHash;
2a6e947d »
2011-06-10 Adding high level support for munging sentences, paragraphs and line …
171 - (NSString *)diff_sentencesToCharsMungeOfText:(NSString *)text sentenceArray:(NSMutableArray *)sentenceArray sentenceHash:(NSMutableDictionary *)sentenceHash;
172 - (NSString *)diff_paragraphsToCharsMungeOfText:(NSString *)text paragraphArray:(NSMutableArray *)paragraphArray paragraphHash:(NSMutableDictionary *)paragraphHash;
81921bcf »
2011-06-10 Fixing symbol names and options to correctly represent what “line bre…
173 - (NSString *)diff_lineBreakDelimiteredToCharsMungeOfText:(NSString *)text lineArray:(NSMutableArray *)lineArray lineHash:(NSMutableDictionary *)lineHash;
4d4563cd »
2011-03-04 Objective C port of “diff-match-patch” as released in sub-folder “obj…
174 - (void)diff_chars:(NSArray *)diffs toLines:(NSMutableArray *)lineArray;
175 - (NSMutableArray *)diff_bisectOfOldString:(NSString *)text1 andNewString:(NSString *)text2 deadline:(NSTimeInterval)deadline;
176 - (NSMutableArray *)diff_bisectSplitOfOldString:(NSString *)text1 andNewString:(NSString *)text2 x:(NSUInteger)x y:(NSUInteger)y deadline:(NSTimeInterval)deadline;
177 - (NSUInteger)diff_commonOverlapOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
178 - (NSArray *)diff_halfMatchOfFirstString:(NSString *)text1 andSecondString:(NSString *)text2;
179 - (NSArray *)diff_halfMatchIOfLongString:(NSString *)longtext andShortString:(NSString *)shorttext;
180 - (NSInteger)diff_cleanupSemanticScoreOfFirstString:(NSString *)one andSecondString:(NSString *)two;
181
182 - (NSUInteger)match_bitapOfText:(NSString *)text andPattern:(NSString *)pattern near:(NSUInteger)loc;
183 - (double)match_bitapScoreForErrorCount:(NSUInteger)e location:(NSUInteger)x near:(NSUInteger)loc pattern:(NSString *)pattern;
184
185 - (void)patch_addContextToPatch:(Patch *)patch sourceText:(NSString *)text;
186
187 @end
Something went wrong with that request. Please try again.