-
Notifications
You must be signed in to change notification settings - Fork 0
/
strconv.c
56 lines (52 loc) · 1.25 KB
/
strconv.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
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include "strconv.h"
bool strconv_to_u32(const char *str, const char *strend, uint32_t *val)
{
char *endptr;
unsigned long r = strtoul(str, &endptr, 10);
if (r == ULONG_MAX && errno == ERANGE)
return false;
if (*str != '\0' && *endptr == *strend) {
*val = r;
return true;
}
return false;
}
bool strconv_to_u64(const char *str, const char *strend, uint64_t *val)
{
char *endptr;
unsigned long long r = strtoull(str, &endptr, 10);
if (r == ULLONG_MAX && errno == ERANGE)
return false;
if (*str != '\0' && *endptr == *strend) {
*val = r;
return true;
}
return false;
}
bool strconv_to_i32(const char *str, const char *strend, int32_t *val)
{
char *endptr;
long r = strtol(str, &endptr, 10);
if ((r == LONG_MAX || r == LONG_MIN) && errno == ERANGE)
return false;
if (*str != '\0' && *endptr == *strend) {
*val = r;
return true;
}
return false;
}
bool strconv_to_i64(const char *str, const char *strend, int64_t *val)
{
char *endptr;
long long r = strtoll(str, &endptr, 10);
if ((r == LLONG_MAX || r == LLONG_MIN) && errno == ERANGE)
return false;
if (*str != '\0' && *endptr == *strend) {
*val = r;
return true;
}
return false;
}