This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: problem with safe-ctype.h
- To: Haj dot Ten dot Brugge at net dot HCC dot nl
- Subject: Re: problem with safe-ctype.h
- From: "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu>
- Date: Fri, 15 Dec 2000 13:10:06 -0500 (EST)
- Cc: gcc-patches at gcc dot gnu dot org
> From: Herman ten Brugge <Haj.Ten.Brugge@net.HCC.nl>
>
> 2000-12-14 Herman A.J. ten Brugge <Haj.Ten.Brugge@net.HCC.nl>
>
> * safe-ctype.h: Make code work on all targets and not just on
> targets where a char is 8 bits.
>
>
> -#define _sch_test(c, bit) (_sch_istable[(int)(unsigned char)(c)] & (bit))
> +#define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (bit))
> -#define TOUPPER(c) _sch_toupper[(int)(unsigned char)(c)]
> -#define TOLOWER(c) _sch_tolower[(int)(unsigned char)(c)]
> +#define TOUPPER(c) _sch_toupper[(c) & 0xff]
> +#define TOLOWER(c) _sch_tolower[(c) & 0xff]
The reason for these casts is to avoid problems when parameter `c' is
a signed char. If you pass in something > 127 you'll get a negative
offset into the _sch_* arrays.
We used to have code to handle this in system.h before safe-ctype was
put in libiberty. Something like:
> #define CTYPE_CONV(CH) \
> (sizeof(CH) == sizeof(int) ? (int)(CH) : (int)(unsigned char)(CH))
Then you'd wrap CTYPE_CONV around the `c' parameter in the ctype
definitions.
Note: CTYPE_CONV is resolved at compile-time so you don't have
multiple evaluation problems.
So e.g. if you #define TOUPPER _sch_toupper[CTYPE_CONV(c)] it should
DTRT. You may still need to & 0xff to truncate 32-bit chars into the
right range, but CTYPE_CONV will also ensure that char 255 won't
become negative.
--Kaveh
--
Kaveh R. Ghazi Engagement Manager / Project Services
ghazi@caip.rutgers.edu Qwest Internet Solutions