-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSArray+DespoofStrings.m
47 lines (39 loc) · 1.64 KB
/
NSArray+DespoofStrings.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
#import "NSString+SSYExtraUtils.h"
@implementation NSArray (DespoofStrings)
- (NSArray*)replaceOccurrencesOfString:(NSString*)target
withString:(NSString*)replacement {
NSArray* despoofedArray = nil ;
if (target && replacement) {
// This is written to be efficient, assuming patches will be rare.
// If any spoofs are found, patches will be a dictionary with
// key = index of string containing one or more target strings
// value = string with spoofs replaced by underscore
NSMutableDictionary* patches = nil ;
NSInteger index = 0 ;
for (NSString* string in self) {
if (([string containsString:target])) {
NSMutableString* newTag = [string mutableCopy] ;
[newTag replaceOccurrencesOfString:target
withString:replacement] ;
if (!patches) {
patches = [[NSMutableDictionary alloc] init] ;
}
[patches setObject:newTag forKey:[NSNumber numberWithInteger:index]] ;
[newTag release] ;
}
index++ ;
}
if (patches) {
NSMutableArray* newArray = [self mutableCopy] ;
for (NSNumber* oIndex in patches) {
[newArray replaceObjectAtIndex:[oIndex integerValue]
withObject:[patches objectForKey:oIndex]] ;
}
[patches release] ;
despoofedArray = [NSArray arrayWithArray:newArray] ;
[newArray release] ;
}
}
return despoofedArray ;
}
@end