forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSArray+DespoofStrings.m
43 lines (36 loc) · 1.23 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
#import "NSString+SSYExtraUtils.h"
@implementation NSArray (DespoofStrings)
- (NSArray*)replaceByUnderscoreOccurrencesInStringsOfCharacter:(NSString*)delimiter {
// 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 delimiter spoofs
// value = string with spoofs replaced by underscore
NSMutableDictionary* patches = nil ;
int index = 0 ;
for (NSString* string in self) {
if (([string containsString:delimiter])) {
NSMutableString* newTag = [string mutableCopy] ;
[newTag replaceOccurrencesOfString:delimiter
withString:@"_"] ;
if (!patches) {
patches = [[NSMutableDictionary alloc] init] ;
}
[patches setObject:newTag forKey:[NSNumber numberWithInt:index]] ;
[newTag release] ;
}
index++ ;
}
NSArray* despoofedArray = nil ;
if (patches) {
NSMutableArray* newArray = [self mutableCopy] ;
for (NSNumber* oIndex in patches) {
[newArray replaceObjectAtIndex:[oIndex intValue]
withObject:[patches objectForKey:oIndex]] ;
}
[patches release] ;
despoofedArray = [NSArray arrayWithArray:newArray] ;
[newArray release] ;
}
return despoofedArray ;
}
@end