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

http://macfuse.googlecode.com/ · Objective C · 46 lines · 21 code · 6 blank · 19 comment · 0 complexity · c546fc7e40fccf4d9253880f86c25da5 MD5 · raw file

  1. //
  2. // GTMNSString+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 "GTMNSString+URLArguments.h"
  19. #import "GTMDefines.h"
  20. @implementation NSString (GTMNSStringURLArgumentsAdditions)
  21. - (NSString*)gtm_stringByEscapingForURLArgument {
  22. // Encode all the reserved characters, per RFC 3986
  23. // (<http://www.ietf.org/rfc/rfc3986.txt>)
  24. CFStringRef escaped =
  25. CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
  26. (CFStringRef)self,
  27. NULL,
  28. (CFStringRef)@"!*'();:@&=+$,/?%#[]",
  29. kCFStringEncodingUTF8);
  30. return GTMCFAutorelease(escaped);
  31. }
  32. - (NSString*)gtm_stringByUnescapingFromURLArgument {
  33. NSMutableString *resultString = [NSMutableString stringWithString:self];
  34. [resultString replaceOccurrencesOfString:@"+"
  35. withString:@" "
  36. options:NSLiteralSearch
  37. range:NSMakeRange(0, [resultString length])];
  38. return [resultString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  39. }
  40. @end