towards a faster isdigit()

Stephen Clamage steve at taumet.com
Fri May 10 01:00:48 AEST 1991


wicklund at intellistor.com (Tom Wicklund) writes:

>In <1991May8.030515.7004 at twinsun.com> eggert at twinsun.com (Paul Eggert) writes:
>>Why not use the following implementation instead?
>>	#define isdigit(c) ((unsigned)((c)-'0') < 10)

>Two portability problems --

>1)  Numeric digits aren't adjacent character codes in all character
>sets ...

ANSI C requires that '0' thru '9' be contiguous and in that order.

>2) The (unsigned) cast may not work 

ANSI C requires that the unsigned cast work as well-defined modulo
arithmetic, so that any negative value cast to unsigned looks like a
large positive value.

>Note the first "traditional" implementation isn't completely portable
>either since it assumes that EOF is -1, not necessarily true.

EOF and the <ctype.h> functions are supplied as part of the C library.
They must all be consistent.  Any library for which EOF is not -1 would
not use _ctype[(c)+1] to index an array.  We are talking about the
library implementation, not arbitrary user code.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list