by gumby · 5y ago · 0 comments
glibc needs to solve two hard problems: be very fast and run on innumerable systems. Some of that conditional stuff is because all the world is not Linux or BSD; some of the macrology is there to make sure such handling is performed everywhere needed, and of course the preprocessor is the closest a language like C can get to preprocessing.
I was in the code as glibc started to exist (we paid for a lot of it) and it looked like Musl: very straightforward.
And... yes, glibc does have support for EBCDIC, which is probably ultimately why it has these run-time indirections in its ctype. There's no other reason to have run-time indirections for ctype functions given the limitation of unsigned char values + EOF. That means this code can be simplified a great deal.
Anyways, yes, Drew DeVault's rant misses glibc's need to support EBCDIC, but glibc is exactly like this for every little thing -- an unmaintainable mess. There has to be a better way to produce a fast C library w/o being such a mess on the inside.
Instead:
- use UTF-8 locales
- use a Unicode library for all things Unicode
- make your own ctype for when it's Just ASCIIIn a given locale, you can almost certainly regard wchar_t as being a continuation of the range of unsigned char. If you want to know whether the value UCHAR_MAX + 1 is alpha-numeric, you can't pass that to isalnum, but if that value is in the range of wint_t, you can pass it to iswalnum.
For values 0 to UCHAR_MAX, it would be surprising if isalnum and iswalnum produced different results.
Therefore, for instance, isspace(0xA0) might usefully report true if we are in a Unicode locale, otherwise not.
The 0x80-0xFF values are also used in 8 bit extensions over ASCII, like ISO-8859 1 and ISO 8859-15 character sets. E.g. 0xE0 is à in ISO-8859 1 (which is, of course, the same as the Unicode U+00E0 but logically distinct).
A totally different 8 bit extension is KOI-8.
The point is valid that if you don't support any "weird" extensions to ASCII (just ISO Latin) or non-ASCII 8 bit, then there isn't much of a need for run-time table indirection. The cases that may arise can be handled ad hoc. Along the lines of "if we are in an ASCII locale, then report false above 7F, otherwise go through the combined Latin/Unicode combined table".
Musl does have a lot less legacy to contend with, and musl is often much slower than glibc, so your point stands, of course.
Is there some other setup I'd need to do to see it work in glibc?
here
#include <ctype.h>
#include <locale.h>
#include <stdio.h>
int main(int argc, char** argv)
{
setlocale(LC_CTYPE, "fr_FR.iso88591");
if(isalpha('ç'))
printf("ok\n");
}
prints ok (with the file in the correct encoding)For example, on my system isalpha(0xe7) is true if I first call setlocale(LC_ALL, "en_US.iso88591").
isalnum works fine of both, it only veers off when you get into UB which is UB.
If you define “works fine” as “gives correct answers even in ub” then musl’s is completely broken since it only gives correct answers for english in ascii.
It can't give correct answers for any non-Latin scripts in any locales.
The problem is ctype and POSIX.
Given that, making ctype only work for ASCII (and maybe EBCDIC if you're really unlucky, which glibc is) is basically sufficient.
Of course, but then why complain that glibc doesn't work outsid of ascii is the point.
That is a misunderstanding on your part. Calling isalnum with out-of-range values is UB. Per-spec: "If the argument has any other value, the behavior is undefined.".
> It is entirely permissible to return 0 for values out of range. Or -1. Or anything else.
Of course it is, it's UB, there's nothing it can't do.
> POSIX is not part of the C language, if a function's behaviour is not defined for a given input, that's not UB
So a behaviour which is not defined is not an undefined behaviour. Sure. Whatever.
> that's a license for the implementation to do whatever is natural. Which can be crashing embarrassingly, but why would you do that.
Why wouldn't you? A table-based implementation is flexible, convenient and efficient, and you don't care what happens outside of the function's bounds.
Yikes. If you have wide characters, you want iswalnum, or else preprocessing: (ch <= UCHAR_MAX) ? iswhatever(ch) : 0, assuming positive ch.
since when does glibc run on bsd
https://sourceware.org/git/?p=glibc.git;a=blob;f=README;h=b9...