[comp.lang.c] Case ranges

karl@haddock.ISC.COM (Karl Heuer) (01/01/70)

In article <6289@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <10265@orchid.waterloo.edu> rbutterworth@orchid.waterloo.edu (Ray Butterworth) writes:
>>    case 'a' :: 'z' :
>
>There's another serious problem underlying all these suggestions;
>namely, that "a through z" has a well-defined meaning.

Let's separate the two issues.  As far as C's switch statement is concerned,
the proposed notation *does* have a well-defined meaning, namely the set of
all integers inclusively between the values for 'a' and 'z'.  This is the same
set matched by "if (c >= 'a' && c <= 'z')".  This is as it should be.

If, however, the user intended to match the set of all lowercase letters, then
neither notation gives the correct answer outside the English/ASCII locale.
Thus one should write "if (islower(c))", or in the proposed switch extension,
"case ISLOWER:", where "ISLOWER" is defined in some header file.

If the set to be matched is not determined at compile-time, then one should
definitely use the "if" statement.  I do not believe "switch" should be
enhanced to match non-constant values.

Now, if the only use for case ranges were to implement character typing, I'd
say forget it, use the ctype functions.  There are other uses; unfortunately,
most of them are too application-specific to make good examples.  Thus, each
time this proposal comes up, it uses something like "'a'::'z'" to illustrate
the concept, which immediately gets shot down as ASCII- or English-chauvinism.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint