Refresh 01-gnulib-directory.patch
[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 _@GUARD_PREFIX@_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 _@GUARD_PREFIX@_WCTYPE_H
56 #define _@GUARD_PREFIX@_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 /* Solaris 2.6 <wctype.h> includes <widec.h> which includes <euc.h> which
63    #defines a number of identifiers in the application namespace.  Revert
64    these #defines.  */
65 #ifdef __sun
66 # undef multibyte
67 # undef eucw1
68 # undef eucw2
69 # undef eucw3
70 # undef scrw1
71 # undef scrw2
72 # undef scrw3
73 #endif
74
75 /* Define wint_t and WEOF.  (Also done in wchar.in.h.)  */
76 #if !@HAVE_WINT_T@ && !defined wint_t
77 # define wint_t int
78 # ifndef WEOF
79 #  define WEOF -1
80 # endif
81 #else
82 # ifndef WEOF
83 #  define WEOF ((wint_t) -1)
84 # endif
85 #endif
86
87
88 #if !GNULIB_defined_wctype_functions
89
90 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
91    Linux libc5 has <wctype.h> and the functions but they are broken.
92    Assume all 11 functions (all isw* except iswblank) are implemented the
93    same way, or not at all.  */
94 # if ! @HAVE_ISWCNTRL@ || @REPLACE_ISWCNTRL@
95
96 /* IRIX 5.3 has macros but no functions, its isw* macros refer to an
97    undefined variable _ctmp_ and to <ctype.h> macros like _P, and they
98    refer to system functions like _iswctype that are not in the
99    standard C library.  Rather than try to get ancient buggy
100    implementations like this to work, just disable them.  */
101 #  undef iswalnum
102 #  undef iswalpha
103 #  undef iswblank
104 #  undef iswcntrl
105 #  undef iswdigit
106 #  undef iswgraph
107 #  undef iswlower
108 #  undef iswprint
109 #  undef iswpunct
110 #  undef iswspace
111 #  undef iswupper
112 #  undef iswxdigit
113 #  undef towlower
114 #  undef towupper
115
116 /* Linux libc5 has <wctype.h> and the functions but they are broken.  */
117 #  if @REPLACE_ISWCNTRL@
118 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
119 #    define iswalnum rpl_iswalnum
120 #    define iswalpha rpl_iswalpha
121 #    define iswblank rpl_iswblank
122 #    define iswcntrl rpl_iswcntrl
123 #    define iswdigit rpl_iswdigit
124 #    define iswgraph rpl_iswgraph
125 #    define iswlower rpl_iswlower
126 #    define iswprint rpl_iswprint
127 #    define iswpunct rpl_iswpunct
128 #    define iswspace rpl_iswspace
129 #    define iswupper rpl_iswupper
130 #    define iswxdigit rpl_iswxdigit
131 #   endif
132 #  endif
133 #  if @REPLACE_TOWLOWER@
134 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
135 #    define towlower rpl_towlower
136 #    define towupper rpl_towupper
137 #   endif
138 #  endif
139
140 static inline int
141 #  if @REPLACE_ISWCNTRL@
142 rpl_iswalnum
143 #  else
144 iswalnum
145 #  endif
146          (wint_t wc)
147 {
148   return ((wc >= '0' && wc <= '9')
149           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'));
150 }
151
152 static inline int
153 #  if @REPLACE_ISWCNTRL@
154 rpl_iswalpha
155 #  else
156 iswalpha
157 #  endif
158          (wint_t wc)
159 {
160   return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z';
161 }
162
163 static inline int
164 #  if @REPLACE_ISWCNTRL@
165 rpl_iswblank
166 #  else
167 iswblank
168 #  endif
169          (wint_t wc)
170 {
171   return wc == ' ' || wc == '\t';
172 }
173
174 static inline int
175 #  if @REPLACE_ISWCNTRL@
176 rpl_iswcntrl
177 #  else
178 iswcntrl
179 #  endif
180         (wint_t wc)
181 {
182   return (wc & ~0x1f) == 0 || wc == 0x7f;
183 }
184
185 static inline int
186 #  if @REPLACE_ISWCNTRL@
187 rpl_iswdigit
188 #  else
189 iswdigit
190 #  endif
191          (wint_t wc)
192 {
193   return wc >= '0' && wc <= '9';
194 }
195
196 static inline int
197 #  if @REPLACE_ISWCNTRL@
198 rpl_iswgraph
199 #  else
200 iswgraph
201 #  endif
202          (wint_t wc)
203 {
204   return wc >= '!' && wc <= '~';
205 }
206
207 static inline int
208 #  if @REPLACE_ISWCNTRL@
209 rpl_iswlower
210 #  else
211 iswlower
212 #  endif
213          (wint_t wc)
214 {
215   return wc >= 'a' && wc <= 'z';
216 }
217
218 static inline int
219 #  if @REPLACE_ISWCNTRL@
220 rpl_iswprint
221 #  else
222 iswprint
223 #  endif
224          (wint_t wc)
225 {
226   return wc >= ' ' && wc <= '~';
227 }
228
229 static inline int
230 #  if @REPLACE_ISWCNTRL@
231 rpl_iswpunct
232 #  else
233 iswpunct
234 #  endif
235          (wint_t wc)
236 {
237   return (wc >= '!' && wc <= '~'
238           && !((wc >= '0' && wc <= '9')
239                || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')));
240 }
241
242 static inline int
243 #  if @REPLACE_ISWCNTRL@
244 rpl_iswspace
245 #  else
246 iswspace
247 #  endif
248          (wint_t wc)
249 {
250   return (wc == ' ' || wc == '\t'
251           || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r');
252 }
253
254 static inline int
255 #  if @REPLACE_ISWCNTRL@
256 rpl_iswupper
257 #  else
258 iswupper
259 #  endif
260          (wint_t wc)
261 {
262   return wc >= 'A' && wc <= 'Z';
263 }
264
265 static inline int
266 #  if @REPLACE_ISWCNTRL@
267 rpl_iswxdigit
268 #  else
269 iswxdigit
270 #  endif
271           (wint_t wc)
272 {
273   return ((wc >= '0' && wc <= '9')
274           || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F'));
275 }
276
277 static inline wint_t
278 #  if @REPLACE_TOWLOWER@
279 rpl_towlower
280 #  else
281 towlower
282 #  endif
283          (wint_t wc)
284 {
285   return (wc >= 'A' && wc <= 'Z' ? wc - 'A' + 'a' : wc);
286 }
287
288 static inline wint_t
289 #  if @REPLACE_TOWLOWER@
290 rpl_towupper
291 #  else
292 towupper
293 #  endif
294          (wint_t wc)
295 {
296   return (wc >= 'a' && wc <= 'z' ? wc - 'a' + 'A' : wc);
297 }
298
299 # elif @GNULIB_ISWBLANK@ && (! @HAVE_ISWBLANK@ || @REPLACE_ISWBLANK@)
300 /* Only the iswblank function is missing.  */
301
302 #  if @REPLACE_ISWBLANK@
303 #   if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 #    define iswblank rpl_iswblank
305 #   endif
306 _GL_FUNCDECL_RPL (iswblank, int, (wint_t wc));
307 #  else
308 _GL_FUNCDECL_SYS (iswblank, int, (wint_t wc));
309 #  endif
310
311 # endif
312
313 # if defined __MINGW32__
314
315 /* On native Windows, wchar_t is uint16_t, and wint_t is uint32_t.
316    The functions towlower and towupper are implemented in the MSVCRT library
317    to take a wchar_t argument and return a wchar_t result.  mingw declares
318    these functions to take a wint_t argument and return a wint_t result.
319    This means that:
320    1. When the user passes an argument outside the range 0x0000..0xFFFF, the
321       function will look only at the lower 16 bits.  This is allowed according
322       to POSIX.
323    2. The return value is returned in the lower 16 bits of the result register.
324       The upper 16 bits are random: whatever happened to be in that part of the
325       result register.  We need to fix this by adding a zero-extend from
326       wchar_t to wint_t after the call.  */
327
328 static inline wint_t
329 rpl_towlower (wint_t wc)
330 {
331   return (wint_t) (wchar_t) towlower (wc);
332 }
333 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
334 #   define towlower rpl_towlower
335 #  endif
336
337 static inline wint_t
338 rpl_towupper (wint_t wc)
339 {
340   return (wint_t) (wchar_t) towupper (wc);
341 }
342 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
343 #   define towupper rpl_towupper
344 #  endif
345
346 # endif /* __MINGW32__ */
347
348 # define GNULIB_defined_wctype_functions 1
349 #endif
350
351 #if @REPLACE_ISWCNTRL@
352 _GL_CXXALIAS_RPL (iswalnum, int, (wint_t wc));
353 _GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc));
354 _GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc));
355 _GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc));
356 _GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc));
357 _GL_CXXALIAS_RPL (iswlower, int, (wint_t wc));
358 _GL_CXXALIAS_RPL (iswprint, int, (wint_t wc));
359 _GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc));
360 _GL_CXXALIAS_RPL (iswspace, int, (wint_t wc));
361 _GL_CXXALIAS_RPL (iswupper, int, (wint_t wc));
362 _GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc));
363 #else
364 _GL_CXXALIAS_SYS (iswalnum, int, (wint_t wc));
365 _GL_CXXALIAS_SYS (iswalpha, int, (wint_t wc));
366 _GL_CXXALIAS_SYS (iswcntrl, int, (wint_t wc));
367 _GL_CXXALIAS_SYS (iswdigit, int, (wint_t wc));
368 _GL_CXXALIAS_SYS (iswgraph, int, (wint_t wc));
369 _GL_CXXALIAS_SYS (iswlower, int, (wint_t wc));
370 _GL_CXXALIAS_SYS (iswprint, int, (wint_t wc));
371 _GL_CXXALIAS_SYS (iswpunct, int, (wint_t wc));
372 _GL_CXXALIAS_SYS (iswspace, int, (wint_t wc));
373 _GL_CXXALIAS_SYS (iswupper, int, (wint_t wc));
374 _GL_CXXALIAS_SYS (iswxdigit, int, (wint_t wc));
375 #endif
376 _GL_CXXALIASWARN (iswalnum);
377 _GL_CXXALIASWARN (iswalpha);
378 _GL_CXXALIASWARN (iswcntrl);
379 _GL_CXXALIASWARN (iswdigit);
380 _GL_CXXALIASWARN (iswgraph);
381 _GL_CXXALIASWARN (iswlower);
382 _GL_CXXALIASWARN (iswprint);
383 _GL_CXXALIASWARN (iswpunct);
384 _GL_CXXALIASWARN (iswspace);
385 _GL_CXXALIASWARN (iswupper);
386 _GL_CXXALIASWARN (iswxdigit);
387
388 #if @GNULIB_ISWBLANK@
389 # if @REPLACE_ISWCNTRL@ || @REPLACE_ISWBLANK@
390 _GL_CXXALIAS_RPL (iswblank, int, (wint_t wc));
391 # else
392 _GL_CXXALIAS_SYS (iswblank, int, (wint_t wc));
393 # endif
394 _GL_CXXALIASWARN (iswblank);
395 #endif
396
397 #if !@HAVE_WCTYPE_T@
398 # if !GNULIB_defined_wctype_t
399 typedef void * wctype_t;
400 #  define GNULIB_defined_wctype_t 1
401 # endif
402 #endif
403
404 /* Get a descriptor for a wide character property.  */
405 #if @GNULIB_WCTYPE@
406 # if !@HAVE_WCTYPE_T@
407 _GL_FUNCDECL_SYS (wctype, wctype_t, (const char *name));
408 # endif
409 _GL_CXXALIAS_SYS (wctype, wctype_t, (const char *name));
410 _GL_CXXALIASWARN (wctype);
411 #elif defined GNULIB_POSIXCHECK
412 # undef wctype
413 # if HAVE_RAW_DECL_WCTYPE
414 _GL_WARN_ON_USE (wctype, "wctype is unportable - "
415                  "use gnulib module wctype for portability");
416 # endif
417 #endif
418
419 /* Test whether a wide character has a given property.
420    The argument WC must be either a wchar_t value or WEOF.
421    The argument DESC must have been returned by the wctype() function.  */
422 #if @GNULIB_ISWCTYPE@
423 # if !@HAVE_WCTYPE_T@
424 _GL_FUNCDECL_SYS (iswctype, int, (wint_t wc, wctype_t desc));
425 # endif
426 _GL_CXXALIAS_SYS (iswctype, int, (wint_t wc, wctype_t desc));
427 _GL_CXXALIASWARN (iswctype);
428 #elif defined GNULIB_POSIXCHECK
429 # undef iswctype
430 # if HAVE_RAW_DECL_ISWCTYPE
431 _GL_WARN_ON_USE (iswctype, "iswctype is unportable - "
432                  "use gnulib module iswctype for portability");
433 # endif
434 #endif
435
436 #if @REPLACE_TOWLOWER@ || defined __MINGW32__
437 _GL_CXXALIAS_RPL (towlower, wint_t, (wint_t wc));
438 _GL_CXXALIAS_RPL (towupper, wint_t, (wint_t wc));
439 #else
440 _GL_CXXALIAS_SYS (towlower, wint_t, (wint_t wc));
441 _GL_CXXALIAS_SYS (towupper, wint_t, (wint_t wc));
442 #endif
443 _GL_CXXALIASWARN (towlower);
444 _GL_CXXALIASWARN (towupper);
445
446 #if !@HAVE_WCTRANS_T@
447 # if !GNULIB_defined_wctrans_t
448 typedef void * wctrans_t;
449 #  define GNULIB_defined_wctrans_t 1
450 # endif
451 #endif
452
453 /* Get a descriptor for a wide character case conversion.  */
454 #if @GNULIB_WCTRANS@
455 # if !@HAVE_WCTRANS_T@
456 _GL_FUNCDECL_SYS (wctrans, wctrans_t, (const char *name));
457 # endif
458 _GL_CXXALIAS_SYS (wctrans, wctrans_t, (const char *name));
459 _GL_CXXALIASWARN (wctrans);
460 #elif defined GNULIB_POSIXCHECK
461 # undef wctrans
462 # if HAVE_RAW_DECL_WCTRANS
463 _GL_WARN_ON_USE (wctrans, "wctrans is unportable - "
464                  "use gnulib module wctrans for portability");
465 # endif
466 #endif
467
468 /* Perform a given case conversion on a wide character.
469    The argument WC must be either a wchar_t value or WEOF.
470    The argument DESC must have been returned by the wctrans() function.  */
471 #if @GNULIB_TOWCTRANS@
472 # if !@HAVE_WCTRANS_T@
473 _GL_FUNCDECL_SYS (towctrans, wint_t, (wint_t wc, wctrans_t desc));
474 # endif
475 _GL_CXXALIAS_SYS (towctrans, wint_t, (wint_t wc, wctrans_t desc));
476 _GL_CXXALIASWARN (towctrans);
477 #elif defined GNULIB_POSIXCHECK
478 # undef towctrans
479 # if HAVE_RAW_DECL_TOWCTRANS
480 _GL_WARN_ON_USE (towctrans, "towctrans is unportable - "
481                  "use gnulib module towctrans for portability");
482 # endif
483 #endif
484
485
486 #endif /* _@GUARD_PREFIX@_WCTYPE_H */
487 #endif /* _@GUARD_PREFIX@_WCTYPE_H */