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