-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathc_xatoll.c
More file actions
60 lines (60 loc) · 1.1 KB
/
c_xatoll.c
File metadata and controls
60 lines (60 loc) · 1.1 KB
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
/*
* A C implementation of xatoll().
*/
const char* c_xatoll(const char* string, long long* value) {
int sign = 0;
if (*string == '+')
++string;
else if (*string == '-') {
sign = 1;
++string;
}
*value = 0;
if (*string == '0' && (*(string + 1) | ('X' ^ 'x')) == 'x')
for (string += 2;;) {
signed char next = *string - '0';
if (next < 0)
goto end;
if (next > 9) {
next |= 'a' ^ 'A';
next -= 'a' - '0';
if (next < 0)
goto end;
if (next > 5)
goto end;
next += 10;
}
if (*value & 0xF800000000000000)
goto end;
*value <<= 4;
*value |= next;
++string;
}
while (*string == '0')
++string;
for (;;) {
signed char next = *string - '0';
#ifdef CXATOLL_OVERFLOW
long long result;
#endif
if (next < 0)
goto end;
if (next > 9)
goto end;
#ifdef CXATOLL_OVERFLOW
if (__builtin_smulll_overflow(*value, 10, &result))
goto end;
*value = result;
if (__builtin_saddll_overflow(*value, next, &result))
goto end;
*value = result;
#else
*value = 10**value + next;
#endif
++string;
}
end:
if (sign)
*value = -*value;
return string;
}