-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtil.m
150 lines (123 loc) · 3.89 KB
/
StringUtil.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
//
// StringUtil.m
// TwitterFon
//
// Created by kaz on 7/20/08.
// Copyright 2008 naan studio. All rights reserved.
//
#import "StringUtil.h"
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@implementation NSString (NSStringUtils)
- (NSString*)encodeAsURIComponent
{
const char* p = [self UTF8String];
NSMutableString* result = [NSMutableString string];
for (;*p ;p++) {
unsigned char c = *p;
if ('0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '-' || c == '_') {
[result appendFormat:@"%c", c];
} else {
[result appendFormat:@"%%%02X", c];
}
}
return result;
}
+ (NSString*)base64encode:(NSString*)str
{
if ([str length] == 0)
return @"";
const char *source = [str UTF8String];
int strlength = strlen(source);
char *characters = malloc(((strlength + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < strlength) {
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < strlength)
buffer[bufferLength++] = source[i++];
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}
- (NSString*)escapeHTML
{
NSMutableString* s = [NSMutableString string];
int start = 0;
int len = [self length];
NSCharacterSet* chs = [NSCharacterSet characterSetWithCharactersInString:@"<>&\""];
while (start < len) {
NSRange r = [self rangeOfCharacterFromSet:chs options:0 range:NSMakeRange(start, len-start)];
if (r.location == NSNotFound) {
[s appendString:[self substringFromIndex:start]];
break;
}
if (start < r.location) {
[s appendString:[self substringWithRange:NSMakeRange(start, r.location-start)]];
}
switch ([self characterAtIndex:r.location]) {
case '<':
[s appendString:@"<"];
break;
case '>':
[s appendString:@">"];
break;
case '"':
[s appendString:@"""];
break;
case '&':
[s appendString:@"&"];
break;
}
start = r.location + 1;
}
return s;
}
- (NSString*)unescapeHTML
{
NSMutableString* s = [NSMutableString string];
NSMutableString* target = [[self mutableCopy] autorelease];
NSCharacterSet* chs = [NSCharacterSet characterSetWithCharactersInString:@"&"];
while ([target length] > 0) {
NSRange r = [target rangeOfCharacterFromSet:chs];
if (r.location == NSNotFound) {
[s appendString:target];
break;
}
if (r.location > 0) {
[s appendString:[target substringToIndex:r.location]];
[target deleteCharactersInRange:NSMakeRange(0, r.location)];
}
if ([target hasPrefix:@"<"]) {
[s appendString:@"<"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@">"]) {
[s appendString:@">"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@"""]) {
[s appendString:@"\""];
[target deleteCharactersInRange:NSMakeRange(0, 6)];
} else if ([target hasPrefix:@"&"]) {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 5)];
} else {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 1)];
}
}
return s;
}
+ (NSString*)localizedString:(NSString*)key
{
return [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:key];
}
@end