forked from regehr/str2long_contest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidney.c
More file actions
30 lines (24 loc) · 633 Bytes
/
sidney.c
File metadata and controls
30 lines (24 loc) · 633 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
29
30
#include "str2long.h"
long str2long_sidney (const char *in) {
int negative = (*in == '-');
long out = 0;
if (negative) in++;
if (!*in) goto failure;
for (; *in; in++) {
if (*in < '0' || *in > '9') goto failure;
int next = *in - '0';
if (negative) {
if (out < LONG_MIN / 10 || LONG_MIN - (out *= 10) > -next
) goto failure;
out -= next;
} else {
if (out > LONG_MAX / 10 || LONG_MAX - (out *= 10) < next
) goto failure;
out += next;
}
}
return out;
failure:
error = 1;
return 0;
}