Skip to content
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

iscntrl() broken? #56

Open
th-otto opened this issue May 15, 2023 · 2 comments
Open

iscntrl() broken? #56

th-otto opened this issue May 15, 2023 · 2 comments

Comments

@th-otto
Copy link
Contributor

th-otto commented May 15, 2023

I ever wondered about the strange extra code for iscntrl() in ctype.h. I explicitly returns 0 for EOF(-1), and 1 for 255.

But i think that is wrong. In all environments where i tested it, only characters 0-31 and 127 return != for iscntrl, and in all cases both -1 and 255 return 0.

And there is also room for optimizations. Even if we want to keep the current behaviour, we do not need to explicitly check the 255 case. The same effect can be achieved by setting myctype[255] to zero. It would also by possible to get rid of the extra access through the _ctype pointer, and to make the array(s) unsigned char instead of int in order to save some space, essentially implementing it similar to libcmini.

Any suggestions?

@mikrosk
Copy link
Member

mikrosk commented May 22, 2023

The specification would suggest that you are correct: https://en.cppreference.com/w/cpp/string/byte/iscntrl

@xdelatour
Copy link
Contributor

I agree that only 0-31 and 127 are control characters: https://cplusplus.com/reference/cctype/iscntrl/, https://www.oreilly.com/library/view/c-in-a/0596006977/re117.html and https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/iscntrl-iswcntrl-iscntrl-l-iswcntrl-l?view=msvc-170

musl-libc uses:

return (unsigned)c < 0x20 || c == 0x7f;

Unfortunately the old thread (February and March 2000) on the mailing list doesn't help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants