diff options
author | Alejandro Colomar <alx@kernel.org> | 2024-04-18 13:45:01 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2024-04-18 13:46:19 +0200 |
commit | c6e42ef8199f9b20e1f85d2e7df9bf9f46acd8eb (patch) | |
tree | 8fb6b7cd015f1fff4697023769a7546359bd0f90 | |
parent | 5562a2d020e8cef201b2c0461955f4e17f9f8201 (diff) | |
download | liba2i-c6e42ef8199f9b20e1f85d2e7df9bf9f46acd8eb.tar.gz |
include/a2i/, lib/src/a2i/: Remove helper macros, and type-traits macros
This adds some repetition, but allows removing many magic macros, so
overall, I think it makes the code simpler, and thus safer.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | include/a2i/str2i.h | 28 | ||||
-rw-r--r-- | include/a2i/typetraits.h | 31 | ||||
-rw-r--r-- | lib/src/a2i/typetraits.c | 5 |
3 files changed, 11 insertions, 53 deletions
diff --git a/include/a2i/str2i.h b/include/a2i/str2i.h index cd4aea8..3cdfcff 100644 --- a/include/a2i/str2i.h +++ b/include/a2i/str2i.h @@ -6,12 +6,12 @@ #define INCLUDE_A2I_STR2I_H_ +#include <limits.h> #include <stddef.h> #include <a2i/a2i.h> #include <a2i/attr.h> #include <a2i/inline.h> -#include <a2i/typetraits.h> #define str2i(TYPE, ...) \ @@ -56,12 +56,6 @@ ) -#define a2i_str2I(n, s) \ -( \ - a2i(typeof(*n), n, s, NULL, 0, a2i_typeof_min(*n), a2i_typeof_max(*n))\ -) - - #define A2I_STR2I_PROTOTYPE(name, TYPE) \ A2I_ATTR_ACCESS(write_only, 1) \ A2I_ATTR_ACCESS(read_only, 2) \ @@ -88,70 +82,70 @@ A2I_STR2I_PROTOTYPE(str2ull, unsigned long long); a2i_inline int str2shh(signed char *restrict n, const char *restrict s) { - return a2i_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, 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_str2I(n, s); + return a2i(unsigned long long, n, s, NULL, 0, 0, ULLONG_MAX); } #if defined(__clang__) # pragma clang assume_nonnull end diff --git a/include/a2i/typetraits.h b/include/a2i/typetraits.h deleted file mode 100644 index f88c2e0..0000000 --- a/include/a2i/typetraits.h +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org> -// SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception - - -#ifndef INCLUDE_A2I_TYPETRAITS_H_ -#define INCLUDE_A2I_TYPETRAITS_H_ - - -#include <limits.h> - - -#define a2i_is_unsigned(x) (((typeof(x)) -1) > 1) -#define a2i_is_signed(x) (((typeof(x)) -1) < 1) - -#define a2i_widthof(x) (sizeof(x) * CHAR_BIT) - -#define a2i_stype_max(T) \ -( \ - (T) (((((T) 1 << (a2i_widthof(T) - 2)) - 1) << 1) + 1) \ -) -#define a2i_utype_max(T) ((T) -1) -#define a2i_type_max(T) \ -( \ - (T) (a2i_is_signed(T) ? a2i_stype_max(T) : a2i_utype_max(T)) \ -) -#define a2i_type_min(T) ((T) ~a2i_type_max(T)) -#define a2i_typeof_max(x) a2i_type_max(typeof(x)) -#define a2i_typeof_min(x) a2i_type_min(typeof(x)) - - -#endif // include guard diff --git a/lib/src/a2i/typetraits.c b/lib/src/a2i/typetraits.c deleted file mode 100644 index 553ed68..0000000 --- a/lib/src/a2i/typetraits.c +++ /dev/null @@ -1,5 +0,0 @@ -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org> -// SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception - - -#include <a2i/typetraits.h> |