safe-ctype.h revision 218822
1228753Smm/* <ctype.h> replacement macros.
2228753Smm
3228753Smm   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4228753Smm   Contributed by Zack Weinberg <zackw@stanford.edu>.
5228753Smm
6228753SmmThis file is part of the libiberty library.
7228753SmmLibiberty is free software; you can redistribute it and/or
8228753Smmmodify it under the terms of the GNU Library General Public
9228753SmmLicense as published by the Free Software Foundation; either
10228753Smmversion 2 of the License, or (at your option) any later version.
11228753Smm
12228753SmmLibiberty is distributed in the hope that it will be useful,
13228753Smmbut WITHOUT ANY WARRANTY; without even the implied warranty of
14228753SmmMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15228753SmmLibrary General Public License for more details.
16228753Smm
17228753SmmYou should have received a copy of the GNU Library General Public
18228753SmmLicense along with libiberty; see the file COPYING.LIB.  If
19228753Smmnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20228753SmmBoston, MA 02110-1301, USA.  */
21228753Smm
22228753Smm/* This is a compatible replacement of the standard C library's <ctype.h>
23228753Smm   with the following properties:
24228753Smm
25228753Smm   - Implements all isxxx() macros required by C99.
26228753Smm   - Also implements some character classes useful when
27228763Smm     parsing C-like languages.
28228753Smm   - Does not change behavior depending on the current locale.
29228753Smm   - Behaves properly for all values in the range of a signed or
30228753Smm     unsigned char.
31228753Smm
32228753Smm   To avoid conflicts, this header defines the isxxx functions in upper
33228753Smm   case, e.g. ISALPHA not isalpha.  */
34228753Smm
35228753Smm#ifndef SAFE_CTYPE_H
36228753Smm#define SAFE_CTYPE_H
37228753Smm
38228753Smm#ifdef isalpha
39228753Smm #error "safe-ctype.h and ctype.h may not be used simultaneously"
40228753Smm#endif
41228753Smm
42228753Smm/* Determine host character set.  */
43228753Smm#define HOST_CHARSET_UNKNOWN 0
44228753Smm#define HOST_CHARSET_ASCII   1
45228753Smm#define HOST_CHARSET_EBCDIC  2
46228753Smm
47228753Smm#if  '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
48228753Smm   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
49228753Smm#  define HOST_CHARSET HOST_CHARSET_ASCII
50228753Smm#else
51228753Smm# if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
52228753Smm   && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
53228753Smm#  define HOST_CHARSET HOST_CHARSET_EBCDIC
54228753Smm# else
55228753Smm#  define HOST_CHARSET HOST_CHARSET_UNKNOWN
56228753Smm# endif
57228753Smm#endif
58228753Smm
59228753Smm/* Categories.  */
60228753Smm
61232153Smmenum {
62232153Smm  /* In C99 */
63228753Smm  _sch_isblank  = 0x0001,	/* space \t */
64228753Smm  _sch_iscntrl  = 0x0002,	/* nonprinting characters */
65228753Smm  _sch_isdigit  = 0x0004,	/* 0-9 */
66228753Smm  _sch_islower  = 0x0008,	/* a-z */
67228753Smm  _sch_isprint  = 0x0010,	/* any printing character including ' ' */
68228753Smm  _sch_ispunct  = 0x0020,	/* all punctuation */
69228753Smm  _sch_isspace  = 0x0040,	/* space \t \n \r \f \v */
70228753Smm  _sch_isupper  = 0x0080,	/* A-Z */
71228753Smm  _sch_isxdigit = 0x0100,	/* 0-9A-Fa-f */
72228753Smm
73228753Smm  /* Extra categories useful to cpplib.  */
74228753Smm  _sch_isidst	= 0x0200,	/* A-Za-z_ */
75228753Smm  _sch_isvsp    = 0x0400,	/* \n \r */
76228753Smm  _sch_isnvsp   = 0x0800,	/* space \t \f \v \0 */
77228753Smm
78228753Smm  /* Combinations of the above.  */
79228753Smm  _sch_isalpha  = _sch_isupper|_sch_islower,	/* A-Za-z */
80228753Smm  _sch_isalnum  = _sch_isalpha|_sch_isdigit,	/* A-Za-z0-9 */
81228753Smm  _sch_isidnum  = _sch_isidst|_sch_isdigit,	/* A-Za-z0-9_ */
82228753Smm  _sch_isgraph  = _sch_isalnum|_sch_ispunct,	/* isprint and not space */
83228753Smm  _sch_iscppsp  = _sch_isvsp|_sch_isnvsp,	/* isspace + \0 */
84228753Smm  _sch_isbasic  = _sch_isprint|_sch_iscppsp     /* basic charset of ISO C
85228753Smm						   (plus ` and @)  */
86228753Smm};
87228753Smm
88228753Smm/* Character classification.  */
89228753Smmextern const unsigned short _sch_istable[256];
90228753Smm
91228753Smm#define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (unsigned short)(bit))
92228753Smm
93228753Smm#define ISALPHA(c)  _sch_test(c, _sch_isalpha)
94228753Smm#define ISALNUM(c)  _sch_test(c, _sch_isalnum)
95228753Smm#define ISBLANK(c)  _sch_test(c, _sch_isblank)
96232153Smm#define ISCNTRL(c)  _sch_test(c, _sch_iscntrl)
97232153Smm#define ISDIGIT(c)  _sch_test(c, _sch_isdigit)
98228753Smm#define ISGRAPH(c)  _sch_test(c, _sch_isgraph)
99228753Smm#define ISLOWER(c)  _sch_test(c, _sch_islower)
100228753Smm#define ISPRINT(c)  _sch_test(c, _sch_isprint)
101228753Smm#define ISPUNCT(c)  _sch_test(c, _sch_ispunct)
102228753Smm#define ISSPACE(c)  _sch_test(c, _sch_isspace)
103228753Smm#define ISUPPER(c)  _sch_test(c, _sch_isupper)
104228753Smm#define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
105228753Smm
106228753Smm#define ISIDNUM(c)	_sch_test(c, _sch_isidnum)
107228753Smm#define ISIDST(c)	_sch_test(c, _sch_isidst)
108228753Smm#define IS_ISOBASIC(c)	_sch_test(c, _sch_isbasic)
109228753Smm#define IS_VSPACE(c)	_sch_test(c, _sch_isvsp)
110228753Smm#define IS_NVSPACE(c)	_sch_test(c, _sch_isnvsp)
111228753Smm#define IS_SPACE_OR_NUL(c)	_sch_test(c, _sch_iscppsp)
112228753Smm
113228753Smm/* Character transformation.  */
114228753Smmextern const unsigned char  _sch_toupper[256];
115228753Smmextern const unsigned char  _sch_tolower[256];
116228753Smm#define TOUPPER(c) _sch_toupper[(c) & 0xff]
117228753Smm#define TOLOWER(c) _sch_tolower[(c) & 0xff]
118228753Smm
119228753Smm#endif /* SAFE_CTYPE_H */
120228753Smm