-
Notifications
You must be signed in to change notification settings - Fork 2
/
c_xatoull.c
49 lines (49 loc) · 1.01 KB
/
c_xatoull.c
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
/*
* A C implementation of xatoull().
*/
const char* c_xatoull(const char* string, unsigned long long* value) {
*value = 0;
if (*string == '0' && (*(string + 1) | ('X' ^ 'x')) == 'x')
for (string += 2;;) {
signed char next = *string - '0';
if (next < 0)
return string;
if (next > 9) {
next |= 'a' ^ 'A';
next -= 'a' - '0';
if (next < 0)
return string;
if (next > 5)
return string;
next += 10;
}
if (*value & 0xF000000000000000)
return string;
*value <<= 4;
*value |= next;
++string;
}
while (*string == '0')
++string;
for (;;) {
signed char next = *string - '0';
#ifdef CXATOULL_OVERFLOW
unsigned long long result;
#endif
if (next < 0)
return string;
if (next > 9)
return string;
#ifdef CXATOULL_OVERFLOW
if (__builtin_umulll_overflow(*value, 10, &result))
return string;
*value = result;
if (__builtin_uaddll_overflow(*value, next, &result))
return string;
*value = result;
#else
*value = 10**value + next;
#endif
++string;
}
}