-
Notifications
You must be signed in to change notification settings - Fork 8.2k
libc: minimal: add missing ctype.h functions #99451
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,42 +15,57 @@ extern "C" { | |
|
|
||
| static inline int isupper(int a) | ||
| { | ||
| return (int)(((unsigned)(a)-(unsigned)'A') < 26U); | ||
| return (int)(((int)'A' <= a) && (a <= (int)'Z')); | ||
| } | ||
|
|
||
| static inline int isalpha(int c) | ||
| { | ||
| return (int)((((unsigned)c|32u)-(unsigned)'a') < 26U); | ||
| /* force to lowercase */ | ||
| c |= 32U; | ||
|
|
||
| return (int)(((int)'a' <= c) && (c <= (int)'z')); | ||
| } | ||
|
|
||
| static inline int isblank(int c) | ||
| { | ||
| return (int)((c == (int)' ') || (c == (int)'\t')); | ||
| } | ||
|
|
||
| static inline int isspace(int c) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd let the compiler do something cute if it likes, but express these in a direct fashion. Similarly for the other functions. There's no reason to obscure what these functions do; having to check that there are 5 'blank' values between
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll prepend a commit for that, since it's technically not related to the bugfix.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could also prepend a commit that would eliminate linter issues as well (and fix up the commit I added). |
||
| { | ||
| return (int)(c == (int)' ' || ((unsigned)c-(unsigned)'\t') < 5U); | ||
| return (int)((c == (int)' ') || (((int)'\t' <= c) && (c <= (int)'\r'))); | ||
| } | ||
|
|
||
| static inline int isgraph(int c) | ||
| { | ||
| return (int)((((unsigned)c) > ' ') && | ||
| (((unsigned)c) <= (unsigned)'~')); | ||
| return (int)(((int)' ' < c) && (c <= (int)'~')); | ||
| } | ||
|
|
||
| static inline int isprint(int c) | ||
| { | ||
| return (int)((((unsigned)c) >= ' ') && | ||
| (((unsigned)c) <= (unsigned)'~')); | ||
| return (int)(((int)' ' <= c) && (c <= (int)'~')); | ||
| } | ||
|
|
||
| static inline int isdigit(int a) | ||
| { | ||
| return (int)(((unsigned)(a)-(unsigned)'0') < 10U); | ||
| return (int)(((int)'0' <= a) && (a <= (int)'9')); | ||
| } | ||
|
|
||
| static inline int islower(int c) | ||
| { | ||
| return (int)(((int)'a' <= c) && (c <= (int)'z')); | ||
| } | ||
|
|
||
| static inline int isxdigit(int a) | ||
| { | ||
| unsigned int ua = (unsigned int)a; | ||
| if (isdigit(a) != 0) { | ||
| return 1; | ||
| } | ||
|
|
||
| /* force to lowercase */ | ||
| a |= 32U; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tolower uses addition instead of bitwise OR; I'd be consistent here. |
||
|
|
||
| return (int)(((ua - (unsigned)'0') < 10U) || | ||
| ((ua | 32U) - (unsigned)'a' < 6U)); | ||
| return (int)(((int)'a' <= a) && (a <= (int)'f')); | ||
| } | ||
|
|
||
| static inline int tolower(int chr) | ||
|
|
@@ -69,6 +84,11 @@ static inline int isalnum(int chr) | |
| return (int)(isalpha(chr) || isdigit(chr)); | ||
| } | ||
|
|
||
| static inline int ispunct(int c) | ||
| { | ||
| return (int)(isgraph(c) && !isalnum(c)); | ||
| } | ||
|
|
||
| static inline int iscntrl(int c) | ||
| { | ||
| return (int)((((unsigned int)c) <= 31U) || (((unsigned int)c) == 127U)); | ||
|
|
||
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.
You don't need any casts in this expression -- character constants are of type int, the relational operators result is of type int and the logical AND operator result is of type int. This should be just
return 'A' <= a && a <= 'Z';Feel free to add parentheses if you like, they aren't required as the precedence works correctly for this case.