forked from ConnectSDK/Connect-SDK-iOS-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectUtil.m
123 lines (104 loc) · 4.71 KB
/
ConnectUtil.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
//
// ConnectUtil.m
// Connect SDK
//
// Created by Jeremy White on 3/6/14.
// Copyright (c) 2014 LG Electronics.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ConnectUtil.h"
@implementation ConnectUtil
+ (NSString *)urlEncode:(NSString *)targetString
{
NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *) [targetString UTF8String];
int sourceLen = (int) strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
const unsigned char thisChar = source[i];
// credit: http://www.w3schools.com/tags/ref_urlencode.asp
switch (thisChar)
{
case ' ': [output appendString:@"%20"]; break;
case '!': [output appendString:@"%21"]; break;
case '"': [output appendString:@"%22"]; break;
case '#': [output appendString:@"%23"]; break;
case '$': [output appendString:@"%24"]; break;
case '%': [output appendString:@"%25"]; break;
case '&': [output appendString:@"%26"]; break;
case '\'': [output appendString:@"%27"]; break;
case '(': [output appendString:@"%28"]; break;
case ')': [output appendString:@"%29"]; break;
case '*': [output appendString:@"%2A"]; break;
case '+': [output appendString:@"%2B"]; break;
case ',': [output appendString:@"%2C"]; break;
case '-': [output appendString:@"%2D"]; break;
case '.': [output appendString:@"%2E"]; break;
case '/': [output appendString:@"%2F"]; break;
case ':': [output appendString:@"%3A"]; break;
case ';': [output appendString:@"%3B"]; break;
case '<': [output appendString:@"%3C"]; break;
case '=': [output appendString:@"%3D"]; break;
case '>': [output appendString:@"%3E"]; break;
case '?': [output appendString:@"%3F"]; break;
case '@': [output appendString:@"%40"]; break;
default:
if ((thisChar >= 'a' && thisChar <= 'z') || (thisChar >= 'A' && thisChar <= 'Z') || (thisChar >= '0' && thisChar <= '9'))
[output appendFormat:@"%c", thisChar];
else
[output appendFormat:@"%%%02X", thisChar];
}
}
return output;
}
+ (NSString *)urlDecode:(NSString *)targetString
{
NSString *result = [targetString stringByReplacingOccurrencesOfString:@"+" withString:@" "];
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return result;
}
// credit: karsten @ stackoverflow.com (http://stackoverflow.com/users/272152/karsten)
// source: http://stackoverflow.com/a/2591544/2715
+ (NSString *) escapedUnicodeForString:(NSString *)input
{
NSMutableString *uniString = [ [ NSMutableString alloc ] init ];
UniChar *uniBuffer = (UniChar *) malloc ( sizeof(UniChar) * [ input length ] );
CFRange stringRange = CFRangeMake ( 0, [ input length ] );
CFStringGetCharacters ( (__bridge CFStringRef)input, stringRange, uniBuffer );
for ( int i = 0; i < [ input length ]; i++ ) {
if ( uniBuffer[i] < 0x30 || uniBuffer[i] > 0x7e )
[ uniString appendFormat: @"\\u%04x", uniBuffer[i] ];
else
[ uniString appendFormat: @"%c", uniBuffer[i] ];
}
free ( uniBuffer );
NSString *retString = [ NSString stringWithString: uniString ];
return retString;
}
+ (NSString *) entityEncode:(NSString *)input
{
NSString *output = [[[[input stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
stringByReplacingOccurrencesOfString: @"\"" withString: @"""]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
return output;
}
+ (NSString *) entityDecode:(NSString *)input
{
NSString *output = [[[[input stringByReplacingOccurrencesOfString: @"&" withString: @"&"]
stringByReplacingOccurrencesOfString: @""" withString: @"\""]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
return output;
}
@end