From eff8351b590bcf190b175e2add2ab87c8e7204ec Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Wed, 29 Jul 2009 02:00:24 +0000 Subject: [Author: iwade] Add gtm_dictionaryWithHttpArgumentsString to NSDictionary+URLArguments. R=thomasvl APPROVED=thomasvl DELTA=73 (72 added, 0 deleted, 1 changed) --- Foundation/GTMNSDictionary+URLArguments.m | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'Foundation/GTMNSDictionary+URLArguments.m') 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]]; -- cgit v1.2.3