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: <1990Nov12.040933.5419@sq.sq.com> Summary: 8-bit clean tricky outside ANSI C Organization: SoftQuad Inc., Toronto, Canada References: <11021@hubcap.clemson.edu> <152580@felix.UUCP> <1990Nov7.043705.15051@robot.in-berlin.de> Date: Mon, 12 Nov 90 04:09:33 GMT Lines: 51 Karl-P. Huestegge (karl@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 #ifdef __STDC__ # include # 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@sq.com -- Manchester Guardian Weekly This article is in the public domain.