Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 271 lines (216 sloc) 8.093 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
//
// NSString+SSToolkitAdditions.m
// SSToolkit
//
// Created by Sam Soffes on 6/22/09.
// Copyright 2009-2011 Sam Soffes. All rights reserved.
//

#import "NSString+SSToolkitAdditions.h"
#include <CommonCrypto/CommonDigest.h>

static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

@implementation NSString (SSToolkitAdditions)

- (BOOL)containsString:(NSString *)string {
return !NSEqualRanges([self rangeOfString:string], NSMakeRange(NSNotFound, 0));
}

- (NSString *)MD5Sum {
unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
CC_MD5([self UTF8String], [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
NSMutableString *ms = [NSMutableString string];
for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[ms appendFormat: @"%02x", (int)(digest[i])];
}
return [[ms copy] autorelease];
}


// Adapted from http://snipplr.com/view/2771/compare-two-version-strings
- (NSComparisonResult)compareToVersionString:(NSString *)version {
// Break version into fields (separated by '.')
NSMutableArray *leftFields = [[NSMutableArray alloc] initWithArray:[self componentsSeparatedByString:@"."]];
NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[version componentsSeparatedByString:@"."]];

// Implict ".0" in case version doesn't have the same number of '.'
if ([leftFields count] < [rightFields count]) {
while ([leftFields count] != [rightFields count]) {
[leftFields addObject:@"0"];
}
} else if ([leftFields count] > [rightFields count]) {
while ([leftFields count] != [rightFields count]) {
[rightFields addObject:@"0"];
}
}

// Do a numeric comparison on each field
for (NSUInteger i = 0; i < [leftFields count]; i++) {
NSComparisonResult result = [[leftFields objectAtIndex:i] compare:[rightFields objectAtIndex:i] options:NSNumericSearch];
if (result != NSOrderedSame) {
[leftFields release];
[rightFields release];
return result;
}
}

[leftFields release];
[rightFields release];
return NSOrderedSame;
}


#pragma mark Localization Methods

+ (NSString *)localizedString:(NSString *)key {
return [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:key];
}


#pragma mark HTML Methods

- (NSString *)escapeHTML {
NSMutableString *s = [NSMutableString string];

NSUInteger start = 0;
NSUInteger 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:@"&lt;"];
break;
case '>':
[s appendString:@"&gt;"];
break;
case '"':
[s appendString:@"&quot;"];
break;
// case '…':
// [s appendString:@"&hellip;"];
// break;
case '&':
[s appendString:@"&amp;"];
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:@"&lt;"]) {
[s appendString:@"<"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@"&gt;"]) {
[s appendString:@">"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@"&quot;"]) {
[s appendString:@"\""];
[target deleteCharactersInRange:NSMakeRange(0, 6)];
} else if ([target hasPrefix:@"&#39;"]) {
[s appendString:@"'"];
[target deleteCharactersInRange:NSMakeRange(0, 5)];
} else if ([target hasPrefix:@"&amp;"]) {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 5)];
} else if ([target hasPrefix:@"&hellip;"]) {
[s appendString:@"…"];
[target deleteCharactersInRange:NSMakeRange(0, 8)];
} else {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 1)];
}
}

return s;
}


#pragma mark URL Methods

- (NSString *)URLEncodedString {
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8) autorelease];
}


- (NSString *)URLEncodedParameterString {
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR(":/=,!$&'()*+;[]@#?"),
                                                                           kCFStringEncodingUTF8);
return [result autorelease];
}


- (NSString*)URLDecodedString {
return [(NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8) autorelease];
}


- (NSString *)removeQuotes {
NSUInteger length = [self length];
NSString *ret = self;
if ([self characterAtIndex:0] == '"') {
ret = [ret substringFromIndex:1];
}
if ([self characterAtIndex:length - 1] == '"') {
ret = [ret substringToIndex:length - 2];
}

return ret;
}


- (NSString*)stringByEscapingForURLQuery {
NSString *result = self;

CFStringRef originalAsCFString = (CFStringRef) self;
CFStringRef leaveAlone = CFSTR(" ");
CFStringRef toEscape = CFSTR("\n\r?[]()$,!'*;:@&=#%+/");

CFStringRef escapedStr;
escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, originalAsCFString, leaveAlone, toEscape, kCFStringEncodingUTF8);

if (escapedStr) {
NSMutableString *mutable = [NSMutableString stringWithString:(NSString *)escapedStr];
CFRelease(escapedStr);

[mutable replaceOccurrencesOfString:@" " withString:@"+" options:0 range:NSMakeRange(0, [mutable length])];
result = mutable;
}
return result;
}


- (NSString*)stringByUnescapingFromURLQuery {
return [[self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@" "];
}


#pragma mark Base64 Methods

- (NSString *)base64EncodedString {
    if ([self length] == 0) {
        return @"";
}

    const char *source = [self UTF8String];
    NSUInteger 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];
}

@end
Something went wrong with that request. Please try again.