wctype: Avoid #define replacements in C++ mode.
[gnulib.git] / lib / wctype.in.h
1 /* A substitute for ISO C99 <wctype.h>, for platforms that lack it.
2
3    Copyright (C) 2006-2010 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
31 #if __GNUC__ >= 3
32 @PRAGMA_SYSTEM_HEADER@
33 #endif
34
35 #if @HAVE_WINT_T@
36 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.
37    Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
38    <wchar.h>.
39    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
40    included before <wchar.h>.  */
41 # include <stddef.h>
42 # include <stdio.h>
43 # include <time.h>
44 # include <wchar.h>
45 #endif
46
47 /* Include the original <wctype.h> if it exists.
48    BeOS 5 has the functions but no <wctype.h>.  */
49 /* The include_next requires a split double-inclusion guard.  */
50 #if @HAVE_WCTYPE_H@
51 # @INCLUDE_NEXT@ @NEXT_WCTYPE_H@
52 #endif
53
54 #ifndef _GL_WCTYPE_H
55 #define _GL_WCTYPE_H
56
57 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
58
59 /* The definition of _GL_WARN_ON_USE is copied here.  */
60
61 /* Define wint_t.  (Also done in wchar.in.h.)  */
62 #if !@HAVE_WINT_T@ && !defined wint_t
63 # define wint_t int
64 # ifndef WEOF
65 #  define WEOF -1
66 # endif
67 #endif
68
69
70 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
71    Linux libc5 has <wctype.h> and the functions but they are broken.
72    Assume all 12 functions are implemented the same way, or not at all.  */
73 #if ! @HAVE_ISWCNTRL@ || @REPLACE_ISWCNTRL@
74
75 /* IRIX 5.3 has macros but no functions, its isw* macros refer to an
76    undefined variable _ctmp_ and to <ctype.h> macros like _P, and they
77    refer to system functions like _iswctype that are not in the
78    standard C library.  Rather than try to get ancient buggy
79    implementations like this to work, just disable them.  */
80 # undef iswalnum
81 # undef iswalpha
82 # undef iswblank
83 # undef iswcntrl
84 # undef iswdigit
85 # undef iswgraph
86 # undef iswlower
87 # undef iswprint
88 # undef iswpunct
89 # undef iswspace
90 # undef iswupper
91 # undef iswxdigit
92 # undef towlower
93 # undef towupper
94
95 /* Linux libc5 has <wctype.h> and the functions but they are broken.  */
96 # if @REPLACE_ISWCNTRL@
97 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
98 #   define iswalnum rpl_iswalnum
99 #   define iswalpha rpl_iswalpha
100 #   define iswblank rpl_iswblank
101 #   define iswcntrl rpl_iswcntrl
102 #   define iswdigit rpl_iswdigit
103 #   define iswgraph rpl_iswgraph
104 #   define iswlower rpl_iswlower
105 #   define iswprint rpl_iswprint
106 #   define iswpunct rpl_iswpunct
107 #   define iswspace rpl_iswspace
108 #   define iswupper rpl_iswupper
109 #   define iswxdigit rpl_iswxdigit
110 #   define towlower rpl_towlower
111 #   define towupper rpl_towupper
112 #  endif
113 # endif
114
115 static inline int
116 # if @REPLACE_ISWCNTRL@
117 rpl_iswalnum
118 # else
119 iswalnum
120 # endif
121          (wint_t wc)
122 {
123   return ((wc >= '0' && wc <= '9')
124           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'));
125 }
126
127 static inline int
128 # if @REPLACE_ISWCNTRL@
129 rpl_iswalpha
130 # else
131 iswalpha
132 # endif
133          (wint_t wc)
134 {
135   return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z';
136 }
137
138 static inline int
139 # if @REPLACE_ISWCNTRL@
140 rpl_iswblank
141 # else
142 iswblank
143 # endif
144          (wint_t wc)
145 {
146   return wc == ' ' || wc == '\t';
147 }
148
149 static inline int
150 # if @REPLACE_ISWCNTRL@
151 rpl_iswcntrl
152 # else
153 iswcntrl
154 # endif
155         (wint_t wc)
156 {
157   return (wc & ~0x1f) == 0 || wc == 0x7f;
158 }
159
160 static inline int
161 # if @REPLACE_ISWCNTRL@
162 rpl_iswdigit
163 # else
164 iswdigit
165 # endif
166          (wint_t wc)
167 {
168   return wc >= '0' && wc <= '9';
169 }
170
171 static inline int
172 # if @REPLACE_ISWCNTRL@
173 rpl_iswgraph
174 # else
175 iswgraph
176 # endif
177          (wint_t wc)
178 {
179   return wc >= '!' && wc <= '~';
180 }
181
182 static inline int
183 # if @REPLACE_ISWCNTRL@
184 rpl_iswlower
185 # else
186 iswlower
187 # endif
188          (wint_t wc)
189 {
190   return wc >= 'a' && wc <= 'z';
191 }
192
193 static inline int
194 # if @REPLACE_ISWCNTRL@
195 rpl_iswprint
196 # else
197 iswprint
198 # endif
199          (wint_t wc)
200 {
201   return wc >= ' ' && wc <= '~';
202 }
203
204 static inline int
205 # if @REPLACE_ISWCNTRL@
206 rpl_iswpunct
207 # else
208 iswpunct
209 # endif
210          (wint_t wc)
211 {
212   return (wc >= '!' && wc <= '~'
213           && !((wc >= '0' && wc <= '9')
214                || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')));
215 }
216
217 static inline int
218 # if @REPLACE_ISWCNTRL@
219 rpl_iswspace
220 # else
221 iswspace
222 # endif
223          (wint_t wc)
224 {
225   return (wc == ' ' || wc == '\t'
226           || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r');
227 }
228
229 static inline int
230 # if @REPLACE_ISWCNTRL@
231 rpl_iswupper
232 # else
233 iswupper
234 # endif
235          (wint_t wc)
236 {
237   return wc >= 'A' && wc <= 'Z';
238 }
239
240 static inline int
241 # if @REPLACE_ISWCNTRL@
242 rpl_iswxdigit
243 # else
244 iswxdigit
245 # endif
246           (wint_t wc)
247 {
248   return ((wc >= '0' && wc <= '9')
249           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F'));
250 }
251
252 static inline wint_t
253 # if @REPLACE_ISWCNTRL@
254 rpl_towlower
255 # else
256 towlower
257 # endif
258          (wint_t wc)
259 {
260   return (wc >= 'A' && wc <= 'Z' ? wc - 'A' + 'a' : wc);
261 }
262
263 static inline wint_t
264 # if @REPLACE_ISWCNTRL@
265 rpl_towupper
266 # else
267 towupper
268 # endif
269          (wint_t wc)
270 {
271   return (wc >= 'a' && wc <= 'z' ? wc - 'a' + 'A' : wc);
272 }
273
274 #endif /* ! HAVE_ISWCNTRL || REPLACE_ISWCNTRL */
275
276 #if defined __MINGW32__
277
278 /* On native Windows, wchar_t is uint16_t, and wint_t is uint32_t.
279    The functions towlower and towupper are implemented in the MSVCRT library
280    to take a wchar_t argument and return a wchar_t result.  mingw declares
281    these functions to take a wint_t argument and return a wint_t result.
282    This means that:
283    1. When the user passes an argument outside the range 0x0000..0xFFFF, the
284       function will look only at the lower 16 bits.  This is allowed according
285       to POSIX.
286    2. The return value is returned in the lower 16 bits of the result register.
287       The upper 16 bits are random: whatever happened to be in that part of the
288       result register.  We need to fix this by adding a zero-extend from
289       wchar_t to wint_t after the call.  */
290
291 static inline wint_t
292 rpl_towlower (wint_t wc)
293 {
294   return (wint_t) (wchar_t) towlower (wc);
295 }
296 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
297 #  define towlower rpl_towlower
298 # endif
299
300 static inline wint_t
301 rpl_towupper (wint_t wc)
302 {
303   return (wint_t) (wchar_t) towupper (wc);
304 }
305 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
306 #  define towupper rpl_towupper
307 # endif
308
309 #endif /* __MINGW32__ */
310
311 #if @REPLACE_ISWCNTRL@
312 _GL_CXXALIAS_RPL (iswalnum, int, (wint_t wc));
313 _GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc));
314 _GL_CXXALIAS_RPL (iswblank, int, (wint_t wc));
315 _GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc));
316 _GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc));
317 _GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc));
318 _GL_CXXALIAS_RPL (iswlower, int, (wint_t wc));
319 _GL_CXXALIAS_RPL (iswprint, int, (wint_t wc));
320 _GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc));
321 _GL_CXXALIAS_RPL (iswspace, int, (wint_t wc));
322 _GL_CXXALIAS_RPL (iswupper, int, (wint_t wc));
323 _GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc));
324 #else
325 _GL_CXXALIAS_SYS (iswalnum, int, (wint_t wc));
326 _GL_CXXALIAS_SYS (iswalpha, int, (wint_t wc));
327 _GL_CXXALIAS_SYS (iswblank, int, (wint_t wc));
328 _GL_CXXALIAS_SYS (iswcntrl, int, (wint_t wc));
329 _GL_CXXALIAS_SYS (iswdigit, int, (wint_t wc));
330 _GL_CXXALIAS_SYS (iswgraph, int, (wint_t wc));
331 _GL_CXXALIAS_SYS (iswlower, int, (wint_t wc));
332 _GL_CXXALIAS_SYS (iswprint, int, (wint_t wc));
333 _GL_CXXALIAS_SYS (iswpunct, int, (wint_t wc));
334 _GL_CXXALIAS_SYS (iswspace, int, (wint_t wc));
335 _GL_CXXALIAS_SYS (iswupper, int, (wint_t wc));
336 _GL_CXXALIAS_SYS (iswxdigit, int, (wint_t wc));
337 #endif
338 _GL_CXXALIASWARN (iswalnum);
339 _GL_CXXALIASWARN (iswalpha);
340 _GL_CXXALIASWARN (iswblank);
341 _GL_CXXALIASWARN (iswcntrl);
342 _GL_CXXALIASWARN (iswdigit);
343 _GL_CXXALIASWARN (iswgraph);
344 _GL_CXXALIASWARN (iswlower);
345 _GL_CXXALIASWARN (iswprint);
346 _GL_CXXALIASWARN (iswpunct);
347 _GL_CXXALIASWARN (iswspace);
348 _GL_CXXALIASWARN (iswupper);
349 _GL_CXXALIASWARN (iswxdigit);
350
351 #if @REPLACE_ISWCNTRL@ || defined __MINGW32__
352 _GL_CXXALIAS_RPL (towlower, wint_t, (wint_t wc));
353 _GL_CXXALIAS_RPL (towupper, wint_t, (wint_t wc));
354 #else
355 _GL_CXXALIAS_SYS (towlower, wint_t, (wint_t wc));
356 _GL_CXXALIAS_SYS (towupper, wint_t, (wint_t wc));
357 #endif
358 _GL_CXXALIASWARN (towlower);
359 _GL_CXXALIASWARN (towupper);
360
361
362 #endif /* _GL_WCTYPE_H */
363 #endif /* _GL_WCTYPE_H */