Is there a good example of how toupper() works?

Mark Brader msb at sq.sq.com
Mon Nov 12 15:09:33 AEST 1990


Karl-P. Huestegge (karl at robot.in-berlin.de) writes:
> One additional advice: Please don't use isascii() in text-functions,
> because this forbids all international chars > 127. Use isprint()
> instead (or whatever is appropriate).
> Please keep your code 8-bit clean. Thousands of Users thank you.

The trouble with this advice is that isprint() is not a replacement
for isascii().  All of the "ctype functions" other than isascii()
are restricted in the arguments they can take, so as to permit the
simple implementation by table lookup.  In an ASCII environment,
isascii() serves as a validator, to see whether the argument value
is permissible to pass to other "ctype functions".  isprint() is
merely another "ctype function" with the same domain of validity
for its argument as the rest.

Now, isascii() itself is not in ANSI C.  (More precisely, implementations
are allowed but not required to provide it, along with any other is...()
functions not mentioned explicitly in the standard.)  As a replacement
for it in its role as a validator, my usual suggestion is:

	#include <ctype.h>

	#ifdef __STDC__
	#  include <limits.h>
	#  define IS_CTYPABLE(c) (((c) < UCHAR_MAX && (c) >= 0) || (c) == EOF)
	#else
	#  define IS_CTYPABLE isascii
	#endif

We would then see things like

	if (IS_CTYPABLE (*p) && islower (*p)) *p = toupper (*p);

But this does not allow for non-ANSI, non-ASCII environments where the
"ctype functions" accept a greater range of argument values than
isascii() returns true on.  I'm not aware of any way to make an
automated test for those environments, which could conveniently be
added to the #ifdef above.  Perhaps Karl can suggest a way.

Caveat: this article was prepared without reference to the final standard.
Please email me if you detect errors, and I'll post a correction.
(This is, incidentally, *almost always* the way that errors on Usenet
are best handled: give the poster a chance to announce their own error
first.  For these purposes, not reading the FAQ list counts as an error.)
-- 
Mark Brader, SoftQuad Inc., Toronto	"... pure English is de rigueur"
utzoo!sq!msb, msb at sq.com			-- Manchester Guardian Weekly

This article is in the public domain.



More information about the Comp.lang.c mailing list