4 * Functions for character classification and conversion.
8 * Written by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
9 * Copyright (C) 1997-2008, 2016, MinGW.org Project
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice, this permission notice, and the following
20 * disclaimer shall be included in all copies or substantial portions of
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
29 * DEALINGS IN THE SOFTWARE.
33 #pragma GCC system_header
36 /* All MinGW headers must include <_mingw.h>
40 /* Although conflicting with both ISO-C and POSIX, Microsoft say that,
41 * in addition to the single byte character classification API which is
42 * properly declared here, this file should also declare elements of the
43 * wide classification API, which is properly declared in <wctype.h>
45 * To avoid the burden of maintaining duplicate declarations, in two
46 * locations, we keep the wide character declarations where ISO-C and
47 * POSIX say they belong, in <wctype.h>, while accommodating Microsoft
48 * compatibility by providing for selective inclusion of the relevant
49 * elements of it here. (Note that we must do this early, because to
50 * avoid duplication, we delegate the definition of common character
51 * classification macros, with the exception of _LEADBYTE, which is
52 * not required in both headers, to <wctype.h>).
54 #define __CTYPE_H_SOURCED__
57 /* This is the one character classification macro, for which definition
58 * is NOT delegated to <wctype.h>
60 #define _LEADBYTE 0x8000
66 _CRTIMP __cdecl __MINGW_NOTHROW int isalnum(int);
67 _CRTIMP __cdecl __MINGW_NOTHROW int isalpha(int);
68 _CRTIMP __cdecl __MINGW_NOTHROW int iscntrl(int);
69 _CRTIMP __cdecl __MINGW_NOTHROW int isdigit(int);
70 _CRTIMP __cdecl __MINGW_NOTHROW int isgraph(int);
71 _CRTIMP __cdecl __MINGW_NOTHROW int islower(int);
72 _CRTIMP __cdecl __MINGW_NOTHROW int isleadbyte (int);
73 _CRTIMP __cdecl __MINGW_NOTHROW int isprint(int);
74 _CRTIMP __cdecl __MINGW_NOTHROW int ispunct(int);
75 _CRTIMP __cdecl __MINGW_NOTHROW int isspace(int);
76 _CRTIMP __cdecl __MINGW_NOTHROW int isupper(int);
77 _CRTIMP __cdecl __MINGW_NOTHROW int isxdigit(int);
79 #if __STDC_VERSION__ >= 199901L || !defined __STRICT_ANSI__
80 __cdecl __MINGW_NOTHROW int isblank (int);
83 #ifndef __STRICT_ANSI__
84 _CRTIMP __cdecl __MINGW_NOTHROW int _isctype (int, int);
87 /* These are ISO-C conforming functions, with correct checking of
88 * argument; they are exported by MSVCRT.DLL, and by CRTDLL.DLL as
89 * such, and are NOT simply old-name mapped variants; however...
91 _CRTIMP __cdecl __MINGW_NOTHROW int tolower (int);
92 _CRTIMP __cdecl __MINGW_NOTHROW int toupper (int);
94 #ifndef __STRICT_ANSI__
95 /* ...Microsoft's beloved underscore decorated variants are also
96 * exported. These are cheap non-standard versions: their return
97 * values are undefined if the argument is not an ASCII character,
98 * or is not of appropriate case for the requested conversion.
100 * Note that these appear to be approximately equivalent to POSIX
101 * macros of the same names, (declared obsolescent in POSIX.1-2008),
102 * but are implemented as DLL functions, rather than macros.
104 _CRTIMP __cdecl __MINGW_NOTHROW int _tolower (int);
105 _CRTIMP __cdecl __MINGW_NOTHROW int _toupper (int);
109 /* FIXME: also defined in stdlib.h; should be factored into ONE
110 * location ONLY, and included into the other.
112 # ifdef __DECLSPEC_SUPPORTED
114 # define MB_CUR_MAX __mb_cur_max
115 __MINGW_IMPORT int __mb_cur_max;
117 # else /* !__MSVCRT__ */
118 # define MB_CUR_MAX __mb_cur_max_dll
119 __MINGW_IMPORT int __mb_cur_max_dll;
120 # endif /* !__MSVCRT__ */
122 # else /* !__DECLSPEC_SUPPORTED */
124 # define MB_CUR_MAX (*_imp____mb_cur_max)
125 extern int *_imp____mb_cur_max;
127 # else /* !__MSVCRT__ */
128 # define MB_CUR_MAX (*_imp____mb_cur_max_dll)
129 extern int *_imp____mb_cur_max_dll;
130 # endif /* !__MSVCRT__ */
131 # endif /* __DECLSPEC_SUPPORTED */
132 #endif /* ! MB_CUR_MAX */
134 #ifdef __DECLSPEC_SUPPORTED
135 # if __MSVCRT_VERSION__ <= __MSVCR70_DLL
136 __MINGW_IMPORT unsigned short _ctype[];
140 __MINGW_IMPORT unsigned short *_pctype;
142 # else /* !__MSVCRT__ (implies CRTDLL) */
143 # define _pctype _pctype_dll
144 __MINGW_IMPORT unsigned short *_pctype_dll;
147 #else /* !__DECLSPEC_SUPPORTED */
148 # if __MSVCRT_VERSION__ <= __MSVCR70_DLL
149 # define _ctype (*_imp___ctype)
150 extern unsigned short **_imp___ctype;
154 # define _pctype (*_imp___pctype)
155 extern unsigned short **_imp___pctype;
157 # else /* !__MSVCRT__ (implies CRTDLL) */
158 # define _pctype (*_imp___pctype_dll)
159 extern unsigned short **_imp___pctype_dll;
161 #endif /* !__DECLSPEC_SUPPORTED */
163 /* Use inlines here rather than macros, because macros will upset
164 * C++ usage (eg, ::isalnum), and so usually get undefined
166 * According to standard for SB chars, these function are defined only
167 * for input values representable by unsigned char or EOF.
168 * Thus, there is no range test.
169 * This reproduces behaviour of MSVCRT.dll lib implemention for SB chars.
171 * If no MB char support is needed, these can be simplified even
172 * more by command line define -DMB_CUR_MAX=1. The compiler will then
173 * optimise away the constant condition.
176 #if !(defined (__NO_INLINE__) || defined (__NO_CTYPE_INLINES) \
177 || defined (__STRICT_ANSI__))
179 /* Use simple lookup in single byte locales, else _isctype()
181 #define __ISCTYPE(c, mask) (MB_CUR_MAX == 1 ? (_pctype[c] & mask) : _isctype (c, mask))
183 __CRT_INLINE __cdecl __MINGW_NOTHROW int isalnum (int c)
184 { return __ISCTYPE(c, (_ALPHA|_DIGIT)); }
186 __CRT_INLINE __cdecl __MINGW_NOTHROW int isalpha (int c)
187 { return __ISCTYPE(c, _ALPHA); }
189 __CRT_INLINE __cdecl __MINGW_NOTHROW int iscntrl (int c)
190 { return __ISCTYPE(c, _CONTROL); }
192 __CRT_INLINE __cdecl __MINGW_NOTHROW int isdigit (int c)
193 { return __ISCTYPE(c, _DIGIT); }
195 __CRT_INLINE __cdecl __MINGW_NOTHROW int isgraph (int c)
196 { return __ISCTYPE(c, (_PUNCT|_ALPHA|_DIGIT)); }
198 __CRT_INLINE __cdecl __MINGW_NOTHROW int islower (int c)
199 { return __ISCTYPE(c, _LOWER); }
201 __CRT_INLINE __cdecl __MINGW_NOTHROW int isprint (int c)
202 { return __ISCTYPE(c, (_BLANK|_PUNCT|_ALPHA|_DIGIT)); }
204 __CRT_INLINE __cdecl __MINGW_NOTHROW int ispunct (int c)
205 { return __ISCTYPE(c, _PUNCT); }
207 __CRT_INLINE __cdecl __MINGW_NOTHROW int isspace (int c)
208 { return __ISCTYPE(c, _SPACE); }
210 __CRT_INLINE __cdecl __MINGW_NOTHROW int isupper (int c)
211 { return __ISCTYPE(c, _UPPER); }
213 __CRT_INLINE __cdecl __MINGW_NOTHROW int isxdigit (int c)
214 { return __ISCTYPE(c, _HEX); }
216 #if __STDC_VERSION__ >= 199901L || !defined __STRICT_ANSI__
217 __CRT_INLINE __cdecl __MINGW_NOTHROW int isblank (int c)
218 { return (__ISCTYPE(c, _BLANK) || c == '\t'); }
221 /* These reproduce the behaviour of their DLL exported namesakes,
222 * and thus ARE the effective equivalents of the similarly named,
223 * and now-obsolescent, POSIX macros.
225 __CRT_INLINE __cdecl __MINGW_NOTHROW int _tolower (int c)
226 { return (c - 'A' + 'a'); }
228 __CRT_INLINE __cdecl __MINGW_NOTHROW int _toupper (int c)
229 { return (c - 'a' + 'A'); }
231 /* TODO: is it worth inlining ANSI tolower(), and toupper()?
232 * Probably only if we only want C-locale.
234 #endif /* _NO_CTYPE_INLINES */
236 __CRT_INLINE __cdecl __MINGW_NOTHROW int isleadbyte (int c)
237 { return (_pctype[(unsigned char)(c)] & _LEADBYTE); }
239 #ifndef __STRICT_ANSI__
240 __cdecl __MINGW_NOTHROW int __isascii (int);
241 __cdecl __MINGW_NOTHROW int __toascii (int);
242 __cdecl __MINGW_NOTHROW int __iscsymf (int); /* Valid as first character in a C symbol */
243 __cdecl __MINGW_NOTHROW int __iscsym (int); /* Valid character in C symbol (after first) */
245 #if !(defined (__NO_INLINE__) || defined (__NO_CTYPE_INLINES))
247 __CRT_INLINE __cdecl __MINGW_NOTHROW int __isascii (int c)
248 { return ((c & ~0x7F) == 0); }
250 __CRT_INLINE __cdecl __MINGW_NOTHROW int __toascii (int c)
251 { return (c & 0x7F); }
253 __CRT_INLINE __cdecl __MINGW_NOTHROW int __iscsymf (int c)
254 { return (isalpha(c) || (c == '_')); }
256 __CRT_INLINE __cdecl __MINGW_NOTHROW int __iscsym (int c)
257 { return (isalnum(c) || (c == '_')); }
259 #endif /* !__NO_CTYPE_INLINES */
263 __cdecl __MINGW_NOTHROW int isascii (int);
264 __cdecl __MINGW_NOTHROW int toascii (int);
265 __cdecl __MINGW_NOTHROW int iscsymf (int);
266 __cdecl __MINGW_NOTHROW int iscsym (int);
267 #endif /* !_NO_OLDNAMES */
269 #endif /* !__STRICT_ANSI__ */
273 #endif /* ! RC_INVOKED */
275 #undef __CTYPE_H_SOURCED__
276 #endif /* !_CTYPE_H: $RCSfile$: end of file */