forked from soffes/sstoolkit
/
NSDictionary+SSToolkitAdditions.m
89 lines (65 loc) · 2.21 KB
/
NSDictionary+SSToolkitAdditions.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
//
// NSDictionary+SSToolkitAdditions.m
// SSToolkit
//
// Created by Sam Soffes on 9/21/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "NSDictionary+SSToolkitAdditions.h"
#import "NSString+SSToolkitAdditions.h"
#import "NSData+SSToolkitAdditions.h"
@interface NSDictionary (SSToolkitPrivateAdditions)
- (NSData *)_prehashData;
@end
@implementation NSDictionary (SSToolkitAdditions)
+ (NSDictionary *)dictionaryWithFormEncodedString:(NSString *)encodedString {
if (!encodedString) {
return nil;
}
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *pairs = [encodedString componentsSeparatedByString:@"&"];
for (NSString *kvp in pairs) {
if ([kvp length] == 0) {
continue;
}
NSRange pos = [kvp rangeOfString:@"="];
NSString *key;
NSString *val;
if (pos.location == NSNotFound) {
key = [kvp stringByUnescapingFromURLQuery];
val = @"";
} else {
key = [[kvp substringToIndex:pos.location] stringByUnescapingFromURLQuery];
val = [[kvp substringFromIndex:pos.location + pos.length] stringByUnescapingFromURLQuery];
}
if (!key || !val) {
continue; // I'm sure this will bite my arse one day
}
[result setObject:val forKey:key];
}
return result;
}
- (NSString *)stringWithFormEncodedComponents {
NSMutableArray *arguments = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
[arguments addObject:[NSString stringWithFormat:@"%@=%@",
[key stringByEscapingForURLQuery],
[[object description] stringByEscapingForURLQuery]]];
}];
return [arguments componentsJoinedByString:@"&"];
}
- (NSMutableDictionary *)deepMutableCopy {
return (__bridge_transfer NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (__bridge CFDictionaryRef)self, kCFPropertyListMutableContainers);
}
- (NSString *)MD5Sum {
return [[self _prehashData] MD5Sum];
}
- (NSString *)SHA1Sum {
return [[self _prehashData] SHA1Sum];
}
@end
@implementation NSDictionary (SSToolkitPrivateAdditions)
- (NSData *)_prehashData {
return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
}
@end