Re: What's the deal with the "toupper" family?



On 2006-07-06, Peter Nilsson <airia@xxxxxxxxxxx> wrote:
Andrew Poelstra wrote:
On 2006-07-06, Frederick Gotham <fgothamNO@xxxxxxxx> wrote:
Peter Nilsson posted:
<slightly altered>
toupper( *(unsigned char const *)&c )

Does anyone else agree with this?

It looks overly complicated to me.

In normal form, I use things like...

const unsigned char *us = (const unsigned char *) s;
for (; *us; us++) *us = toupper(*us);


No matter what you think `const' means in this context, it's wrong. You
change both `us' /and/ `*us' in the second line.

If that's too complicated for some people, so be it.


Most simple-minded people believe that the const keyword will create a
constant. It's true that we find it `too complicated' to violate that.

It's safe because an unsigned char cannot have any trap
representations, but nonetheless, does it do what we want
it to do, and is it preferable over the following?

As I said, it's up to the programmer to pass the right value.
Different circumstances may well require different forms.
Where and how you source and store the character is a
factor in deciding which method you use.


The point of the cast is to work correctly, even if the programmer passes
the wrong value. Perhaps the programmer is passing input from a file
stream or something, and doesn't want to validate the string for such
a simple function. (And perhaps the string being uppercase is required
for future validations.)

toupper( (unsigned char)c );

No; the latter is much clearer and just as functional, IMHO.

But fails for potentially conforming implementations. To many people,
that's acceptable.


Under what circumstances will casting to unsigned char fail, and how
will it fail?

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
.



Relevant Pages

  • Re: wmi fails after large number of calls
    ... Or, does it connect to a given computer sometimes, but fail at ... ' | <Script Description> ... Const ForAppending = 8 ... Function PingHost ...
    (microsoft.public.scripting.vbscript)
  • OK to disguise a macro function as a function?
    ... functions to warn the programmer to be picky about what arguments they ... Currently I'm using an embedded systems compiler for programming ... because there's no stack (another strategy to save memory). ... the compiler is very particular about what it considers const. ...
    (comp.lang.c)
  • Re: BDA driver, question about get properties from node
    ... return code is 80004002 is for fail case. ... // Input pin to Output pin Topology Joints ... >> IN PKSPROPERTY pKSProperty, ...
    (microsoft.public.development.device.drivers)
  • Re: how do professional PHP developers handle defaults for function parameters?
    ... If something is going to fail, ... someone who calls a function with wrong arguments is a bad programmer. ... When you need a new feature, just extend the baseclass, and if your classes are properly decoupled, this change won't affect the rest of the program. ... Documentation should document behaviour, not syntax. ...
    (comp.lang.php)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >reasonably be expected to enfore the concept of 'const'. ... I expect it to enforce the concept of const, ... not to guarantee that a programmer provides a correct implementation. ...
    (microsoft.public.dotnet.languages.csharp)