/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. #import "GTMNSDictionary+URLArguments.h"
  19. #import "GTMNSString+URLArguments.h"
  20. #import "GTMMethodCheck.h"
  21. #import "GTMDefines.h"
  22. @implementation NSDictionary (GTMNSDictionaryURLArgumentsAdditions)
  23. GTM_METHOD_CHECK(NSString, gtm_stringByEscapingForURLArgument);
  24. GTM_METHOD_CHECK(NSString, gtm_stringByUnescapingFromURLArgument);
  25. + (NSDictionary *)gtm_dictionaryWithHttpArgumentsString:(NSString *)argString {
  26. NSMutableDictionary* ret = [NSMutableDictionary dictionary];
  27. NSArray* components = [argString componentsSeparatedByString:@"&"];
  28. NSString* component;
  29. // Use reverse order so that the first occurrence of a key replaces
  30. // those subsequent.
  31. GTM_FOREACH_ENUMEREE(component, [components reverseObjectEnumerator]) {
  32. if ([component length] == 0)
  33. continue;
  34. NSRange pos = [component rangeOfString:@"="];
  35. NSString *key;
  36. NSString *val;
  37. if (pos.location == NSNotFound) {
  38. key = [component gtm_stringByUnescapingFromURLArgument];
  39. val = @"";
  40. } else {
  41. key = [[component substringToIndex:pos.location]
  42. gtm_stringByUnescapingFromURLArgument];
  43. val = [[component substringFromIndex:pos.location + pos.length]
  44. gtm_stringByUnescapingFromURLArgument];
  45. }
  46. // gtm_stringByUnescapingFromURLArgument returns nil on invalid UTF8
  47. // and NSMutableDictionary raises an exception when passed nil values.
  48. if (!key) key = @"";
  49. if (!val) val = @"";
  50. [ret setObject:val forKey:key];
  51. }
  52. return ret;
  53. }
  54. - (NSString *)gtm_httpArgumentsString {
  55. NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:[self count]];
  56. NSString* key;
  57. GTM_FOREACH_KEY(key, self) {
  58. [arguments addObject:[NSString stringWithFormat:@"%@=%@",
  59. [key gtm_stringByEscapingForURLArgument],
  60. [[[self objectForKey:key] description] gtm_stringByEscapingForURLArgument]]];
  61. }
  62. return [arguments componentsJoinedByString:@"&"];
  63. }
  64. @end