forked from torifat/iAvro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AvroParser.m
306 lines (266 loc) · 11.2 KB
/
AvroParser.m
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// AvroKeyboard
//
// Created by Rifat Nabi on 6/22/12.
// Copyright (c) 2012 OmicronLab. All rights reserved.
//
#import "AvroParser.h"
static AvroParser* sharedInstance = nil;
@implementation AvroParser
+ (AvroParser *)sharedInstance {
if (sharedInstance == nil) {
[[self alloc] init]; // assignment not done here, see allocWithZone
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (oneway void)release {
//do nothing
}
- (id)autorelease {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; // This is sooo not zero
}
- (id)init {
self = [super init];
if (self) {
NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error: &error];
if (jsonData) {
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
if (!jsonArray) {
@throw error;
// @throw [NSException exceptionWithName:@"AvroParser init" reason:@"Error parsing JSON" userInfo:nil];
} else {
_vowel = [[NSString alloc] initWithString:[jsonArray objectForKey:@"vowel"]];
_consonant = [[NSString alloc] initWithString:[jsonArray objectForKey:@"consonant"]];
_casesensitive = [[NSString alloc] initWithString:[jsonArray objectForKey:@"casesensitive"]];
_patterns = [[NSArray alloc] initWithArray:[jsonArray objectForKey:@"patterns"]];
_maxPatternLength = [[[_patterns objectAtIndex:0] objectForKey:@"find"] length];
}
} else {
@throw error;
}
}
return self;
}
- (void)dealloc {
[_vowel release];
[_consonant release];
[_casesensitive release];
[_patterns release];
[super dealloc];
}
- (NSString*)parse:(NSString *)string {
if (!string || [string length] == 0) {
return @"";
}
NSString * fixed = [self fix:string];
NSMutableString* output = [[NSMutableString alloc] initWithCapacity:0];
int len = [fixed length], cur;
for(cur = 0; cur < len; ++cur) {
int start = cur, end;
BOOL matched = FALSE;
int chunkLen;
for(chunkLen = _maxPatternLength; chunkLen > 0; --chunkLen) {
end = start + chunkLen;
if(end <= len) {
NSString* chunk = [fixed substringWithRange:NSMakeRange(start, chunkLen)];
// Binary Search
int left = 0, right = [_patterns count] - 1, mid;
while(right >= left) {
mid = (right + left) / 2;
NSDictionary* pattern = [_patterns objectAtIndex:mid];
NSString* find = [pattern objectForKey:@"find"];
if([find isEqualToString:chunk]) {
NSArray* rules = [pattern objectForKey:@"rules"];
for(NSDictionary* rule in rules) {
BOOL replace = TRUE;
int chk = 0;
NSArray* matches = [rule objectForKey:@"matches"];
for(NSDictionary* match in matches) {
NSString* value = [match objectForKey:@"value"];
NSString* type = [match objectForKey:@"type"];
NSString* scope = [match objectForKey:@"scope"];
BOOL isNegative = [[match objectForKey:@"negative"] boolValue];
if([type isEqualToString:@"suffix"]) {
chk = end;
}
// Prefix
else {
chk = start - 1;
}
// Beginning
if([scope isEqualToString:@"punctuation"]) {
if(
! (
(chk < 0 && [type isEqualToString:@"prefix"]) ||
(chk >= len && [type isEqualToString:@"suffix"]) ||
[self isPunctuation:[fixed characterAtIndex:chk]]
) ^ isNegative
) {
replace = FALSE;
break;
}
}
// Vowel
else if([scope isEqualToString:@"vowel"]) {
if(
! (
(
(chk >= 0 && [type isEqualToString:@"prefix"]) ||
(chk < len && [type isEqualToString:@"suffix"])
) &&
[self isVowel:[fixed characterAtIndex:chk]]
) ^ isNegative
) {
replace = FALSE;
break;
}
}
// Consonant
else if([scope isEqualToString:@"consonant"]) {
if(
! (
(
(chk >= 0 && [type isEqualToString:@"prefix"]) ||
(chk < len && [type isEqualToString:@"suffix"])
) &&
[self isConsonant:[fixed characterAtIndex:chk]]
) ^ isNegative
) {
replace = FALSE;
break;
}
}
// Exact
else if([scope isEqualToString:@"exact"]) {
int s, e;
if([type isEqualToString:@"suffix"]) {
s = end;
e = end + [value length];
}
// Prefix
else {
s = start - [value length];
e = start;
}
if(![self isExact:value heystack:fixed start:s end:e not:isNegative]) {
replace = FALSE;
break;
}
}
}
if(replace) {
[output appendString:[rule objectForKey:@"replace"]];
cur = end - 1;
matched = TRUE;
break;
}
}
if(matched == TRUE) break;
// Default
[output appendString:[pattern objectForKey:@"replace"]];
cur = end - 1;
matched = TRUE;
break;
}
else if ([find length] > [chunk length] ||
([find length] == [chunk length] && [find compare:chunk] == NSOrderedAscending)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if(matched == TRUE) break;
}
}
if(!matched) {
unichar oldChar = [fixed characterAtIndex:cur];
[output appendString:[NSString stringWithCharacters:&oldChar length:1]];
}
// NSLog(@"cur: %s, start: %s, end: %s, prev: %s\n", cur, start, end, prev);
}
[output autorelease];
return output;
}
- (BOOL)isVowel:(unichar)c {
// Making it lowercase for checking
c = [self smallCap:c];
int i, len = [_vowel length];
for (i = 0; i < len; ++i) {
if ([_vowel characterAtIndex:i] == c) {
return TRUE;
}
}
return FALSE;
}
- (BOOL)isConsonant:(unichar)c {
// Making it lowercase for checking
c = [self smallCap:c];
int i, len = [_consonant length];
for (i = 0; i < len; ++i) {
if ([_consonant characterAtIndex:i] == c) {
return TRUE;
}
}
return FALSE;
//return [consonant rangeOfString:c options:NSCaseInsensitiveSearch].location != NSNotFound;
}
- (BOOL)isPunctuation:(unichar)c {
return !([self isVowel:c] || [self isConsonant:c]);
}
- (BOOL)isCaseSensitive:(unichar)c {
// Making it lowercase for checking
c = [self smallCap:c];
int i, len = [_casesensitive length];
for (i = 0; i < len; ++i) {
if ([_casesensitive characterAtIndex:i] == c) {
return TRUE;
}
}
return FALSE;
}
- (BOOL)isExact:(NSString*) needle heystack:(NSString*)heystack start:(int)start end:(int)end not:(BOOL)not {
// NSLog(@"Cut: %@", [heystack substringWithRange:NSMakeRange(start, end)]);
int len = end - start;
return ((start >= 0 && end < [heystack length]
&& [[heystack substringWithRange:NSMakeRange(start, len)] isEqualToString:needle]) ^ not);
}
- (unichar)smallCap:(unichar) letter {
if(letter >= 'A' && letter <= 'Z') {
letter = letter - 'A' + 'a';
}
return letter;
}
- (NSString*)fix:(NSString *)string {
NSMutableString* fixed = [[NSMutableString alloc] initWithCapacity:0];
int i, len = [string length];
for (i = 0; i < len; ++i) {
unichar c = [string characterAtIndex:i];
if (![self isCaseSensitive:c]) {
[fixed appendFormat:@"%C", [self smallCap:c]];
}
else {
[fixed appendFormat:@"%C", c];
}
}
[fixed autorelease];
return fixed;
}
@end