-
Notifications
You must be signed in to change notification settings - Fork 10
/
DDNumber.m
74 lines (57 loc) · 1.25 KB
/
DDNumber.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#import "DDNumber.h"
@implementation NSNumber (DDNumber)
+ (BOOL)parseString:(NSString *)str intoSInt64:(SInt64 *)pNum
{
errno = 0;
#if __LP64__
// long = 64 bit
*pNum = strtol([str UTF8String], NULL, 10);
#else
// long = 32 bit
// long long = 64 bit
*pNum = strtoll([str UTF8String], NULL, 10);
#endif
if(errno != 0)
return NO;
else
return YES;
}
+ (BOOL)parseString:(NSString *)str intoUInt64:(UInt64 *)pNum
{
errno = 0;
#if __LP64__
// unsigned long = 64 bit
*pNum = strtoul([str UTF8String], NULL, 10);
#else
// unsigned long = 32 bit
// unsigned long long = 64 bit
*pNum = strtoull([str UTF8String], NULL, 10);
#endif
if(errno != 0)
return NO;
else
return YES;
}
+ (BOOL)parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum
{
errno = 0;
// On LP64, NSInteger = long = 64 bit
// Otherwise, NSInteger = int = long = 32 bit
*pNum = strtol([str UTF8String], NULL, 10);
if(errno != 0)
return NO;
else
return YES;
}
+ (BOOL)parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum
{
errno = 0;
// On LP64, NSUInteger = unsigned long = 64 bit
// Otherwise, NSUInteger = unsigned int = unsigned long = 32 bit
*pNum = strtoul([str UTF8String], NULL, 10);
if(errno != 0)
return NO;
else
return YES;
}
@end