-
Notifications
You must be signed in to change notification settings - Fork 252
a2i(): Maximal minimalism #1372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
cb1c2f7 to
b9472c3
Compare
lib/atoi/a2i.h
Outdated
| #define QChar(s) typeof \ | ||
| ( \ | ||
| _Generic(s, \ | ||
| const char *: *(const char *) "", \ | ||
| const void *: *(const char *) "", \ | ||
| char *: *(char *) "", \ | ||
| void *: *(char *) "" \ | ||
| ) \ | ||
| ) | ||
|
|
||
|
|
||
| // helper for a2i() | ||
| #define a2i_T(T, QChar, n, s, endp, base, min, max) \ | ||
| ({ \ | ||
| T *n_ = n; \ | ||
| QChar *s_ = s; \ | ||
| QChar **endp_ = endp; \ | ||
| int base_ = base; \ | ||
| T min_ = min; \ | ||
| T max_ = max; \ | ||
| \ | ||
| int status; \ | ||
| \ | ||
| *n_ = _Generic(T, \ | ||
| short: strtoi_, \ | ||
| int: strtoi_, \ | ||
| long: strtoi_, \ | ||
| long long: strtoi_, \ | ||
| unsigned short: strtou_noneg, \ | ||
| unsigned int: strtou_noneg, \ | ||
| unsigned long: strtou_noneg, \ | ||
| unsigned long long: strtou_noneg \ | ||
| )(s_, const_cast(char **, endp_), base_, min_, max_, &status);\ | ||
| \ | ||
| if (status != 0) \ | ||
| errno = status; \ | ||
| status == 0 ? 0 : -1; \ | ||
| }) | ||
|
|
||
|
|
||
| // a2i - alpha to integer | ||
| #define a2i(T, n, s, ...) a2i_T(T, QChar(s), n, s, __VA_ARGS__) | ||
|
|
||
| #define a2sh(...) a2i(short, __VA_ARGS__) | ||
| #define a2si(...) a2i(int, __VA_ARGS__) | ||
| #define a2sl(...) a2i(long, __VA_ARGS__) | ||
| #define a2sll(...) a2i(long long, __VA_ARGS__) | ||
|
|
||
| #define a2uh(...) a2i(unsigned short, __VA_ARGS__) | ||
| #define a2ui(...) a2i(unsigned int, __VA_ARGS__) | ||
| #define a2ul(...) a2i(unsigned long, __VA_ARGS__) | ||
| #define a2ull(...) a2i(unsigned long long, __VA_ARGS__) | ||
|
|
||
| #define str2i(T, ...) a2i(T, __VA_ARGS__, NULL, 0, type_min(T), type_max(T)) | ||
|
|
||
| #define str2sh(...) str2i(short, __VA_ARGS__) | ||
| #define str2si(...) str2i(int, __VA_ARGS__) | ||
| #define str2sl(...) str2i(long, __VA_ARGS__) | ||
| #define str2sll(...) str2i(long long, __VA_ARGS__) | ||
|
|
||
| #define str2uh(...) str2i(unsigned short, __VA_ARGS__) | ||
| #define str2ui(...) str2i(unsigned int, __VA_ARGS__) | ||
| #define str2ul(...) str2i(unsigned long, __VA_ARGS__) | ||
| #define str2ull(...) str2i(unsigned long long, __VA_ARGS__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've finally removed the need to read many layers of files, macros, and functions to understand what these APIs do!
Finally, this API set is much more readable now. :)
Cc: @ikerexxe , @thesamesam
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(There's still strtoi_() and strtou_noneg(), but those are really necessary.)
0f7e3f7 to
d83e5e2
Compare
d83e5e2 to
47c20df
Compare
5dd7f58 to
ff66739
Compare
a036cfb to
8f89996
Compare
This macro is useful to implement QChar versions of functions. See ISO C23 for a description of what QChar is. Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
8f89996 to
f9bb612
Compare
I've managed to find a way to implement a2i() in just a couple of simple macros; a short one for the body, one for massaging const, plus a one-liner for connecting both.
I need to test this in liba2i first. I'll keep as a draft here until I test it thoroughly. (Done.)
I've been trying to find a way to write it this simple for a looong time. Finally, I found it! :)
Edit: Testing in liba2i says all's good.
Revisions:
v2
v3
v3b
v4
v4b
v5
v5b
v6
v6b
v6c