-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeniusStringDiff.m
More file actions
259 lines (213 loc) · 8.5 KB
/
GeniusStringDiff.m
File metadata and controls
259 lines (213 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Genius
Copyright (C) 2003-2006 John R Chang
Copyright (C) 2007-2008 Chris Miner
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
http://www.gnu.org/licenses/gpl.txt
*/
#import "GeniusStringDiff.h"
@interface NSString (GeniusStringDiff)
- (BOOL) _isEqualToStringIgnoringPunctuationAndCase:(NSString *)string;
@end
//! NSString category with helper method for punctuation insensitive comparisons.
/*! @category NSString(GeniusStringDiff) */
@implementation NSString(GeniusStringDiff)
//! Compares self against @a string ignoring punctuation and case.
- (BOOL) _isEqualToStringIgnoringPunctuationAndCase:(NSString *)string
{
if ([self caseInsensitiveCompare:string] == NSOrderedSame)
return YES;
NSMutableString * string1 = [self mutableCopy];
NSMutableString * string2 = [string mutableCopy];
// Remove punctuation
NSCharacterSet * punctuationCharacterSet = [NSCharacterSet punctuationCharacterSet];
while (1)
{
NSRange range = [string1 rangeOfCharacterFromSet:punctuationCharacterSet];
if (range.location == NSNotFound)
break;
[string1 deleteCharactersInRange:range];
}
while (1)
{
NSRange range = [string2 rangeOfCharacterFromSet:punctuationCharacterSet];
if (range.location == NSNotFound)
break;
[string2 deleteCharactersInRange:range];
}
BOOL outResult = ([string1 caseInsensitiveCompare:string2] == NSOrderedSame);
[string1 release];
[string2 release];
return outResult;
}
@end
//! Handles creation of an string that highlights the differences between two strings.
/*!
Used in quiz interface to visualize the differences between what was typed and what
was expected.
*/
@implementation GeniusStringDiff
//! Helper method to determine the differences between two strings.
/*!
The implementation chops @a string1 and @a string2 into words based on the space character, then writes
the results out into two files which it then feeds to the UNIX diff program in order to calculate the
differences between the two. This method then returns the raw output of the UNIX diff command. Have
a look at the side-by-side output of diff for more details.
@todo Release @a diffTask even when running the command causes an exception.
@todo Remove temp files even when running the command causes an exception.
*/
+ (NSString *) _runDiffFromString:(NSString *)string1 toString:(NSString *)string2
{
// Create temp files
NSMutableString * text1 = [string1 mutableCopy];
NSMutableString * text2 = [string2 mutableCopy];
[text1 replaceOccurrencesOfString:@" " withString:@"\n" options:0L range:NSMakeRange(0, [text1 length])];
[text2 replaceOccurrencesOfString:@" " withString:@"\n" options:0L range:NSMakeRange(0, [text2 length])];
[text1 appendString:@"\n"];
[text2 appendString:@"\n"];
NSData * data1 = [text1 dataUsingEncoding:NSUTF8StringEncoding];
NSData * data2 = [text2 dataUsingEncoding:NSUTF8StringEncoding];
NSString * path1 = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Genius1.tmp"];
NSString * path2 = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Genius2.tmp"];
[data1 writeToFile:path1 atomically:NO];
[data2 writeToFile:path2 atomically:NO];
[text1 release];
[text2 release];
// Run diff
NSTask *diffTask = [[NSTask alloc] init];
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPipe fileHandleForReading];
[diffTask setStandardOutput:newPipe];
[diffTask setLaunchPath:@"/usr/bin/diff"];
[diffTask setArguments:[NSArray arrayWithObjects:@"-b", @"-i", @"--side-by-side", path1, path2, nil]];
BOOL succeed = YES;
NS_DURING
[diffTask launch];
NS_HANDLER
succeed = NO;
NS_ENDHANDLER
if (succeed == NO)
return nil;
NSMutableData * diffData = [NSMutableData data];
NSData *inData = nil;
while ((inData = [readHandle availableData]) && [inData length])
[diffData appendData:inData];
NSString * diffOutput = [[NSString alloc] initWithData:diffData encoding:NSUTF8StringEncoding];
[diffTask release];
// Delete temp files
NSFileManager * fm = [NSFileManager defaultManager];
[fm removeFileAtPath:path1 handler:nil];
[fm removeFileAtPath:path2 handler:nil];
return [diffOutput autorelease];
}
//! creates a string that highlights the differences between @a origString and @a newString.
/*!
Relies on the output of the UNIX diff command.
@todo Rewrite this to make it legible.
@see #_runDiffFromString:toString:
*/
+ (NSAttributedString *) attributedStringHighlightingDifferencesFromString:(NSString *)origString toString:(NSString *)newString
{
if ([newString isEqualToString:@""])
return [[[NSAttributedString alloc] initWithString:@""] autorelease];
if ([newString isEqualToString:origString])
return [[[NSAttributedString alloc] initWithString:origString] autorelease];
// Run diff
NSString * diffOutput = [self _runDiffFromString:origString toString:newString];
if (diffOutput == nil)
return [[[NSAttributedString alloc] initWithString:origString] autorelease];
NSArray * diffLines = [diffOutput componentsSeparatedByString:@"\n"];
//NSLog([diffLines description]); // DEBUG
// Parse results
NSMutableArray * mergedWords = [NSMutableArray array];
NSMutableIndexSet * highlightIndexSet = [NSMutableIndexSet indexSet];
int i, count = [diffLines count];
for (i=0; i<count; i++)
{
NSString * line = [diffLines objectAtIndex:i];
NSArray * columns = [line componentsSeparatedByString:@"\t"];
unsigned int columnCount = [columns count];
if (columnCount < 2)
continue;
NSString * origWord = [columns objectAtIndex:0];
NSString * newWord = [columns lastObject];
// Modified
NSString * marker = [columns objectAtIndex:columnCount-2];
NSRange range = [marker rangeOfString:@"|"];
if (range.location == NSNotFound)
range = [marker rangeOfString:@"/"];
if (range.location != NSNotFound)
{
if ([origWord _isEqualToStringIgnoringPunctuationAndCase:newWord] == NO)
[highlightIndexSet addIndex:[mergedWords count]];
/*else
NSLog(@"%@ and %@ are the same", origWord, newWord);*/
[mergedWords addObject:origWord];
continue;
}
// Removed
range = [marker rangeOfString:@">"];
if (range.location != NSNotFound)
{
const unichar kMiddleDotUnichar = 0x00B7;
NSString * sMiddleDotString = [NSString stringWithCharacters:&kMiddleDotUnichar length:1];
NSString * threeDotsString = [NSString stringWithFormat:@"%@%@%@", sMiddleDotString, sMiddleDotString, sMiddleDotString];
if ([[mergedWords lastObject] isEqual:threeDotsString] == NO)
{
[highlightIndexSet addIndex:[mergedWords count]];
[mergedWords addObject:threeDotsString];
}
continue;
}
// Added
if (range.location == NSNotFound)
range = [newWord rangeOfString:@"<"];
if (range.location != NSNotFound)
{
[highlightIndexSet addIndex:[mergedWords count]];
[mergedWords addObject:origWord];
continue;
}
// Original
[mergedWords addObject:origWord];
}
//NSLog(@"mergedWords=%@", [mergedWords description]);
//NSLog(@"highlightIndexSet=%@", [highlightIndexSet description]);
NSMutableAttributedString * outAttrString = [[NSMutableAttributedString alloc] init];
i = 0, count = [mergedWords count];
while (i<count)
{
NSMutableString * chunk = [NSMutableString string];
BOOL isHighlighted = [highlightIndexSet containsIndex:i];
for (; i<count; i++)
{
BOOL shouldHighlight = [highlightIndexSet containsIndex:i];
if (isHighlighted != shouldHighlight)
break;
NSString * word = [mergedWords objectAtIndex:i];
[chunk appendFormat:@"%@ ", word];
}
[chunk deleteCharactersInRange:NSMakeRange([chunk length]-1, 1)];
//NSLog(@"%d/%d %d \"%@\"", i, count, isHighlighted, chunk);
NSDictionary * attrs = nil;
if (isHighlighted)
{
NSColor * highlightColor = [NSColor colorWithCalibratedRed:1.0 green:1.0 blue:0.5625 alpha:1.0];
attrs = [NSDictionary dictionaryWithObject:highlightColor forKey:NSBackgroundColorAttributeName];
}
NSAttributedString * attrChunk = [[NSAttributedString alloc] initWithString:chunk attributes:attrs];
[outAttrString appendAttributedString:attrChunk];
[attrChunk release];
if (i<count)
[outAttrString appendAttributedString:[[[NSAttributedString alloc] initWithString:@" "] autorelease]];
}
return [outAttrString autorelease];
}
@end