19 |
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 |
Boston, MA 02111-1307, USA. */ |
Boston, MA 02111-1307, USA. */ |
21 |
|
|
22 |
/* This is a compatible replacement of the standard C library's <ctype.h> |
/* |
|
with the following properties: |
|
23 |
|
|
24 |
- Implements all isxxx() macros required by C99. |
@defvr Extension HOST_CHARSET |
25 |
- Also implements some character classes useful when |
This macro indicates the basic character set and encoding used by the |
26 |
parsing C-like languages. |
host: more precisely, the encoding used for character constants in |
27 |
- Does not change behavior depending on the current locale. |
preprocessor @samp{#if} statements (the C "execution character set"). |
28 |
- Behaves properly for all values in the range of a signed or |
It is defined by @file{safe-ctype.h}, and will be an integer constant |
29 |
unsigned char. */ |
with one of the following values: |
30 |
|
|
31 |
|
@ftable @code |
32 |
|
@item HOST_CHARSET_UNKNOWN |
33 |
|
The host character set is unknown - that is, not one of the next two |
34 |
|
possibilities. |
35 |
|
|
36 |
|
@item HOST_CHARSET_ASCII |
37 |
|
The host character set is ASCII. |
38 |
|
|
39 |
|
@item HOST_CHARSET_EBCDIC |
40 |
|
The host character set is some variant of EBCDIC. (Only one of the |
41 |
|
nineteen EBCDIC varying characters is tested; exercise caution.) |
42 |
|
@end ftable |
43 |
|
@end defvr |
44 |
|
|
45 |
|
@deffn Extension ISALPHA (@var{c}) |
46 |
|
@deffnx Extension ISALNUM (@var{c}) |
47 |
|
@deffnx Extension ISBLANK (@var{c}) |
48 |
|
@deffnx Extension ISCNTRL (@var{c}) |
49 |
|
@deffnx Extension ISDIGIT (@var{c}) |
50 |
|
@deffnx Extension ISGRAPH (@var{c}) |
51 |
|
@deffnx Extension ISLOWER (@var{c}) |
52 |
|
@deffnx Extension ISPRINT (@var{c}) |
53 |
|
@deffnx Extension ISPUNCT (@var{c}) |
54 |
|
@deffnx Extension ISSPACE (@var{c}) |
55 |
|
@deffnx Extension ISUPPER (@var{c}) |
56 |
|
@deffnx Extension ISXDIGIT (@var{c}) |
57 |
|
|
58 |
|
These twelve macros are defined by @file{safe-ctype.h}. Each has the |
59 |
|
same meaning as the corresponding macro (with name in lowercase) |
60 |
|
defined by the standard header @file{ctype.h}. For example, |
61 |
|
@code{ISALPHA} returns true for alphabetic characters and false for |
62 |
|
others. However, there are two differences between these macros and |
63 |
|
those provided by @file{ctype.h}: |
64 |
|
|
65 |
|
@itemize @bullet |
66 |
|
@item These macros are guaranteed to have well-defined behavior for all |
67 |
|
values representable by @code{signed char} and @code{unsigned char}, and |
68 |
|
for @code{EOF}. |
69 |
|
|
70 |
|
@item These macros ignore the current locale; they are true for these |
71 |
|
fixed sets of characters: |
72 |
|
@multitable {@code{XDIGIT}} {yada yada yada yada yada yada yada yada} |
73 |
|
@item @code{ALPHA} @tab @kbd{A-Za-z} |
74 |
|
@item @code{ALNUM} @tab @kbd{A-Za-z0-9} |
75 |
|
@item @code{BLANK} @tab @kbd{space tab} |
76 |
|
@item @code{CNTRL} @tab @code{!PRINT} |
77 |
|
@item @code{DIGIT} @tab @kbd{0-9} |
78 |
|
@item @code{GRAPH} @tab @code{ALNUM || PUNCT} |
79 |
|
@item @code{LOWER} @tab @kbd{a-z} |
80 |
|
@item @code{PRINT} @tab @code{GRAPH ||} @kbd{space} |
81 |
|
@item @code{PUNCT} @tab @kbd{`~!@@#$%^&*()_-=+[@{]@}\|;:'",<.>/?} |
82 |
|
@item @code{SPACE} @tab @kbd{space tab \n \r \f \v} |
83 |
|
@item @code{UPPER} @tab @kbd{A-Z} |
84 |
|
@item @code{XDIGIT} @tab @kbd{0-9A-Fa-f} |
85 |
|
@end multitable |
86 |
|
|
87 |
|
Note that, if the host character set is ASCII or a superset thereof, |
88 |
|
all these macros will return false for all values of @code{char} outside |
89 |
|
the range of 7-bit ASCII. In particular, both ISPRINT and ISCNTRL return |
90 |
|
false for characters with numeric values from 128 to 255. |
91 |
|
@end itemize |
92 |
|
@end deffn |
93 |
|
|
94 |
|
@deffn Extension ISIDNUM (@var{c}) |
95 |
|
@deffnx Extension ISIDST (@var{c}) |
96 |
|
@deffnx Extension IS_VSPACE (@var{c}) |
97 |
|
@deffnx Extension IS_NVSPACE (@var{c}) |
98 |
|
@deffnx Extension IS_SPACE_OR_NUL (@var{c}) |
99 |
|
@deffnx Extension IS_ISOBASIC (@var{c}) |
100 |
|
These six macros are defined by @file{safe-ctype.h} and provide |
101 |
|
additional character classes which are useful when doing lexical |
102 |
|
analysis of C or similar languages. They are true for the following |
103 |
|
sets of characters: |
104 |
|
|
105 |
|
@multitable {@code{SPACE_OR_NUL}} {yada yada yada yada yada yada yada yada} |
106 |
|
@item @code{IDNUM} @tab @kbd{A-Za-z0-9_} |
107 |
|
@item @code{IDST} @tab @kbd{A-Za-z_} |
108 |
|
@item @code{VSPACE} @tab @kbd{\r \n} |
109 |
|
@item @code{NVSPACE} @tab @kbd{space tab \f \v \0} |
110 |
|
@item @code{SPACE_OR_NUL} @tab @code{VSPACE || NVSPACE} |
111 |
|
@item @code{ISOBASIC} @tab @code{VSPACE || NVSPACE || PRINT} |
112 |
|
@end multitable |
113 |
|
@end deffn |
114 |
|
|
115 |
|
*/ |
116 |
|
|
117 |
#include "ansidecl.h" |
#include "ansidecl.h" |
118 |
#include <safe-ctype.h> |
#include <safe-ctype.h> |
119 |
#include <stdio.h> /* for EOF */ |
#include <stdio.h> /* for EOF */ |
120 |
|
|
121 |
|
#if EOF != -1 |
122 |
|
#error "<safe-ctype.h> requires EOF == -1" |
123 |
|
#endif |
124 |
|
|
125 |
/* Shorthand */ |
/* Shorthand */ |
126 |
#define bl _sch_isblank |
#define bl _sch_isblank |
127 |
#define cn _sch_iscntrl |
#define cn _sch_iscntrl |
137 |
#define xd _sch_isxdigit |
#define xd _sch_isxdigit |
138 |
|
|
139 |
/* Masks. */ |
/* Masks. */ |
140 |
#define L lo|is |pr /* lower case letter */ |
#define L (const unsigned short) (lo|is |pr) /* lower case letter */ |
141 |
#define XL lo|is|xd|pr /* lowercase hex digit */ |
#define XL (const unsigned short) (lo|is|xd|pr) /* lowercase hex digit */ |
142 |
#define U up|is |pr /* upper case letter */ |
#define U (const unsigned short) (up|is |pr) /* upper case letter */ |
143 |
#define XU up|is|xd|pr /* uppercase hex digit */ |
#define XU (const unsigned short) (up|is|xd|pr) /* uppercase hex digit */ |
144 |
#define D di |xd|pr /* decimal digit */ |
#define D (const unsigned short) (di |xd|pr) /* decimal digit */ |
145 |
#define P pn |pr /* punctuation */ |
#define P (const unsigned short) (pn |pr) /* punctuation */ |
146 |
#define _ pn|is |pr /* underscore */ |
#define _ (const unsigned short) (pn|is |pr) /* underscore */ |
147 |
|
|
148 |
#define C cn /* control character */ |
#define C (const unsigned short) ( cn) /* control character */ |
149 |
#define Z nv |cn /* NUL */ |
#define Z (const unsigned short) (nv |cn) /* NUL */ |
150 |
#define M nv|sp |cn /* cursor movement: \f \v */ |
#define M (const unsigned short) (nv|sp |cn) /* cursor movement: \f \v */ |
151 |
#define V vs|sp |cn /* vertical space: \r \n */ |
#define V (const unsigned short) (vs|sp |cn) /* vertical space: \r \n */ |
152 |
#define T nv|sp|bl|cn /* tab */ |
#define T (const unsigned short) (nv|sp|bl|cn) /* tab */ |
153 |
#define S nv|sp|bl|pr /* space */ |
#define S (const unsigned short) (nv|sp|bl|pr) /* space */ |
154 |
|
|
155 |
/* Are we ASCII? */ |
/* Are we ASCII? */ |
156 |
#if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \ |
#if HOST_CHARSET == HOST_CHARSET_ASCII |
|
&& 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \ |
|
|
&& EOF == -1 |
|
157 |
|
|
158 |
const unsigned short _sch_istable[256] = |
const unsigned short _sch_istable[256] = |
159 |
{ |
{ |
246 |
}; |
}; |
247 |
|
|
248 |
#else |
#else |
249 |
#error "Unsupported host character set" |
# if HOST_CHARSET == HOST_CHARSET_EBCDIC |
250 |
#endif /* not ASCII */ |
#error "FIXME: write tables for EBCDIC" |
251 |
|
# else |
252 |
|
#error "Unrecognized host character set" |
253 |
|
# endif |
254 |
|
#endif |