wctype.in.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* A substitute for ISO C99 <wctype.h>, for platforms that lack it.
  2. Copyright (C) 2006-2010 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Bruno Haible and Paul Eggert. */
  15. /*
  16. * ISO C 99 <wctype.h> for platforms that lack it.
  17. * <http://www.opengroup.org/susv3xbd/wctype.h.html>
  18. *
  19. * iswctype, towctrans, towlower, towupper, wctrans, wctype,
  20. * wctrans_t, and wctype_t are not yet implemented.
  21. */
  22. #ifndef _GL_WCTYPE_H
  23. #if __GNUC__ >= 3
  24. @PRAGMA_SYSTEM_HEADER@
  25. #endif
  26. #if @HAVE_WINT_T@
  27. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.
  28. Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
  29. <wchar.h>.
  30. BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
  31. included before <wchar.h>. */
  32. # include <stddef.h>
  33. # include <stdio.h>
  34. # include <time.h>
  35. # include <wchar.h>
  36. #endif
  37. /* Include the original <wctype.h> if it exists.
  38. BeOS 5 has the functions but no <wctype.h>. */
  39. /* The include_next requires a split double-inclusion guard. */
  40. #if @HAVE_WCTYPE_H@
  41. # @INCLUDE_NEXT@ @NEXT_WCTYPE_H@
  42. #endif
  43. #ifndef _GL_WCTYPE_H
  44. #define _GL_WCTYPE_H
  45. /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
  46. /* The definition of _GL_WARN_ON_USE is copied here. */
  47. /* Define wint_t and WEOF. (Also done in wchar.in.h.) */
  48. #if !@HAVE_WINT_T@ && !defined wint_t
  49. # define wint_t int
  50. # ifndef WEOF
  51. # define WEOF -1
  52. # endif
  53. #else
  54. # ifndef WEOF
  55. # define WEOF ((wint_t) -1)
  56. # endif
  57. #endif
  58. /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
  59. Linux libc5 has <wctype.h> and the functions but they are broken.
  60. Assume all 11 functions (all isw* except iswblank) are implemented the
  61. same way, or not at all. */
  62. #if ! @HAVE_ISWCNTRL@ || @REPLACE_ISWCNTRL@
  63. /* IRIX 5.3 has macros but no functions, its isw* macros refer to an
  64. undefined variable _ctmp_ and to <ctype.h> macros like _P, and they
  65. refer to system functions like _iswctype that are not in the
  66. standard C library. Rather than try to get ancient buggy
  67. implementations like this to work, just disable them. */
  68. # undef iswalnum
  69. # undef iswalpha
  70. # undef iswblank
  71. # undef iswcntrl
  72. # undef iswdigit
  73. # undef iswgraph
  74. # undef iswlower
  75. # undef iswprint
  76. # undef iswpunct
  77. # undef iswspace
  78. # undef iswupper
  79. # undef iswxdigit
  80. # undef towlower
  81. # undef towupper
  82. /* Linux libc5 has <wctype.h> and the functions but they are broken. */
  83. # if @REPLACE_ISWCNTRL@
  84. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  85. # define iswalnum rpl_iswalnum
  86. # define iswalpha rpl_iswalpha
  87. # define iswblank rpl_iswblank
  88. # define iswcntrl rpl_iswcntrl
  89. # define iswdigit rpl_iswdigit
  90. # define iswgraph rpl_iswgraph
  91. # define iswlower rpl_iswlower
  92. # define iswprint rpl_iswprint
  93. # define iswpunct rpl_iswpunct
  94. # define iswspace rpl_iswspace
  95. # define iswupper rpl_iswupper
  96. # define iswxdigit rpl_iswxdigit
  97. # define towlower rpl_towlower
  98. # define towupper rpl_towupper
  99. # endif
  100. # endif
  101. static inline int
  102. # if @REPLACE_ISWCNTRL@
  103. rpl_iswalnum
  104. # else
  105. iswalnum
  106. # endif
  107. (wint_t wc)
  108. {
  109. return ((wc >= '0' && wc <= '9')
  110. || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'));
  111. }
  112. static inline int
  113. # if @REPLACE_ISWCNTRL@
  114. rpl_iswalpha
  115. # else
  116. iswalpha
  117. # endif
  118. (wint_t wc)
  119. {
  120. return (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z';
  121. }
  122. static inline int
  123. # if @REPLACE_ISWCNTRL@
  124. rpl_iswblank
  125. # else
  126. iswblank
  127. # endif
  128. (wint_t wc)
  129. {
  130. return wc == ' ' || wc == '\t';
  131. }
  132. static inline int
  133. # if @REPLACE_ISWCNTRL@
  134. rpl_iswcntrl
  135. # else
  136. iswcntrl
  137. # endif
  138. (wint_t wc)
  139. {
  140. return (wc & ~0x1f) == 0 || wc == 0x7f;
  141. }
  142. static inline int
  143. # if @REPLACE_ISWCNTRL@
  144. rpl_iswdigit
  145. # else
  146. iswdigit
  147. # endif
  148. (wint_t wc)
  149. {
  150. return wc >= '0' && wc <= '9';
  151. }
  152. static inline int
  153. # if @REPLACE_ISWCNTRL@
  154. rpl_iswgraph
  155. # else
  156. iswgraph
  157. # endif
  158. (wint_t wc)
  159. {
  160. return wc >= '!' && wc <= '~';
  161. }
  162. static inline int
  163. # if @REPLACE_ISWCNTRL@
  164. rpl_iswlower
  165. # else
  166. iswlower
  167. # endif
  168. (wint_t wc)
  169. {
  170. return wc >= 'a' && wc <= 'z';
  171. }
  172. static inline int
  173. # if @REPLACE_ISWCNTRL@
  174. rpl_iswprint
  175. # else
  176. iswprint
  177. # endif
  178. (wint_t wc)
  179. {
  180. return wc >= ' ' && wc <= '~';
  181. }
  182. static inline int
  183. # if @REPLACE_ISWCNTRL@
  184. rpl_iswpunct
  185. # else
  186. iswpunct
  187. # endif
  188. (wint_t wc)
  189. {
  190. return (wc >= '!' && wc <= '~'
  191. && !((wc >= '0' && wc <= '9')
  192. || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')));
  193. }
  194. static inline int
  195. # if @REPLACE_ISWCNTRL@
  196. rpl_iswspace
  197. # else
  198. iswspace
  199. # endif
  200. (wint_t wc)
  201. {
  202. return (wc == ' ' || wc == '\t'
  203. || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r');
  204. }
  205. static inline int
  206. # if @REPLACE_ISWCNTRL@
  207. rpl_iswupper
  208. # else
  209. iswupper
  210. # endif
  211. (wint_t wc)
  212. {
  213. return wc >= 'A' && wc <= 'Z';
  214. }
  215. static inline int
  216. # if @REPLACE_ISWCNTRL@
  217. rpl_iswxdigit
  218. # else
  219. iswxdigit
  220. # endif
  221. (wint_t wc)
  222. {
  223. return ((wc >= '0' && wc <= '9')
  224. || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F'));
  225. }
  226. static inline wint_t
  227. # if @REPLACE_ISWCNTRL@
  228. rpl_towlower
  229. # else
  230. towlower
  231. # endif
  232. (wint_t wc)
  233. {
  234. return (wc >= 'A' && wc <= 'Z' ? wc - 'A' + 'a' : wc);
  235. }
  236. static inline wint_t
  237. # if @REPLACE_ISWCNTRL@
  238. rpl_towupper
  239. # else
  240. towupper
  241. # endif
  242. (wint_t wc)
  243. {
  244. return (wc >= 'a' && wc <= 'z' ? wc - 'a' + 'A' : wc);
  245. }
  246. #elif ! @HAVE_ISWBLANK@
  247. /* Only the iswblank function is missing. */
  248. static inline int
  249. iswblank (wint_t wc)
  250. {
  251. return wc == ' ' || wc == '\t';
  252. }
  253. #endif
  254. #if defined __MINGW32__
  255. /* On native Windows, wchar_t is uint16_t, and wint_t is uint32_t.
  256. The functions towlower and towupper are implemented in the MSVCRT library
  257. to take a wchar_t argument and return a wchar_t result. mingw declares
  258. these functions to take a wint_t argument and return a wint_t result.
  259. This means that:
  260. 1. When the user passes an argument outside the range 0x0000..0xFFFF, the
  261. function will look only at the lower 16 bits. This is allowed according
  262. to POSIX.
  263. 2. The return value is returned in the lower 16 bits of the result register.
  264. The upper 16 bits are random: whatever happened to be in that part of the
  265. result register. We need to fix this by adding a zero-extend from
  266. wchar_t to wint_t after the call. */
  267. static inline wint_t
  268. rpl_towlower (wint_t wc)
  269. {
  270. return (wint_t) (wchar_t) towlower (wc);
  271. }
  272. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  273. # define towlower rpl_towlower
  274. # endif
  275. static inline wint_t
  276. rpl_towupper (wint_t wc)
  277. {
  278. return (wint_t) (wchar_t) towupper (wc);
  279. }
  280. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  281. # define towupper rpl_towupper
  282. # endif
  283. #endif /* __MINGW32__ */
  284. #if @REPLACE_ISWCNTRL@
  285. _GL_CXXALIAS_RPL (iswalnum, int, (wint_t wc));
  286. _GL_CXXALIAS_RPL (iswalpha, int, (wint_t wc));
  287. _GL_CXXALIAS_RPL (iswblank, int, (wint_t wc));
  288. _GL_CXXALIAS_RPL (iswcntrl, int, (wint_t wc));
  289. _GL_CXXALIAS_RPL (iswdigit, int, (wint_t wc));
  290. _GL_CXXALIAS_RPL (iswgraph, int, (wint_t wc));
  291. _GL_CXXALIAS_RPL (iswlower, int, (wint_t wc));
  292. _GL_CXXALIAS_RPL (iswprint, int, (wint_t wc));
  293. _GL_CXXALIAS_RPL (iswpunct, int, (wint_t wc));
  294. _GL_CXXALIAS_RPL (iswspace, int, (wint_t wc));
  295. _GL_CXXALIAS_RPL (iswupper, int, (wint_t wc));
  296. _GL_CXXALIAS_RPL (iswxdigit, int, (wint_t wc));
  297. #else
  298. _GL_CXXALIAS_SYS (iswalnum, int, (wint_t wc));
  299. _GL_CXXALIAS_SYS (iswalpha, int, (wint_t wc));
  300. _GL_CXXALIAS_SYS (iswblank, int, (wint_t wc));
  301. _GL_CXXALIAS_SYS (iswcntrl, int, (wint_t wc));
  302. _GL_CXXALIAS_SYS (iswdigit, int, (wint_t wc));
  303. _GL_CXXALIAS_SYS (iswgraph, int, (wint_t wc));
  304. _GL_CXXALIAS_SYS (iswlower, int, (wint_t wc));
  305. _GL_CXXALIAS_SYS (iswprint, int, (wint_t wc));
  306. _GL_CXXALIAS_SYS (iswpunct, int, (wint_t wc));
  307. _GL_CXXALIAS_SYS (iswspace, int, (wint_t wc));
  308. _GL_CXXALIAS_SYS (iswupper, int, (wint_t wc));
  309. _GL_CXXALIAS_SYS (iswxdigit, int, (wint_t wc));
  310. #endif
  311. _GL_CXXALIASWARN (iswalnum);
  312. _GL_CXXALIASWARN (iswalpha);
  313. _GL_CXXALIASWARN (iswblank);
  314. _GL_CXXALIASWARN (iswcntrl);
  315. _GL_CXXALIASWARN (iswdigit);
  316. _GL_CXXALIASWARN (iswgraph);
  317. _GL_CXXALIASWARN (iswlower);
  318. _GL_CXXALIASWARN (iswprint);
  319. _GL_CXXALIASWARN (iswpunct);
  320. _GL_CXXALIASWARN (iswspace);
  321. _GL_CXXALIASWARN (iswupper);
  322. _GL_CXXALIASWARN (iswxdigit);
  323. #if @REPLACE_ISWCNTRL@ || defined __MINGW32__
  324. _GL_CXXALIAS_RPL (towlower, wint_t, (wint_t wc));
  325. _GL_CXXALIAS_RPL (towupper, wint_t, (wint_t wc));
  326. #else
  327. _GL_CXXALIAS_SYS (towlower, wint_t, (wint_t wc));
  328. _GL_CXXALIAS_SYS (towupper, wint_t, (wint_t wc));
  329. #endif
  330. _GL_CXXALIASWARN (towlower);
  331. _GL_CXXALIASWARN (towupper);
  332. #endif /* _GL_WCTYPE_H */
  333. #endif /* _GL_WCTYPE_H */