/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSScanner+Unsigned.m

http://macfuse.googlecode.com/ · Objective C · 60 lines · 36 code · 6 blank · 18 comment · 7 complexity · 6b0419bcd9e65c421355ac9c2476dbcb MD5 · raw file

  1. //
  2. // GTMNSScanner+Unsigned.m
  3. //
  4. // Copyright 2010 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 "GTMNSScanner+Unsigned.h"
  19. #include <stdlib.h>
  20. #include <limits.h>
  21. @implementation NSScanner (GTMUnsignedAdditions)
  22. - (BOOL)gtm_scanUnsignedInt:(unsigned int *)value {
  23. unsigned long long uLongLongValue = 0;
  24. BOOL wasGood = [self gtm_scanUnsignedLongLong:&uLongLongValue];
  25. if (wasGood && value) {
  26. if (uLongLongValue > UINT_MAX) {
  27. *value = UINT_MAX;
  28. } else {
  29. *value = (unsigned int)uLongLongValue;
  30. }
  31. }
  32. return wasGood;
  33. }
  34. - (BOOL)gtm_scanUInteger:(NSUInteger *)value {
  35. #if defined(__LP64__) && __LP64__
  36. return [self gtm_scanUnsignedLongLong:(unsigned long long*)value];
  37. #else
  38. return [self gtm_scanUnsignedInt:value];
  39. #endif // defined(__LP64__) && __LP64__
  40. }
  41. - (BOOL)gtm_scanUnsignedLongLong:(unsigned long long *)value {
  42. // Slow path
  43. NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
  44. NSString *digitString = nil;
  45. BOOL wasGood = [self scanCharactersFromSet:decimalSet intoString:&digitString];
  46. if (wasGood) {
  47. const char *digitChars = [digitString UTF8String];
  48. if (value) {
  49. *value = strtoull(digitChars, NULL, 10);
  50. }
  51. }
  52. return wasGood;
  53. }
  54. @end