-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSString+Utilities.m
executable file
·236 lines (193 loc) · 7.47 KB
/
NSString+Utilities.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
//
// StringUtil.m
#import "NSString+Utilities.h"
// https://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
static inline BOOL IsEmpty(id thing) {
return thing == nil ||
([thing isEqual:[NSNull null]]) ||
([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) ||
([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0);
}
@implementation NSString (NSStringUtils)
- (NSString *)stringWithMaxLength:(NSUInteger)maxLen {
NSUInteger length = [self length];
if (length <= maxLen || length <= 3) {
return self;
}else {
return [NSString stringWithFormat:@"%@...", [self substringToIndex:maxLen - 3]];
}
}
- (NSString *)urlWithoutParameters {
NSRange r;
NSString *newUrl;
r = [self rangeOfString:@"?" options: NSBackwardsSearch];
if (r.length > 0)
newUrl = [self substringToIndex: NSMaxRange (r) - 1];
else
newUrl = self;
return newUrl;
}
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString {
unsigned int bufferSize;
unsigned int selfLen = [self length];
unsigned int aStringLen = [aString length];
unichar *buffer;
NSRange localRange;
NSString *result;
bufferSize = selfLen + aStringLen - aRange.length;
buffer = NSAllocateMemoryPages(bufferSize*sizeof(unichar));
/* Get first part into buffer */
localRange.location = 0;
localRange.length = aRange.location;
[self getCharacters:buffer range:localRange];
/* Get middle part into buffer */
localRange.location = 0;
localRange.length = aStringLen;
[aString getCharacters:(buffer+aRange.location) range:localRange];
/* Get last part into buffer */
localRange.location = aRange.location + aRange.length;
localRange.length = selfLen - localRange.location;
[self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
/* Build output string */
result = [NSString stringWithCharacters:buffer length:bufferSize];
NSDeallocateMemoryPages(buffer, bufferSize);
return result;
}
- (NSString *)trimmedString
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString *)htmlDecodedString
{
NSMutableString *temp = [NSMutableString stringWithString:self];
[temp replaceOccurrencesOfString:@"&" withString:@"&" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@">" withString:@">" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"<" withString:@"<" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@""" withString:@"\"" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"'" withString:@"'" options:0 range:NSMakeRange(0, [temp length])];
return [NSString stringWithString:temp];
}
- (NSString *)htmlEncodedString
{
NSMutableString *temp = [NSMutableString stringWithString:self];
[temp replaceOccurrencesOfString:@"&" withString:@"&" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@">" withString:@">" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"<" withString:@"<" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"\"" withString:@""" options:0 range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"'" withString:@"'" options:0 range:NSMakeRange(0, [temp length])];
return [NSString stringWithString:temp];
}
/*
* source: https://stackoverflow.com/questions/1967399/parse-nsurl-path-and-query-iphoneos
*/
- (NSMutableDictionary *)explodeToDictionaryInnerGlue:(NSString *)innerGlue outterGlue:(NSString *)outterGlue {
// Explode based on outter glue
NSArray *firstExplode = [self componentsSeparatedByString:outterGlue];
NSArray *secondExplode;
// Explode based on inner glue
NSInteger count = [firstExplode count];
NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithCapacity:count];
for (NSInteger i = 0; i < count; i++) {
secondExplode = [(NSString *)[firstExplode objectAtIndex:i] componentsSeparatedByString:innerGlue];
if ([secondExplode count] == 2) {
[returnDictionary setObject:[secondExplode objectAtIndex:1] forKey:[secondExplode objectAtIndex:0]];
}
}
return returnDictionary;
}
- (NSMutableDictionary *)explodeToDictionaryInnerGlueUTF8Decode:(NSString *)innerGlue outterGlue:(NSString *)outterGlue isCompatibleMode:(BOOL) isCompatibleMode
{
NSMutableDictionary *srcDictionary = [self explodeToDictionaryInnerGlue:innerGlue outterGlue:outterGlue];
NSEnumerator* keyEnum = [srcDictionary keyEnumerator];
NSMutableDictionary* returnDictionary = [NSMutableDictionary dictionary];
id key = nil;
NSString* src = nil;
NSString* dec = nil;
while (key = [keyEnum nextObject])
{
src = [srcDictionary objectForKey:key];
if ([src isKindOfClass:[NSString class]])
{
if (isCompatibleMode)
{
src = [src stringByReplacingOccurrencesOfString:@"+" withString:@"%20"];
}
dec = [src stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([dec length] > 0)
{
src = dec;
}
}
if (key && [key lowercaseString])
{
[returnDictionary setObject:src forKey:[key lowercaseString]];
}
}
return returnDictionary;
}
+ (NSString *)firstNonNsNullStringWithString:(NSString*)string, ...
{
NSString* result = nil;
id arg = nil;
va_list argList;
if (string && [string isKindOfClass:[NSString class]])
{
return string;
}
va_start(argList, string);
while ((arg = va_arg(argList, id)))
{
if (arg && [arg isKindOfClass:[NSString class]])
{
result = arg;
break;
}
}
va_end(argList);
return result;
}
@end
//@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 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