_tolower and _toupper macros

Arnold Robbins arnold at audiofax.com
Tue Jul 31 00:27:45 AEST 1990


>In article <246 at audfax.audiofax.com> arnold at audiofax.com (Arnold Robbins) writes:
>>I guess tolower() and toupper() remain real functions in V.3.2 in case
>>anyone takes their address; I can't see any other reason to not have them
>>be macros identical to their _to* counterparts.

In article <1990Jul28.193255.16540 at laguna.ccsf.caltech.edu> bruce at seismo.gps.caltech.edu (Bruce Worden) writes:
>I don't believe that this is the reason for implementing to*() as functions.
>The most important reason is so that these functions can work on 
>non-US-ascii character sets (i.e. they will continue to function correctly 
>after a call to setlocale() which changes the LC_CTYPE locale.)  
>Another important reason is to avoid multiple evaluations of the argument 
>as has been discussed elsewhere.

On the surface this makes sense, but it's still possible to write a macro
that will work when setlocale changes the locale and only evaluates its
argument once.  Like so:

In ctype.h:
	extern char *_casemap;
	#define tolower(c)	(_casemap[c])
	#define toupper(c)	(_casemap[c])

In setlocale.c:

	static char casemap_french[256] = { .... };
	static char casemap_spanish[256] = { .... };
	static char casemap_c_locale[256] = { .... };
	....
	char *_casemap = casemap_c_locale;

	setlocale(int locale)	/* or whatever arg it takes, i don't know */
	{
		if (locale == france)
			_casemap = casemap_french;
		else if (locale == spain)
			_casemap = casemap_spanish;
		else
			.....
	}

Simple enough, no?  (Yes, I know setlocale has to do lots of other
stuff.  This is an example for the sake of discussion, ok?)
-- 
Arnold Robbins				AudioFAX, Inc. | Laundry increases
2000 Powers Ferry Road, #220 / Marietta, GA. 30067     | exponentially in the
INTERNET: arnold at audiofax.com	Phone: +1 404 933 7600 | number of children.
UUCP:	  emory!audfax!arnold	Fax:   +1 404 933 7606 |   -- Miriam Robbins



More information about the Comp.lang.c mailing list