PageRenderTime 52ms CodeModel.GetById 9ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSDictionary+URLArguments.m

http://macfuse.googlecode.com/
Objective C | 71 lines | 43 code | 7 blank | 21 comment | 7 complexity | 2006c3391eaa369c618c44ec2f526bea MD5 | raw file
 1//
 2//  GTMNSDictionary+URLArguments.m
 3//
 4//  Copyright 2006-2008 Google Inc.
 5//
 6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 7//  use this file except in compliance with the License.  You may obtain a copy
 8//  of the License at
 9// 
10//  http://www.apache.org/licenses/LICENSE-2.0
11// 
12//  Unless required by applicable law or agreed to in writing, software
13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15//  License for the specific language governing permissions and limitations under
16//  the License.
17//
18
19#import "GTMNSDictionary+URLArguments.h"
20#import "GTMNSString+URLArguments.h"
21#import "GTMMethodCheck.h"
22#import "GTMDefines.h"
23
24@implementation NSDictionary (GTMNSDictionaryURLArgumentsAdditions)
25
26GTM_METHOD_CHECK(NSString, gtm_stringByEscapingForURLArgument);
27GTM_METHOD_CHECK(NSString, gtm_stringByUnescapingFromURLArgument);
28
29+ (NSDictionary *)gtm_dictionaryWithHttpArgumentsString:(NSString *)argString {
30  NSMutableDictionary* ret = [NSMutableDictionary dictionary];
31  NSArray* components = [argString componentsSeparatedByString:@"&"];
32  NSString* component;
33  // Use reverse order so that the first occurrence of a key replaces
34  // those subsequent.
35  GTM_FOREACH_ENUMEREE(component, [components reverseObjectEnumerator]) {
36    if ([component length] == 0)
37      continue;
38    NSRange pos = [component rangeOfString:@"="];
39    NSString *key;
40    NSString *val;
41    if (pos.location == NSNotFound) {
42      key = [component gtm_stringByUnescapingFromURLArgument];
43      val = @"";
44    } else {
45      key = [[component substringToIndex:pos.location]
46             gtm_stringByUnescapingFromURLArgument];
47      val = [[component substringFromIndex:pos.location + pos.length]
48             gtm_stringByUnescapingFromURLArgument];
49    }
50    // gtm_stringByUnescapingFromURLArgument returns nil on invalid UTF8
51    // and NSMutableDictionary raises an exception when passed nil values.
52    if (!key) key = @"";
53    if (!val) val = @"";
54    [ret setObject:val forKey:key];
55  }
56  return ret;
57}
58
59- (NSString *)gtm_httpArgumentsString {
60  NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:[self count]];
61  NSString* key;
62  GTM_FOREACH_KEY(key, self) {
63    [arguments addObject:[NSString stringWithFormat:@"%@=%@",
64                          [key gtm_stringByEscapingForURLArgument],
65                          [[[self objectForKey:key] description] gtm_stringByEscapingForURLArgument]]];
66  }
67
68  return [arguments componentsJoinedByString:@"&"];
69}
70
71@end