Don't worry about using IRIX 5.3's wctype.h broken definitions;
[gnulib.git] / lib / wctype_.h
1 /* A substitute for ISO C99 <wctype.h>, for platforms that lack it.
2
3    Copyright (C) 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Bruno Haible and Paul Eggert.  */
20
21 /*
22  * ISO C 99 <wctype.h> for platforms that lack it.
23  * <http://www.opengroup.org/susv3xbd/wctype.h.html>
24  *
25  * iswctype, towctrans, towlower, towupper, wctrans, wctype,
26  * wctrans_t, and wctype_t are not yet implemented.
27  */
28
29 #ifndef _GL_WCTYPE_H
30 #define _GL_WCTYPE_H
31
32 #if @HAVE_WINT_T@ - 0
33 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.
34    Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
35    <wchar.h>.
36    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
37    <wchar.h>.  */
38 # include <stdio.h>
39 # include <time.h>
40 # include <wchar.h>
41 typedef wint_t __wctype_wint_t;
42 #else
43 typedef int __wctype_wint_t;
44 #endif
45
46 /* Include the original <wctype.h> if it exists.
47    BeOS 5 has the functions but no <wctype.h>.  */
48 #if @HAVE_WCTYPE_H@
49 # include @ABSOLUTE_WCTYPE_H@
50 #endif
51
52 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
53    Assume all 12 functions are implemented the same way, or not at all.  */
54 #if ! HAVE_ISWCNTRL
55
56 /* IRIX 5.3 has macros but no functions, its isw* macros refer to an
57    undefined variable _ctmp_ and to <ctype.h> macros like _P, and they
58    refer to system functions like _iswctype that are not in the
59    standard C library.  Rather than try to get ancient buggy
60    implementations like this to work, just disable them.  */
61 #  undef iswalnum
62 #  undef iswalpha
63 #  undef iswblank
64 #  undef iswcntrl
65 #  undef iswdigit
66 #  undef iswgraph
67 #  undef iswlower
68 #  undef iswprint
69 #  undef iswpunct
70 #  undef iswspace
71 #  undef iswupper
72 #  undef iswxdigit
73
74 static inline int
75 iswalnum (__wctype_wint_t wc)
76 {
77   return ((wc >= '0' && wc <= '9')
78           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'));
79 }
80
81 static inline int
82 iswalpha (__wctype_wint_t wc)
83 {
84   return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z';
85 }
86
87 static inline int
88 iswblank (__wctype_wint_t wc)
89 {
90   return wc == ' ' || wc == '\t';
91 }
92
93 static inline int
94 iswcntrl (__wctype_wint_t wc)
95 {
96   return (wc & ~0x1f) == 0 || wc == 0x7f;
97 }
98
99 static inline int
100 iswdigit (__wctype_wint_t wc)
101 {
102   return wc >= '0' && wc <= '9';
103 }
104
105 static inline int
106 iswgraph (__wctype_wint_t wc)
107 {
108   return wc >= '!' && wc <= '~';
109 }
110
111 static inline int
112 iswlower (__wctype_wint_t wc)
113 {
114   return wc >= 'a' && wc <= 'z';
115 }
116
117 static inline int
118 iswprint (__wctype_wint_t wc)
119 {
120   return wc >= ' ' && wc <= '~';
121 }
122
123 static inline int
124 iswpunct (__wctype_wint_t wc)
125 {
126   return (wc >= '!' && wc <= '~'
127           && !((wc >= '0' && wc <= '9')
128                || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')));
129 }
130
131 static inline int
132 iswspace (__wctype_wint_t wc)
133 {
134   return (wc == ' ' || wc == '\t'
135           || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r');
136 }
137
138 static inline int
139 iswupper (__wctype_wint_t wc)
140 {
141   return wc >= 'A' && wc <= 'Z';
142 }
143
144 static inline int
145 iswxdigit (__wctype_wint_t wc)
146 {
147   return ((wc >= '0' && wc <= '9')
148           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F'));
149 }
150
151 # endif /* ! HAVE_ISWCNTRL */
152
153 #endif /* _GL_WCTYPE_H */