Newsgroups: comp.lang.c
Path: utzoo!sq!msb
From: msb@sq.sq.com (Mark Brader)
Subject: Re: Is there a good example of how toupper() works?
Message-ID: <1990Oct21.060952.191@sq.sq.com>
Summary: Don't forget to make the argument in range for islower, too
Organization: SoftQuad Inc., Toronto, Canada
References: <2466@ux.acs.umn.edu> <15857@csli.Stanford.EDU> <859@agcsun.UUCP>
Date: Sun, 21 Oct 90 06:09:52 GMT
Lines: 32


Not yet pointed out in all this discussion is that just because
you retrieve a value through a pointer of type char *, it isn't
necessarily a permissible argument of EITHER islower() or toupper().

In early implementations, an argument of islower() or toupper()
has to be in the range 0 to 127, which isascii() checks.  In ANSI
implementations, isascii() is allowed to not exist, but the
argument of islower() or toupper() can validly go as high as
MAX_UCHAR, so you only need to ensure the char is nonnegative.

This, then, should be a solution:

	#ifdef __STDC__			/* ANSI C */
	# if (MAX_CHAR < MAX_UCHAR)	/* chars are signed */
	#   define TOUPP(c) ((c) < 0? (c): toupper((c)))
	# else
	#   define TOUPP(c) toupper((c))
	# endif
	#else
	# define TOUPP(c) ((isascii((c)) && islower((c))? toupper((c)): ((c)))
	#endif

	for (p = duh; *p != '\0'; ++p)
		*p = TOUPP(*p);

-- 
Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
#define	MSB(type)	(~(((unsigned type)-1)>>1))

This article is in the public domain.
