forked from regehr/str2long_contest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathahmed_2.c
More file actions
28 lines (27 loc) · 813 Bytes
/
ahmed_2.c
File metadata and controls
28 lines (27 loc) · 813 Bytes
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
#include "str2long.h"
long str2long_ahmed_2 (const char *s) {
long ret = 0;
if (!*s) error = 1;
_Bool negative = *s == '-';
if (negative) ++s;
while (*s) {
if (*s < '0' || *s > '9') {
error = 1;
return 0;
}
long i = *s - '0';
if (((LONG_MAX - i) / 10) < ret) {
if (negative && i != 0 && (((LONG_MAX - i) +
1) / 10) == ret && !s[1]) {
return LONG_MIN;
}
error = 1;
return 0;
}
ret *= 10;
ret += i;
++s;
}
if (negative) ret = -ret;
return ret;
}