Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions lib/libc/minimal/include/ctype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Copy link
Contributor

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.

}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

static inline int isspace (int c)
{
    return c == ' ' || ('\t' <= c && c <= '\r');
}

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 '\t' and '\r' is something the compiler should do, not us.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand All @@ -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));
Expand Down