GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: An iPhoto plugin to export photos to Gallery.
Homepage: http://zwily.com/iphoto
Clone URL: git://github.com/zwily/iphototogallery.git
iphototogallery / Source / NSString+misc.m
100644 286 lines (247 sloc) 8.701 kb
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// Copyright (c) Zach Wily
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// - Neither the name of Zach Wily nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
 
#import "NSString+misc.h"
 
@implementation NSString(URLEscaping)
 
static inline BOOL isUrlAlpha(unsigned char _c) {
    return
    (((_c >= 'a') && (_c <= 'z')) ||
     ((_c >= 'A') && (_c <= 'Z')))
    ? YES : NO;
}
static inline BOOL isUrlDigit(unsigned char _c) {
    return ((_c >= '0') && (_c <= '9')) ? YES : NO;
}
static inline BOOL isUrlSafeChar(unsigned char _c) {
    switch (_c) {
        case '$': case '-': case '_': case '@':
        case '.': case '&': case '+':
            return YES;
 
        default:
            return NO;
    }
}
static inline BOOL isUrlExtraChar(unsigned char _c) {
    switch (_c) {
        case '!': case '*': case '"': case '\'':
        case '|': case ',':
            return YES;
    }
    return NO;
}
static inline BOOL isUrlEscapeChar(unsigned char _c) {
    return (_c == '%') ? YES : NO;
}
static inline BOOL isUrlReservedChar(unsigned char _c) {
    switch (_c) {
        case '=': case ';': case '/':
        case '#': case '?': case ':':
        case ' ':
            return YES;
    }
    return NO;
}
 
static inline BOOL isUrlXalpha(unsigned char _c) {
    if (isUrlAlpha(_c)) return YES;
    if (isUrlDigit(_c)) return YES;
    if (isUrlSafeChar(_c)) return YES;
    if (isUrlExtraChar(_c)) return YES;
    if (isUrlEscapeChar(_c)) return YES;
    return NO;
}
 
static inline BOOL isUrlHexChar(unsigned char _c) {
    if (isUrlDigit(_c))
        return YES;
    if ((_c >= 'a') && (_c <= 'f'))
        return YES;
    if ((_c >= 'A') && (_c <= 'F'))
        return YES;
    return NO;
}
 
static inline BOOL isUrlAlphaNum(unsigned char _c) {
    return (isUrlAlpha(_c) || isUrlDigit(_c)) ? YES : NO;
}
 
static inline BOOL isToBeEscaped(unsigned char _c) {
    return (isUrlAlphaNum(_c) || (_c == '_')) ? NO : YES;
}
 
static void
NGEscapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
{
    register const unsigned char *src = (void*)_source;
    while (*src) {
        //if (*src == ' ') { // a ' ' becomes a '+'
        // *_dest = '+'; _dest++;
        //}
        if (!isToBeEscaped(*src)) {
            *_dest = *src;
            _dest++;
        }
        else { // any other char is escaped ..
            *_dest = '%'; _dest++;
            sprintf(_dest, "%02X", (unsigned)*src);
            _dest += 2;
        }
        src++;
    }
*_dest = '\0';
}
 
static inline int _valueOfHexChar(register unichar _c) {
    switch (_c) {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            return (_c - 48); // 0-9 (ascii-char)'0' - 48 => (int)0
 
        case 'A': case 'B': case 'C':
        case 'D': case 'E': case 'F':
            return (_c - 55); // A-F, A=10..F=15, 'A'=65..'F'=70
 
        case 'a': case 'b': case 'c':
        case 'd': case 'e': case 'f':
            return (_c - 87); // a-f, a=10..F=15, 'a'=97..'f'=102
 
        default:
            return -1;
    }
}
static inline BOOL _isHexDigit(register unichar _c) {
    switch (_c) {
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
        case 'A': case 'B': case 'C':
        case 'D': case 'E': case 'F':
        case 'a': case 'b': case 'c':
        case 'd': case 'e': case 'f':
            return YES;
 
        default:
            return NO;
    }
}
 
static void
NGUnescapeUrlBuffer(const unsigned char *_source, unsigned char *_dest)
{
    BOOL done = NO;
 
    while (!done && (*_source != '\0')) {
        char c = *_source;
 
        if (c == '+') // '+' stands for a space
          *_dest = ' ';
        else if (c == '%') {
            _source++; c = *_source;
 
            if (c == '\0') {
                *_dest = '%';
                done = YES;
            }
            else if (_isHexDigit(c)) { // hex-escaped char, like '%F3'
                int decChar = _valueOfHexChar(c);
                _source++;
                c = *_source;
                decChar = decChar * 16 + _valueOfHexChar(c);
                *_dest = (unsigned char)decChar;
            }
            else // escaped char, like '%%' -> '%'
                *_dest = c;
        }
        else // char passed through
            *_dest = c;
 
        _dest++;
        _source++;
    }
    *_dest = '\0';
}
 
- (BOOL)containsURLEscapeCharacters {
    register unsigned i, len;
    register unichar (*charAtIdx)(id,SEL,unsigned);
    register unichar charAtIndex;
 
    if ((len = [self length]) == 0) return NO;
 
    charAtIdx = (void*)[self methodForSelector:@selector(characterAtIndex:)];
    for (i = 0; i < len; i++) {
        charAtIndex = charAtIdx(self, @selector(characterAtIndex:), i);
        if ((charAtIndex == '%') || (charAtIndex == '+'))
            return YES;
    }
    return NO;
}
- (BOOL)containsURLInvalidCharacters {
    register unsigned i, len;
    register unichar (*charAtIdx)(id,SEL,unsigned);
 
    if ((len = [self length]) == 0) return NO;
 
    charAtIdx = (void*)[self methodForSelector:@selector(characterAtIndex:)];
    for (i = 0; i < len; i++) {
        if (isToBeEscaped(charAtIdx(self, @selector(characterAtIndex:), i)))
            return YES;
    }
    return NO;
}
 
- (NSString *)stringByUnescapingURL {
    unsigned len;
    char *cstr;
    char *buffer = NULL;
    NSString *s;
 
    if (![self containsURLEscapeCharacters])
        return [[self copy] autorelease];
 
    if ((len = [self cStringLength]) == 0) return @"";
 
    cstr = malloc(len + 10);
    [self getCString:cstr];
    cstr[len] = '\0';
 
    buffer = malloc(len + 2);
    NGUnescapeUrlBuffer(cstr, buffer);
    s = [[NSString alloc]
                 initWithCStringNoCopy:buffer
                                length:strlen(buffer)
                          freeWhenDone:YES];
    if (cstr) free(cstr);
    return [s autorelease];
}
 
NSString *URLEscapeString(NSString *str)
{
    NSString *escapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                  (CFStringRef)str,
                                                                                  NULL,
                                                                                  NULL,
                                                                                  kCFStringEncodingISOLatin1);
    return [escapedString autorelease];
}
 
- (NSString *)stringByEscapingURL {
    return URLEscapeString(self);
    
    unsigned len;
    char *cstr;
    NSString *s;
    char *buffer = NULL;
 
    if ((len = [self cStringLength]) == 0) return @"";
 
    if (![self containsURLInvalidCharacters]) // needs to be escaped ?
        return [[self copy] autorelease];
 
    cstr = malloc(len + 1);
    [self getCString:cstr];
    cstr[len] = '\0';
 
    buffer = malloc([self cStringLength] * 3 + 2);
    NGEscapeUrlBuffer(cstr, buffer);
 
    s = [[NSString alloc]
                 initWithCStringNoCopy:buffer
                                length:strlen(buffer)
                          freeWhenDone:YES];
    free(cstr);
    return [s autorelease];
}
 
@end