aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSDictionary+URLArguments.m
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-07-29 02:00:24 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2009-07-29 02:00:24 +0000
commiteff8351b590bcf190b175e2add2ab87c8e7204ec (patch)
treebf73a229e0bf1d397d93461fea55e076c5fa87a4 /Foundation/GTMNSDictionary+URLArguments.m
parentb65c5509d204b37eb79af1307f04a048806def48 (diff)
[Author: iwade]
Add gtm_dictionaryWithHttpArgumentsString to NSDictionary+URLArguments. R=thomasvl APPROVED=thomasvl DELTA=73 (72 added, 0 deleted, 1 changed)
Diffstat (limited to 'Foundation/GTMNSDictionary+URLArguments.m')
-rw-r--r--Foundation/GTMNSDictionary+URLArguments.m31
1 files changed, 31 insertions, 0 deletions
diff --git a/Foundation/GTMNSDictionary+URLArguments.m b/Foundation/GTMNSDictionary+URLArguments.m
index 89610e4..4799b2d 100644
--- a/Foundation/GTMNSDictionary+URLArguments.m
+++ b/Foundation/GTMNSDictionary+URLArguments.m
@@ -24,6 +24,37 @@
@implementation NSDictionary (GTMNSDictionaryURLArgumentsAdditions)
GTM_METHOD_CHECK(NSString, gtm_stringByEscapingForURLArgument);
+GTM_METHOD_CHECK(NSString, gtm_stringByUnescapingFromURLArgument);
+
++ (NSDictionary *)gtm_dictionaryWithHttpArgumentsString:(NSString *)argString {
+ NSMutableDictionary* ret = [NSMutableDictionary dictionary];
+ NSArray* components = [argString componentsSeparatedByString:@"&"];
+ NSString* component;
+ // Use reverse order so that the first occurrence of a key replaces
+ // those subsequent.
+ GTM_FOREACH_ENUMEREE(component, [components reverseObjectEnumerator]) {
+ if ([component length] == 0)
+ continue;
+ NSRange pos = [component rangeOfString:@"="];
+ NSString *key;
+ NSString *val;
+ if (pos.location == NSNotFound) {
+ key = [component gtm_stringByUnescapingFromURLArgument];
+ val = @"";
+ } else {
+ key = [[component substringToIndex:pos.location]
+ gtm_stringByUnescapingFromURLArgument];
+ val = [[component substringFromIndex:pos.location + pos.length]
+ gtm_stringByUnescapingFromURLArgument];
+ }
+ // gtm_stringByUnescapingFromURLArgument returns nil on invalid UTF8
+ // and NSMutableDictionary raises an exception when passed nil values.
+ if (!key) key = @"";
+ if (!val) val = @"";
+ [ret setObject:val forKey:key];
+ }
+ return ret;
+}
- (NSString *)gtm_httpArgumentsString {
NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:[self count]];