/
Categories.m
239 lines (200 loc) · 6.04 KB
/
Categories.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
//
// Categories.m
// IMLocation
//
// Created by Vincent Gable on 5/28/07.
// Copyright 2007 Vincent Gable. All rights reserved.
//
#import "Categories.h"
#import "VTPG_Common.h"
@implementation NSString (UUID)
+ (NSString*) stringWithUUID {
CFUUIDRef uuidObj = CFUUIDCreate(nil);
NSString *UUIDstring = (NSString*)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return [UUIDstring autorelease];
}
@end
@implementation NSString (RangeAvoidance)
- (BOOL) hasSubstring:(NSString*)substring;
{
if(IsEmpty(substring))
return NO;
NSRange substringRange = [self rangeOfString:substring];
return substringRange.location != NSNotFound && substringRange.length > 0;
}
- (NSString*) substringAfterSubstring:(NSString*)substring;
{
if([self hasSubstring:substring])
return [self substringFromIndex:NSMaxRange([self rangeOfString:substring])];
return nil;
}
//Note: -isCaseInsensitiveLike should work when avalible!
- (BOOL) isEqualToStringIgnoringCase:(NSString*)otherString;
{
if(!otherString)
return NO;
return NSOrderedSame == [self compare:otherString options:NSCaseInsensitiveSearch + NSWidthInsensitiveSearch];
}
@end
@implementation NSObject (VTPGExtensions)
+ (id) make;
{
return [[[[self class] alloc] init] autorelease];
}
@end
@implementation NSString (IndempotentPercentEscapes)
- (NSString*) stringByReplacingPercentEscapesOnce;
{
NSString *unescaped = [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//self may be a string that looks like an invalidly escaped string,
//eg @"100%", in that case it clearly wasn't escaped,
//so we return it as our unescaped string.
return unescaped ? unescaped : self;
}
- (NSString*) stringByAddingPercentEscapesOnce;
{
return [[self stringByReplacingPercentEscapesOnce] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end
@implementation NSURL (IsEqualTesting)
//http://vgable.com/blog/2009/04/22/nsurl-isequal-gotcha/
- (BOOL) isEqualToURL:(NSURL*)otherURL;
{
return [[self absoluteURL] isEqual:[otherURL absoluteURL]] ||
([self isFileURL] && [otherURL isFileURL] && [[self path] isEqual:[otherURL path]]);
}
@end
@implementation NSBundle (VTPGExtensions)
- (NSString*) bundleVersion;
{
return [self objectForInfoDictionaryKey:@"CFBundleVersion"];
}
- (NSURL*) URLForResource:(NSString *)name ofType:(NSString *)ext;
{
NSString *path = [self pathForResource:name ofType:ext];
if(!path)
return nil;//fileURLWithPath: will throw an exception if path is nil.
return [NSURL fileURLWithPath:path];
}
@end
@implementation NSDate (VTPGDateComparison)
- (BOOL) isBefore:(NSDate*)otherDate;
{
return [self timeIntervalSinceDate:otherDate] < 0;
}
- (BOOL) isAfter:(NSDate*)otherDate;
{
return [self timeIntervalSinceDate:otherDate] > 0;
}
@end
@implementation NSFileManager (VTPGExtensions)
- (NSDate*) modificationDateOfItemAtURL:(NSURL*)fileURL {
return [[self attributesOfItemAtPath:[fileURL path] error:nil] fileModificationDate];
}
@end
@implementation NSMutableArray(ArrayQueue)
- (id) takeObject {
id object=nil;
if ([self count]) {
object=[self objectAtIndex:0];
[[object retain] autorelease];
[self removeObjectAtIndex:0];
}
return object;
}
@end
@implementation NSMutableArray (ArrayStack)
- (id) pop;
{
id obj = nil;
if([self count]){
obj = [self lastObject];
[[obj retain] autorelease];
[self removeLastObject];
}
return obj;
}
@end
#pragma mark HigherOrderOperations
@implementation NSObject (HigherOrderOperations)
//returns self
- (id) performSelector:(SEL)oneArgumentSelector withEachItemOf:(id)container;
{
for(id obj in container)
[self performSelector:oneArgumentSelector withObject:obj];
return self;
}
- (NSMutableArray*) mutableNonNilResultsOfPerformingSelector:(SEL)oneArgumentSelector withEachItemOf:(id)container;
{
NSMutableArray *results = [NSMutableArray array];
for(id obj in container){
id aResult = [self performSelector:oneArgumentSelector withObject:obj];
if(aResult)
[results addObject:aResult];
}
return results;
}
- (NSArray*) nonNilResultsOfPerformingSelector:(SEL)oneArgumentSelector withEachItemOf:(id)container
{
return [self mutableNonNilResultsOfPerformingSelector:oneArgumentSelector withEachItemOf:container];
}
@end
@implementation NSArray (HigherOrderOperations)
//foldl op r [a, b, c] => ((r `op` a) `op` b) `op` c
- (id) foldlWithSelector:(SEL)sel initialObject:(id)firstObject
{
id target = firstObject;
for(id obj in self)
target = [target performSelector:sel withObject:obj];
return target;
}
- (NSMutableArray*) mutableResultsOfApplyingSelector:(SEL)sel withObject:arg;
{
NSMutableArray *results = [NSMutableArray arrayWithCapacity:[self count]];
for(id object in self) {
id result = [object performSelector:sel withObject:arg];
[results addObject:result];
}
return results;
}
- (NSMutableArray*) mutableNonNilResultsOfApplyingSelector:(SEL)sel withObject:arg;
{
NSMutableArray *results = [NSMutableArray arrayWithCapacity:[self count]];
for(id object in self) {
id result = [object performSelector:sel withObject:arg];
if(result)
[results addObject:result];
}
return results;
}
- (NSMutableArray*) mutableNonNilResultsOfApplyingSelector:(SEL)sel;
{
return [self mutableNonNilResultsOfApplyingSelector:sel withObject:nil];
}
- (NSArray*) resultsOfApplyingSelector:(SEL)sel withObject:arg;
{
return [self mutableResultsOfApplyingSelector:sel withObject:arg];
}
- (NSArray*) resultsOfApplyingSelector:(SEL)sel;
{
return [self resultsOfApplyingSelector:sel withObject:nil];
}
@end
@implementation NSDictionary (HigherOrderOperations)
- (NSMutableDictionary*) mutableResultsOfApplyingSelector:(SEL)sel withObject:arg
{
NSMutableDictionary *results = [NSMutableDictionary dictionaryWithCapacity:[self count]];
for(id key in [self allKeys])
{
id result = [[self objectForKey:key] performSelector:sel withObject:arg];
if(result)
[results setObject:result forKey:key];
}
return results;
}
- (NSDictionary*) resultsOfApplyingSelector:(SEL)sel withObject:arg
{
return [NSDictionary dictionaryWithDictionary:[self mutableResultsOfApplyingSelector:sel withObject:arg]];
}
@end