-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSStringAdditions.m
154 lines (134 loc) · 5.5 KB
/
NSStringAdditions.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
//
// NSStringAdditions.m
// HotVOD
//
// Created by Guy Shaviv on 12/31/09.
// Copyright 2009 .. All rights reserved.
//
#import "NSStringAdditions.h"
#import "CGGeometryAdditions.h"
@implementation NSString (Additions)
- (int) hexValue {
int n = 0;
sscanf([self UTF8String], "%x", &n);
return n;
}
- (NSRange) entireStringRange {
return NSMakeRange(0, self.length);
}
- (NSString*)stringByAddingQueryDictionary:(NSDictionary*)query {
NSMutableArray* pairs = [NSMutableArray array];
[query enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSString* pair = [NSString stringWithFormat:@"%@=%@", key, [value stringByEscapingToURLSafe]];
[pairs addObject:pair];
}];
NSString* params = [pairs componentsJoinedByString:@"&"];
if ([self rangeOfString:@"?"].location == NSNotFound) {
return [self stringByAppendingFormat:@"?%@", params];
} else {
return [self stringByAppendingFormat:@"&%@", params];
}
}
- (NSDictionary*)dictionaryFromQueryUsingEncoding: (NSStringEncoding)encoding {
NSUInteger queryStart;
NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;?"] ;
if ((queryStart = [self rangeOfString:@"?"].location) == NSNotFound) {
queryStart = 0;
} else {
queryStart += 1;
}
NSMutableDictionary* pairs = [NSMutableDictionary dictionary] ;
NSScanner* scanner = [[NSScanner alloc] initWithString:self] ;
scanner.scanLocation = queryStart;
while (![scanner isAtEnd]) {
NSString* pairString ;
[scanner scanUpToCharactersFromSet:delimiterSet
intoString:&pairString] ;
[scanner scanCharactersFromSet:delimiterSet intoString:NULL] ;
NSArray* kvPair = [pairString componentsSeparatedByString:@"="] ;
if ([kvPair count] == 2) {
NSString* key = [kvPair[0] stringByReplacingPercentEscapesUsingEncoding:encoding] ; NSString* value = [kvPair[1] stringByReplacingPercentEscapesUsingEncoding:encoding] ;
pairs[key] = value ;
}
}
return [NSDictionary dictionaryWithDictionary:pairs] ;
}
+ (NSString *)globalUniqueIdentifier
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *str = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
CFRelease(uuid);
return str;
}
- (NSString *)stringByEscapingToURLSafe {
static NSDictionary *entityReverseLookup = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
entityReverseLookup = @{@"%": @"%25",
@"!": @"%21",
@"#": @"%23",
@"$": @"%24",
@"&": @"%26",
@"'": @"%27",
@"(": @"%28",
@")": @"%29",
@"*": @"%2A",
@"+": @"%2B",
@",": @"%2C",
@"/": @"%2F",
@":": @"%3A",
@";": @"%3B",
@"=": @"%3D",
@"?": @"%3F",
@"@": @"%40",
@"[": @"%5B",
@"]": @"%5D",
@"\n": @"%0A",
@" ": @"%20",
@"\"": @"%22",
@"-": @"%2D",
@".": @"%2E",
@"<": @"%3C",
@">": @"%3E",
@"\\": @"%5C",
@"^": @"%5E",
@"_": @"%5F",
@"`": @"%60",
@"{": @"%7B",
@"|": @"%7C",
@"}": @"%7D",
@"~": @"%7E"};
});
NSMutableString *tmpString = [NSMutableString string];
for (NSUInteger i = 0; i<[self length]; i++) {
unichar oneChar = [self characterAtIndex:i];
NSString *subKey = [self substringWithRange:NSMakeRange(i, 1)];
NSString *entity = entityReverseLookup[subKey];
if (entity) {
[tmpString appendString:entity];
} else {
if (oneChar<128) {
[tmpString appendFormat:@"%C", oneChar];
} else {
[tmpString appendFormat:@"%%26%%23%d;", oneChar];
}
}
}
return tmpString;
}
- (NSString*) stringByStrippingHTMLTags {
NSString *htmlStripped = nil;
@autoreleasepool {
static NSRegularExpression *tagEx = nil;
static NSRegularExpression *spaceEx = nil;
if (!tagEx) {
tagEx = [NSRegularExpression regularExpressionWithPattern:@"< *(\\/?)([-A-Za-z0-9._]+) *(.*?)(\\/?)>" options:NSRegularExpressionCaseInsensitive error:nil];
spaceEx = [NSRegularExpression regularExpressionWithPattern:@" +" options:0 error:nil];
}
NSString *brStripped = [self stringByReplacingOccurrencesOfString:@"<br/>" withString:@" "];
htmlStripped = [tagEx stringByReplacingMatchesInString:brStripped options:NSRegularExpressionCaseInsensitive range:NSRangeMake(0, brStripped.length) withTemplate:@""];
htmlStripped = [spaceEx stringByReplacingMatchesInString:htmlStripped options:0 range:NSRangeMake(0, htmlStripped.length) withTemplate:@" "];
}
return htmlStripped;
}
@end