Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is isascii() deprecated?

Tags:

c++

c

posix

ascii

libc

According to the isascii() manpage:

http://linux.die.net/man/3/isascii

POSIX.1-2008 marks isascii() as obsolete, noting that it cannot be used portably in a localized application.

I'm not sure I see where the portability problem is. A very simple implementation of this function is:

int isascii(int ch) { return ch >= 0 && ch < 128; }

In which situations is the above implementation either not sufficient or not portable?

Thank you

like image 214
Matthew Fioravante Avatar asked Sep 25 '14 05:09

Matthew Fioravante


1 Answers

I suppose it would not work if you have a character encoding that does not use the low seven-bit range exclusively for ASCII. Probably happens in some multibyte encodings, when the given byte is only part of the character.

For example, in Shift-JIS, the second byte can start at 0x40, which overlaps with ASCII. And even in the first byte, there are some slight alterations, such as 0x5C (currency symbol instead of backslash) or 0x7E (some sort of slash instead of tilde).

I found this article where someone explained the reason behind the non-inclusion of POSIX functions in their own OS design:

This function is rather pointless. If we use a character encoding that wasn't ascii compatible, then it doesn't make sense. If we use a sane character encoding such as UTF-8, then you can simply check if the value is at most 127.

like image 96
Thilo Avatar answered Oct 16 '22 07:10

Thilo