Proposed Enhancement to select/case (yes, I know...)
Karl Heuer
karl at haddock.ima.isc.com
Wed Sep 5 10:27:09 AEST 1990
In article <13719 at smoke.BRL.MIL> gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
>In article <MEISSNER.90Aug31102441 at osf.osf.org> meissner at osf.org (Michael Meissner) writes:
>>... one of the problems that kept being mentioned was 'a'..'z'
>
>That's not a problem, is it? The only sane meaning would be the numbers from
>'a' through 'z', however many there may be... That fact that that particular
>code would probably not correctly achieve the programmer's intention is the
>programmer's problem, not the language's.
Well put.
There are two reasons why 'a'..'z' might not be the right answer: (a) the
alphabet isn't contiguous (e.g. EBCDIC) or (b) the locale doesn't use English.
Objection (a) is valid, but may plausibly be countered with "I don't care
about oddball machines like that". For many programs, it simply isn't worth
the effort to make them strictly conforming%. Besides which, preprocessor
conditionals may have already established the alphabet, thus making the use
portable:
#if _X_ASCII
#define LOWER 'a'..'z'
#endif
#if _X_EBCDIC /* need case lists as well as ranges */
#define LOWER 'a'..'i','j'..'r','s'..'z'
#endif
... switch (ch) ... case LOWER: ...
Objection (b) doesn't necessarily apply. If the program is processing natural
language text, then islower() is certainly better&. But I think it's more
likely (in the world of existing programs commonly found on Unix systems) that
it's processing some flavor of computer language: for example, ed(1) has
exactly 26 mark registers in any locale, doesn't it?
Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint
________
% Lots of people don't bother to cast pointers before handing them to a
prototypeless free(); this fails on word-addressible architectures, which is
probably a more likely situation than non-ASCII. One's complement is my
personal bugaboo; I have no idea how much of my code it would break.
& Even this is probably the wrong answer: islower() tests the *union* of the
English alphabet and the locale's alphabet; islower('w') tests true even in
a country where 'w' is not a letter.
More information about the Comp.std.c
mailing list