|
|
Log in / Subscribe / Register

Would you like signs with those chars?

Would you like signs with those chars?

Posted Oct 25, 2022 9:14 UTC (Tue) by jengelh (subscriber, #33263)
In reply to: Would you like signs with those chars? by NYKevin
Parent article: Would you like signs with those chars?

>if you accidentally have a character with the high bit set (because you're using ISO-8859-1 or Windows-1252 or some other 8-bit ASCII superset instead of* UTF-8), then it will sign extend and you will get nonsense.

int lowertbl[] = {-1, 0, 1, ..., 0x40, 0x61, 0x62, ...};
#define tolower(c) ((lowertbl+1)[c])

Sign extension need not produce nonsense. Thanks to the equivalency of the expressions lowertbl+1[c] <=> lowertbl+1+c <=> lowertbl+c+1 <=> lowertbl[c+1], what matters is if the pointer still points to something sensible.


to post comments

Would you like signs with those chars?

Posted Oct 26, 2022 7:35 UTC (Wed) by NYKevin (subscriber, #129325) [Link]

The standard requires that all functions implemented as macros must also be implemented as functions, so that you can take their addresses. If implemented as a function, the type must be declared as int, and then c gets coerced to a negative number by sign extension before you even get to the indexing expression. You would have to pad out the table with an extra 128 entries, not an extra 1 entry.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds