safe-ctype.h revision 130561
10Sstevel@tonic-gate/* <ctype.h> replacement macros.
20Sstevel@tonic-gate
30Sstevel@tonic-gate   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
40Sstevel@tonic-gate   Contributed by Zack Weinberg <zackw@stanford.edu>.
56812Sraf
66812SrafThis file is part of the libiberty library.
70Sstevel@tonic-gateLibiberty is free software; you can redistribute it and/or
80Sstevel@tonic-gatemodify it under the terms of the GNU Library General Public
90Sstevel@tonic-gateLicense as published by the Free Software Foundation; either
100Sstevel@tonic-gateversion 2 of the License, or (at your option) any later version.
110Sstevel@tonic-gate
120Sstevel@tonic-gateLibiberty is distributed in the hope that it will be useful,
130Sstevel@tonic-gatebut WITHOUT ANY WARRANTY; without even the implied warranty of
140Sstevel@tonic-gateMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
150Sstevel@tonic-gateLibrary General Public License for more details.
160Sstevel@tonic-gate
170Sstevel@tonic-gateYou should have received a copy of the GNU Library General Public
180Sstevel@tonic-gateLicense along with libiberty; see the file COPYING.LIB.  If
190Sstevel@tonic-gatenot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
200Sstevel@tonic-gateBoston, MA 02111-1307, USA.  */
216812Sraf
226812Sraf/* This is a compatible replacement of the standard C library's <ctype.h>
236812Sraf   with the following properties:
240Sstevel@tonic-gate
250Sstevel@tonic-gate   - Implements all isxxx() macros required by C99.
260Sstevel@tonic-gate   - Also implements some character classes useful when
270Sstevel@tonic-gate     parsing C-like languages.
280Sstevel@tonic-gate   - Does not change behavior depending on the current locale.
290Sstevel@tonic-gate   - Behaves properly for all values in the range of a signed or
300Sstevel@tonic-gate     unsigned char.
310Sstevel@tonic-gate
320Sstevel@tonic-gate   To avoid conflicts, this header defines the isxxx functions in upper
330Sstevel@tonic-gate   case, e.g. ISALPHA not isalpha.  */
340Sstevel@tonic-gate
350Sstevel@tonic-gate#ifndef SAFE_CTYPE_H
360Sstevel@tonic-gate#define SAFE_CTYPE_H
370Sstevel@tonic-gate
380Sstevel@tonic-gate#ifdef isalpha
390Sstevel@tonic-gate #error "safe-ctype.h and ctype.h may not be used simultaneously"
400Sstevel@tonic-gate#endif
410Sstevel@tonic-gate
420Sstevel@tonic-gate/* Determine host character set.  */
430Sstevel@tonic-gate#define HOST_CHARSET_UNKNOWN 0
440Sstevel@tonic-gate#define HOST_CHARSET_ASCII   1
450Sstevel@tonic-gate#define HOST_CHARSET_EBCDIC  2
460Sstevel@tonic-gate
470Sstevel@tonic-gate#if  '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
480Sstevel@tonic-gate   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
490Sstevel@tonic-gate#  define HOST_CHARSET HOST_CHARSET_ASCII
500Sstevel@tonic-gate#else
510Sstevel@tonic-gate# if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
520Sstevel@tonic-gate   && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
530Sstevel@tonic-gate#  define HOST_CHARSET HOST_CHARSET_EBCDIC
540Sstevel@tonic-gate# else
550Sstevel@tonic-gate#  define HOST_CHARSET HOST_CHARSET_UNKNOWN
560Sstevel@tonic-gate# endif
570Sstevel@tonic-gate#endif
580Sstevel@tonic-gate
590Sstevel@tonic-gate/* Categories.  */
606812Sraf
616812Srafenum {
620Sstevel@tonic-gate  /* In C99 */
630Sstevel@tonic-gate  _sch_isblank  = 0x0001,	/* space \t */
640Sstevel@tonic-gate  _sch_iscntrl  = 0x0002,	/* nonprinting characters */
650Sstevel@tonic-gate  _sch_isdigit  = 0x0004,	/* 0-9 */
660Sstevel@tonic-gate  _sch_islower  = 0x0008,	/* a-z */
670Sstevel@tonic-gate  _sch_isprint  = 0x0010,	/* any printing character including ' ' */
680Sstevel@tonic-gate  _sch_ispunct  = 0x0020,	/* all punctuation */
690Sstevel@tonic-gate  _sch_isspace  = 0x0040,	/* space \t \n \r \f \v */
706812Sraf  _sch_isupper  = 0x0080,	/* A-Z */
710Sstevel@tonic-gate  _sch_isxdigit = 0x0100,	/* 0-9A-Fa-f */
720Sstevel@tonic-gate
730Sstevel@tonic-gate  /* Extra categories useful to cpplib.  */
740Sstevel@tonic-gate  _sch_isidst	= 0x0200,	/* A-Za-z_ */
750Sstevel@tonic-gate  _sch_isvsp    = 0x0400,	/* \n \r */
760Sstevel@tonic-gate  _sch_isnvsp   = 0x0800,	/* space \t \f \v \0 */
770Sstevel@tonic-gate
780Sstevel@tonic-gate  /* Combinations of the above.  */
790Sstevel@tonic-gate  _sch_isalpha  = _sch_isupper|_sch_islower,	/* A-Za-z */
800Sstevel@tonic-gate  _sch_isalnum  = _sch_isalpha|_sch_isdigit,	/* A-Za-z0-9 */
810Sstevel@tonic-gate  _sch_isidnum  = _sch_isidst|_sch_isdigit,	/* A-Za-z0-9_ */
820Sstevel@tonic-gate  _sch_isgraph  = _sch_isalnum|_sch_ispunct,	/* isprint and not space */
830Sstevel@tonic-gate  _sch_iscppsp  = _sch_isvsp|_sch_isnvsp,	/* isspace + \0 */
840Sstevel@tonic-gate  _sch_isbasic  = _sch_isprint|_sch_iscppsp     /* basic charset of ISO C
850Sstevel@tonic-gate						   (plus ` and @)  */
860Sstevel@tonic-gate};
870Sstevel@tonic-gate
880Sstevel@tonic-gate/* Character classification.  */
890Sstevel@tonic-gateextern const unsigned short _sch_istable[256];
900Sstevel@tonic-gate
910Sstevel@tonic-gate#define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (unsigned short)(bit))
920Sstevel@tonic-gate
930Sstevel@tonic-gate#define ISALPHA(c)  _sch_test(c, _sch_isalpha)
940Sstevel@tonic-gate#define ISALNUM(c)  _sch_test(c, _sch_isalnum)
950Sstevel@tonic-gate#define ISBLANK(c)  _sch_test(c, _sch_isblank)
960Sstevel@tonic-gate#define ISCNTRL(c)  _sch_test(c, _sch_iscntrl)
970Sstevel@tonic-gate#define ISDIGIT(c)  _sch_test(c, _sch_isdigit)
980Sstevel@tonic-gate#define ISGRAPH(c)  _sch_test(c, _sch_isgraph)
990Sstevel@tonic-gate#define ISLOWER(c)  _sch_test(c, _sch_islower)
1000Sstevel@tonic-gate#define ISPRINT(c)  _sch_test(c, _sch_isprint)
1010Sstevel@tonic-gate#define ISPUNCT(c)  _sch_test(c, _sch_ispunct)
1020Sstevel@tonic-gate#define ISSPACE(c)  _sch_test(c, _sch_isspace)
1030Sstevel@tonic-gate#define ISUPPER(c)  _sch_test(c, _sch_isupper)
1040Sstevel@tonic-gate#define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate#define ISIDNUM(c)	_sch_test(c, _sch_isidnum)
1070Sstevel@tonic-gate#define ISIDST(c)	_sch_test(c, _sch_isidst)
1080Sstevel@tonic-gate#define IS_ISOBASIC(c)	_sch_test(c, _sch_isbasic)
1090Sstevel@tonic-gate#define IS_VSPACE(c)	_sch_test(c, _sch_isvsp)
1100Sstevel@tonic-gate#define IS_NVSPACE(c)	_sch_test(c, _sch_isnvsp)
1110Sstevel@tonic-gate#define IS_SPACE_OR_NUL(c)	_sch_test(c, _sch_iscppsp)
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate/* Character transformation.  */
1140Sstevel@tonic-gateextern const unsigned char  _sch_toupper[256];
1150Sstevel@tonic-gateextern const unsigned char  _sch_tolower[256];
1160Sstevel@tonic-gate#define TOUPPER(c) _sch_toupper[(c) & 0xff]
1170Sstevel@tonic-gate#define TOLOWER(c) _sch_tolower[(c) & 0xff]
1180Sstevel@tonic-gate
119#endif /* SAFE_CTYPE_H */
120