Re: E 0.9.3 release?

Bill Frantz <[email protected]>
Newsgroups gmane.comp.lang.e.general
Message-ID <r314ps-1065i-B9C2F1DBFFB04FAE8696FBB6985622CC@Bill-Frantzs-MacBook-Pro.local>
On 1/3/11 at 7:23 AM, [email protected] (Ben Laurie) wrote:

>Sorry I missed this before - following up on David's post: an int cast
>is _not_ a safe fix, despite it making the warning go away.
>
>FWIW, since you asked me in IM if C++ also suffers from this issue the
>answer is that it could easily avoid it, but in practice probably
>doesn't.
>
>How to avoid it:
>
>inline bool isprint(char c) { return isprint(static_cast<unsigned char>(c)); }
>inline bool isprint(unsigned char c) { ... }
>
>Why it is probably not avoided: in practice C++ code is likely to use
>the C implementation, which cannot be fixed in this way.

Ben brings out the language lawyer in me, and my copy of the 
ANSI C standard.

isprint is defined in the standard as:

   #include <ctype.h>
   int isprint(int c);

It also defines a bunch of constants with the note, "Their 
implementation-defined values shall be equal or greater in 
magnitude (absolute value) ot those shown with the same sign.

   CHAR_BIT 8 -- number of bits for smallest object that is not 
a bit-field
   SCHAR_MIN -127
   SCHAR_MAX +127
   UCHAR_MAX 255

There are also CHAR_MIN and CHAR_MAX with the definition of, "If 
the value of an object of type char is treated as a signed 
integer when used in an expression, the value of CHAR_MIN shall 
be the same as that of SCHAR_MIN and the value of CHAR_MAX shall 
be the same as that of SCHAR_MAX. Otherwise the value of 
CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same 
as that of UCHAR_MAX.

Using the above values, it seems that we can define a macro for isprint:

#define isprint(c) (__isprint_look_up_table[(unsigned int)(c) & 0xff)

If the compiler "knows" that c is only 8 bits (and has been 
loaded into a register with the rest of the bits zero), it can 
optimize the & operation away.

If other values are chosen (for supporting 16 bit characters for 
example), then the mask value will change or a construct such as 
could be used:

#define isprint(c) ((c) < CHAR_MIN || (c) > CHAR_MAX ? 0 : 
__iplut[(c) - CHAR_MIN])

Cheers - Bill

-----------------------------------------------------------------------
Bill Frantz        | OAuth -  It's the best that  | Periwinkle
(408)356-8506      | the wrong way of doing things| 16345 
Englewood Ave
www.pwpconsult.com | can provide. - Mike Stay     | Los Gatos, 
CA 95032
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.