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, 2017, 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>; we use the quoted
53 * form of inclusion here, to ensure that we get our own "wctype.h",
54 * and not any predecessor which may have been insinuated into the
55 * system include path, and which may interfere with our mechanism
56 * for partial inclusion of shared header content).
58 #define __CTYPE_H_SOURCED__
61 /* This is the one character classification macro, for which definition
62 * is NOT delegated to <wctype.h>
64 #define _LEADBYTE 0x8000
70 _CRTIMP __cdecl __MINGW_NOTHROW int isalnum(int);
71 _CRTIMP __cdecl __MINGW_NOTHROW int isalpha(int);
72 _CRTIMP __cdecl __MINGW_NOTHROW int iscntrl(int);
73 _CRTIMP __cdecl __MINGW_NOTHROW int isdigit(int);
74 _CRTIMP __cdecl __MINGW_NOTHROW int isgraph(int);
75 _CRTIMP __cdecl __MINGW_NOTHROW int islower(int);
76 _CRTIMP __cdecl __MINGW_NOTHROW int isleadbyte (int);
77 _CRTIMP __cdecl __MINGW_NOTHROW int isprint(int);
78 _CRTIMP __cdecl __MINGW_NOTHROW int ispunct(int);
79 _CRTIMP __cdecl __MINGW_NOTHROW int isspace(int);
80 _CRTIMP __cdecl __MINGW_NOTHROW int isupper(int);
81 _CRTIMP __cdecl __MINGW_NOTHROW int isxdigit(int);
84 __cdecl __MINGW_NOTHROW int isblank (int);
87 #ifndef __STRICT_ANSI__
88 _CRTIMP __cdecl __MINGW_NOTHROW int _isctype (int, int);
91 /* These are ISO-C conforming functions, with correct checking of
92 * argument; they are exported by MSVCRT.DLL, and by CRTDLL.DLL as
93 * such, and are NOT simply old-name mapped variants; however...
95 _CRTIMP __cdecl __MINGW_NOTHROW int tolower (int);
96 _CRTIMP __cdecl __MINGW_NOTHROW int toupper (int);
98 #ifndef __STRICT_ANSI__
99 /* ...Microsoft's beloved underscore decorated variants are also
100 * exported. These are cheap non-standard versions: their return
101 * values are undefined if the argument is not an ASCII character,
102 * or is not of appropriate case for the requested conversion.
104 * Note that these appear to be approximately equivalent to POSIX
105 * macros of the same names, (declared obsolescent in POSIX.1-2008),
106 * but are implemented as DLL functions, rather than macros.
108 _CRTIMP __cdecl __MINGW_NOTHROW int _tolower (int);
109 _CRTIMP __cdecl __MINGW_NOTHROW int _toupper (int);
113 /* FIXME: also defined in stdlib.h; should be factored into ONE
114 * location ONLY, and included into the other.
116 # ifdef __DECLSPEC_SUPPORTED
118 # define MB_CUR_MAX __mb_cur_max
119 __MINGW_IMPORT int __mb_cur_max;
121 # else /* !__MSVCRT__ */
122 # define MB_CUR_MAX __mb_cur_max_dll
123 __MINGW_IMPORT int __mb_cur_max_dll;
124 # endif /* !__MSVCRT__ */
126 # else /* !__DECLSPEC_SUPPORTED */
128 # define MB_CUR_MAX (*_imp____mb_cur_max)
129 extern int *_imp____mb_cur_max;
131 # else /* !__MSVCRT__ */
132 # define MB_CUR_MAX (*_imp____mb_cur_max_dll)
133 extern int *_imp____mb_cur_max_dll;
134 # endif /* !__MSVCRT__ */
135 # endif /* __DECLSPEC_SUPPORTED */
136 #endif /* ! MB_CUR_MAX */
138 #ifdef __DECLSPEC_SUPPORTED
139 # if __MSVCRT_VERSION__ <= __MSVCR70_DLL
140 __MINGW_IMPORT unsigned short _ctype[];
144 __MINGW_IMPORT unsigned short *_pctype;
146 # else /* !__MSVCRT__ (implies CRTDLL) */
147 # define _pctype _pctype_dll
148 __MINGW_IMPORT unsigned short *_pctype_dll;
151 #else /* !__DECLSPEC_SUPPORTED */
152 # if __MSVCRT_VERSION__ <= __MSVCR70_DLL
153 # define _ctype (*_imp___ctype)
154 extern unsigned short **_imp___ctype;
158 # define _pctype (*_imp___pctype)
159 extern unsigned short **_imp___pctype;
161 # else /* !__MSVCRT__ (implies CRTDLL) */
162 # define _pctype (*_imp___pctype_dll)
163 extern unsigned short **_imp___pctype_dll;
165 #endif /* !__DECLSPEC_SUPPORTED */
167 /* Use inlines here rather than macros, because macros will upset
168 * C++ usage (eg, ::isalnum), and so usually get undefined
170 * According to standard for SB chars, these function are defined only
171 * for input values representable by unsigned char or EOF.
172 * Thus, there is no range test.
173 * This reproduces behaviour of MSVCRT.dll lib implemention for SB chars.
175 * If no MB char support is needed, these can be simplified even
176 * more by command line define -DMB_CUR_MAX=1. The compiler will then
177 * optimise away the constant condition.
180 #if !(defined (__NO_INLINE__) || defined (__NO_CTYPE_INLINES) \
181 || defined (__STRICT_ANSI__))
183 /* Use simple lookup in single byte locales, else _isctype()
185 #define __ISCTYPE(c, mask) (MB_CUR_MAX == 1 ? (_pctype[c] & mask) : _isctype (c, mask))
187 __CRT_INLINE __cdecl __MINGW_NOTHROW int isalnum (int c)
188 { return __ISCTYPE(c, (_ALPHA|_DIGIT)); }
190 __CRT_INLINE __cdecl __MINGW_NOTHROW int isalpha (int c)
191 { return __ISCTYPE(c, _ALPHA); }
193 __CRT_INLINE __cdecl __MINGW_NOTHROW int iscntrl (int c)
194 { return __ISCTYPE(c, _CONTROL); }
196 __CRT_INLINE __cdecl __MINGW_NOTHROW int isdigit (int c)
197 { return __ISCTYPE(c, _DIGIT); }
199 __CRT_INLINE __cdecl __MINGW_NOTHROW int isgraph (int c)
200 { return __ISCTYPE(c, (_PUNCT|_ALPHA|_DIGIT)); }
202 __CRT_INLINE __cdecl __MINGW_NOTHROW int islower (int c)
203 { return __ISCTYPE(c, _LOWER); }
205 __CRT_INLINE __cdecl __MINGW_NOTHROW int isprint (int c)
206 { return __ISCTYPE(c, (_BLANK|_PUNCT|_ALPHA|_DIGIT)); }
208 __CRT_INLINE __cdecl __MINGW_NOTHROW int ispunct (int c)
209 { return __ISCTYPE(c, _PUNCT); }
211 __CRT_INLINE __cdecl __MINGW_NOTHROW int isspace (int c)
212 { return __ISCTYPE(c, _SPACE); }
214 __CRT_INLINE __cdecl __MINGW_NOTHROW int isupper (int c)
215 { return __ISCTYPE(c, _UPPER); }
217 __CRT_INLINE __cdecl __MINGW_NOTHROW int isxdigit (int c)
218 { return __ISCTYPE(c, _HEX); }
221 __CRT_INLINE __cdecl __MINGW_NOTHROW int isblank (int c)
222 { return (__ISCTYPE(c, _BLANK) || c == '\t'); }
225 /* These reproduce the behaviour of their DLL exported namesakes,
226 * and thus ARE the effective equivalents of the similarly named,
227 * and now-obsolescent, POSIX macros.
229 __CRT_INLINE __cdecl __MINGW_NOTHROW int _tolower (int c)
230 { return (c - 'A' + 'a'); }
232 __CRT_INLINE __cdecl __MINGW_NOTHROW int _toupper (int c)
233 { return (c - 'a' + 'A'); }
235 /* TODO: is it worth inlining ANSI tolower(), and toupper()?
236 * Probably only if we only want C-locale.
238 #endif /* _NO_CTYPE_INLINES */
240 __CRT_INLINE __cdecl __MINGW_NOTHROW int isleadbyte (int c)
241 { return (_pctype[(unsigned char)(c)] & _LEADBYTE); }
243 #ifndef __STRICT_ANSI__
244 __cdecl __MINGW_NOTHROW int __isascii (int);
245 __cdecl __MINGW_NOTHROW int __toascii (int);
246 __cdecl __MINGW_NOTHROW int __iscsymf (int); /* Valid as first character in a C symbol */
247 __cdecl __MINGW_NOTHROW int __iscsym (int); /* Valid character in C symbol (after first) */
249 #if !(defined (__NO_INLINE__) || defined (__NO_CTYPE_INLINES))
251 __CRT_INLINE __cdecl __MINGW_NOTHROW int __isascii (int c)
252 { return ((c & ~0x7F) == 0); }
254 __CRT_INLINE __cdecl __MINGW_NOTHROW int __toascii (int c)
255 { return (c & 0x7F); }
257 __CRT_INLINE __cdecl __MINGW_NOTHROW int __iscsymf (int c)
258 { return (isalpha(c) || (c == '_')); }
260 __CRT_INLINE __cdecl __MINGW_NOTHROW int __iscsym (int c)
261 { return (isalnum(c) || (c == '_')); }
263 #endif /* !__NO_CTYPE_INLINES */
267 __cdecl __MINGW_NOTHROW int isascii (int);
268 __cdecl __MINGW_NOTHROW int toascii (int);
269 __cdecl __MINGW_NOTHROW int iscsymf (int);
270 __cdecl __MINGW_NOTHROW int iscsym (int);
271 #endif /* !_NO_OLDNAMES */
273 #endif /* !__STRICT_ANSI__ */
277 #endif /* ! RC_INVOKED */
279 #undef __CTYPE_H_SOURCED__
280 #endif /* !_CTYPE_H: $RCSfile$: end of file */