summaryrefslogtreecommitdiffstats
path: root/include/a2i/str2i.h
blob: e315337ea7f64d3b4444756c1fc327a70187fb59 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception


#ifndef INCLUDE_A2I_STR2I_H_
#define INCLUDE_A2I_STR2I_H_


#include <limits.h>
#include <stddef.h>

#include <a2i/a2i.h>
#include <a2i/attr.h>
#include <a2i/inline.h>


#define str2i(TYPE, ...)                                                      \
(                                                                             \
	_Generic((TYPE) 0,                                                    \
		signed char:         str2shh,                                 \
		short:               str2sh,                                  \
		int:                 str2si,                                  \
		long:                str2sl,                                  \
		long long:           str2sll,                                 \
                                                                              \
		unsigned char:       str2uhh,                                 \
		unsigned short:      str2uh,                                  \
		unsigned int:        str2ui,                                  \
		unsigned long:       str2ul,                                  \
		unsigned long long:  str2ull                                  \
	)(__VA_ARGS__)                                                        \
)


#define str2s(TYPE, ...)                                                      \
(                                                                             \
	_Generic((TYPE) 0,                                                    \
		signed char:         str2shh,                                 \
		short:               str2sh,                                  \
		int:                 str2si,                                  \
		long:                str2sl,                                  \
		long long:           str2sll                                  \
	)(__VA_ARGS__)                                                        \
)


#define str2u(TYPE, ...)                                                      \
(                                                                             \
	_Generic((TYPE) 0,                                                    \
		unsigned char:       str2uhh,                                 \
		unsigned short:      str2uh,                                  \
		unsigned int:        str2ui,                                  \
		unsigned long:       str2ul,                                  \
		unsigned long long:  str2ull                                  \
	)(__VA_ARGS__)                                                        \
)


#define A2I_STR2I_PROTOTYPE(name, TYPE)                                       \
	A2I_ATTR_ACCESS(write_only, 1)                                        \
	A2I_ATTR_ACCESS(read_only, 2)                                         \
	A2I_ATTR_STRING(2)                                                    \
	a2i_inline int name(TYPE *restrict n, const char *s)


#if defined(__clang__)
# pragma clang assume_nonnull begin
#endif
A2I_STR2I_PROTOTYPE(str2shh, signed char);
A2I_STR2I_PROTOTYPE(str2sh, short);
A2I_STR2I_PROTOTYPE(str2si, int);
A2I_STR2I_PROTOTYPE(str2sl, long);
A2I_STR2I_PROTOTYPE(str2sll, long long);

A2I_STR2I_PROTOTYPE(str2uhh, unsigned char);
A2I_STR2I_PROTOTYPE(str2uh, unsigned short);
A2I_STR2I_PROTOTYPE(str2ui, unsigned int);
A2I_STR2I_PROTOTYPE(str2ul, unsigned long);
A2I_STR2I_PROTOTYPE(str2ull, unsigned long long);


a2i_inline int
str2shh(signed char *restrict n, const char *restrict s)
{
	return a2i(signed char, n, s, NULL, 0, SCHAR_MIN, SCHAR_MAX);
}


a2i_inline int
str2sh(short *restrict n, const char *restrict s)
{
	return a2i(short, n, s, NULL, 0, SHRT_MIN, SHRT_MAX);
}


a2i_inline int
str2si(int *restrict n, const char *restrict s)
{
	return a2i(int, n, s, NULL, 0, INT_MIN, INT_MAX);
}


a2i_inline int
str2sl(long *restrict n, const char *restrict s)
{
	return a2i(long, n, s, NULL, 0, LONG_MIN, LONG_MAX);
}


a2i_inline int
str2sll(long long *restrict n, const char *restrict s)
{
	return a2i(long long, n, s, NULL, 0, LLONG_MIN, LLONG_MAX);
}


a2i_inline int
str2uhh(unsigned char *restrict n, const char *restrict s)
{
	return a2i(unsigned char, n, s, NULL, 0, 0, UCHAR_MAX);
}


a2i_inline int
str2uh(unsigned short *restrict n, const char *restrict s)
{
	return a2i(unsigned short, n, s, NULL, 0, 0, USHRT_MAX);
}


a2i_inline int
str2ui(unsigned int *restrict n, const char *restrict s)
{
	return a2i(unsigned int, n, s, NULL, 0, 0, UINT_MAX);
}


a2i_inline int
str2ul(unsigned long *restrict n, const char *restrict s)
{
	return a2i(unsigned long, n, s, NULL, 0, 0, ULONG_MAX);
}


a2i_inline int
str2ull(unsigned long long *restrict n, const char *restrict s)
{
	return a2i(unsigned long long, n, s, NULL, 0, 0, ULLONG_MAX);
}
#if defined(__clang__)
# pragma clang assume_nonnull end
#endif


#endif  // include guard