forked from regehr/str2long_contest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbastian.c
60 lines (44 loc) · 771 Bytes
/
bastian.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
50
51
52
53
54
55
56
57
58
59
60
#include "str2long.h"
long str2long_bastian (const char *s)
{
long result = 0;
if( *s == '-' )
{
++s;
if(*s == '\0')
goto str2long_error;
while(*s != '\0')
{
if(*s < '0' || *s > '9')
goto str2long_error;
if(result < LONG_MIN / 10)
goto str2long_error;
result *= 10;
if(LONG_MIN + (*s - '0') > result)
goto str2long_error;
result -= *s - '0';
++s;
}
}
else
{
if(*s == '\0')
goto str2long_error;
while(*s != '\0')
{
if(*s < '0' || *s > '9')
goto str2long_error;
if(result > LONG_MAX / 10)
goto str2long_error;
result *= 10;
if(LONG_MAX - (*s - '0') < result)
goto str2long_error;
result += *s - '0';
++s;
}
}
return result;
str2long_error:
error = 1;
return 0;
}