3 ms·
The fix if one wants to stick to the standard library is to make use of locale_t posixctypelocale = newlocale(LC_CTYPE_MASK, "POSIX", NULL); saved somewhe
by JdeBP 3y ago
The fix if one wants to stick to the standard library is to make use of
locale_t posixctypelocale = newlocale(LC_CTYPE_MASK, "POSIX", NULL);
saved somewhere early on and then
b = isspace_l(c, posixctypelocale);
and
b = iswspace_l(wc, posixctypelocale);
whenever one needs them.
The irony is that systems based upon the BSD C library like MacOS and FreeBSD will have this.
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/newlocale.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/n...
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/isspace.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/i...
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/iswspace.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/i...
* https://man.freebsd.org/cgi/man.cgi?query=xlocale&sektion=3 https://man.freebsd.org/cgi/man.cgi?query=xlocale&sektion=3
- account42 3y agoThat's quite an absurdly inefficient way to check if a byte is one of 6 fixed values.
- JdeBP 3y agoIt's a simple array index and bitmask in most implementations, which is usually more efficient than what 6 short-circuited comparisons compiles into.
- account42 3y agoPlus a function call (likely with GOT indirection) plus another pointer dereference to get from the locale handle to the array. Also you now need some initialization code to create the locale and store the handle somewhere (unless you want to make this even more ridiculously inefficient and create it each time). Also, five of those whitespace characters have contiguous byte values so you don't even need 6 comparisons. The compiler doesn't have to keep the short circuting here.