PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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