-
Notifications
You must be signed in to change notification settings - Fork 254
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cb1c2f7 to
b9472c3
Compare
0f7e3f7 to
d83e5e2
Compare
d83e5e2 to
47c20df
Compare
5dd7f58 to
ff66739
Compare
a036cfb to
8f89996
Compare
8f89996 to
f9bb612
Compare
cf9fab5 to
f9bb612
Compare
Collaborator
Author
hallyn
reviewed
Nov 4, 2025
df39149 to
3818553
Compare
a2d85a7 to
d6a827d
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 <alx@kernel.org>
Synopsis int a2i(typename T, T *restrict n, QChar *s, QChar **_Nullable restrict endp, int base, T min, T max); Description This macro converts the initial portion of the string pointed to by 's' to an integer of base 'base', ensure that the number is in the range [min, max], and store it in *n. It is similar to NetBSD's strtoi(3) and strtou(3), which themselves are similar to strtol(3) and strtoul(3). Arguments T The integer type used for the number. n A pointer to an integer. The parsed number will be stored there. s See strtol(3). endp See strtol(3). A difference with strtol(3) is that this macro is const-correct. If 's' has type 'const char *', then 'endp' must have type 'const char **', whereas if 's' has type 'char *', 'endp' must have type 'char **'. base See strtol(3). min max See strtoi(3) and strtou(3). An important difference with NetBSD's strtou(3) is that a2i() (with an unsigned type T) doesn't intepret any negative numbers as if they were large positive numbers. a2i() respects the limits [min, max] as one would intuitively expect. Return value On success, 0 is returned. On error, -1 is returned and errno is set to indicate the error. Errors See strtoi(3) and strtou(3). Examples if (a2i(pid_t, &pid, s, &s, 10, 1, _Maxof(pid_t)) == -1) goto err; Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
d6a827d to
f20144b
Compare
hallyn
approved these changes
Nov 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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've tested this thoroughly in liba2i's test suite.
I've been trying to find a way to write it this simple for a looong time. Finally, I found it! :)
Revisions:
v2
v3
v3b
v4
v4b
v5
v5b
v6
v6b
v6c
v6d
v6e
v6f
v6g
v6h
v6i
v6j
v6k
v7
v8
v8b
v9