msvcrt/tests: Remove dead assignments (Clang).
[wine] / dlls / msvcrt / ctype.c
1 /*
2  * msvcrt.dll ctype functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "msvcrt.h"
22 #include "winnls.h"
23 #include "wine/unicode.h"
24
25 /* Some abbreviations to make the following table readable */
26 #define _C_ MSVCRT__CONTROL
27 #define _S_ MSVCRT__SPACE
28 #define _P_ MSVCRT__PUNCT
29 #define _D_ MSVCRT__DIGIT
30 #define _H_ MSVCRT__HEX
31 #define _U_ MSVCRT__UPPER
32 #define _L_ MSVCRT__LOWER
33
34 WORD MSVCRT__ctype [257] = {
35   0, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|_C_, _S_|_C_,
36   _S_|_C_, _S_|_C_, _S_|_C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_,
37   _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|MSVCRT__BLANK,
38   _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_,
39   _P_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_,
40   _D_|_H_, _D_|_H_, _D_|_H_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _U_|_H_,
41   _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_, _U_, _U_, _U_, _U_,
42   _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_,
43   _U_, _P_, _P_, _P_, _P_, _P_, _P_, _L_|_H_, _L_|_H_, _L_|_H_, _L_|_H_,
44   _L_|_H_, _L_|_H_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_,
45   _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _P_, _P_, _P_, _P_,
46   _C_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
52 };
53
54 /* pctype is used by macros in the Win32 headers. It must point
55  * To a table of flags exactly like ctype. To allow locale
56  * changes to affect ctypes (i.e. isleadbyte), we use a second table
57  * and update its flags whenever the current locale changes.
58  */
59 unsigned short *MSVCRT__pctype = NULL;
60
61 /*********************************************************************
62  *              __p__pctype (MSVCRT.@)
63  */
64 unsigned short** CDECL MSVCRT___p__pctype(void)
65 {
66     return &get_locinfo()->pctype;
67 }
68
69 /*********************************************************************
70  *              __pctype_func (MSVCRT.@)
71  */
72 const unsigned short* CDECL MSVCRT___pctype_func(void)
73 {
74     return get_locinfo()->pctype;
75 }
76
77 /*********************************************************************
78  *              _isctype_l (MSVCRT.@)
79  */
80 int CDECL MSVCRT__isctype_l(int c, int type, MSVCRT__locale_t locale)
81 {
82   MSVCRT_pthreadlocinfo locinfo;
83
84   if(!locale)
85     locinfo = get_locinfo();
86   else
87     locinfo = locale->locinfo;
88
89   if (c >= -1 && c <= 255)
90     return locinfo->pctype[c] & type;
91
92   if (locinfo->mb_cur_max != 1 && c > 0)
93   {
94     /* FIXME: Is there a faster way to do this? */
95     WORD typeInfo;
96     char convert[3], *pconv = convert;
97
98     if (locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
99       *pconv++ = (UINT)c >> 8;
100     *pconv++ = c & 0xff;
101     *pconv = 0;
102
103     if (GetStringTypeExA(locinfo->lc_handle[MSVCRT_LC_CTYPE],
104                 CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
105       return typeInfo & type;
106   }
107   return 0;
108 }
109
110 /*********************************************************************
111  *              _isctype (MSVCRT.@)
112  */
113 int CDECL MSVCRT__isctype(int c, int type)
114 {
115     return MSVCRT__isctype_l(c, type, NULL);
116 }
117
118 /*********************************************************************
119  *              _isalnum_l (MSVCRT.@)
120  */
121 int CDECL MSVCRT__isalnum_l(int c, MSVCRT__locale_t locale)
122 {
123   return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT, locale );
124 }
125
126 /*********************************************************************
127  *              isalnum (MSVCRT.@)
128  */
129 int CDECL MSVCRT_isalnum(int c)
130 {
131   return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
132 }
133
134 /*********************************************************************
135  *              _isalpha_l (MSVCRT.@)
136  */
137 int CDECL MSVCRT__isalpha_l(int c, MSVCRT__locale_t locale)
138 {
139   return MSVCRT__isctype_l( c, MSVCRT__ALPHA, locale );
140 }
141
142 /*********************************************************************
143  *              isalpha (MSVCRT.@)
144  */
145 int CDECL MSVCRT_isalpha(int c)
146 {
147   return MSVCRT__isctype( c, MSVCRT__ALPHA );
148 }
149
150 /*********************************************************************
151  *              _iscntrl_l (MSVCRT.@)
152  */
153 int CDECL MSVCRT__iscntrl_l(int c, MSVCRT__locale_t locale)
154 {
155   return MSVCRT__isctype_l( c, MSVCRT__CONTROL, locale );
156 }
157
158 /*********************************************************************
159  *              iscntrl (MSVCRT.@)
160  */
161 int CDECL MSVCRT_iscntrl(int c)
162 {
163   return MSVCRT__isctype( c, MSVCRT__CONTROL );
164 }
165
166 /*********************************************************************
167  *              _isdigit_l (MSVCRT.@)
168  */
169 int CDECL MSVCRT__isdigit_l(int c, MSVCRT__locale_t locale)
170 {
171   return MSVCRT__isctype_l( c, MSVCRT__DIGIT, locale );
172 }
173
174 /*********************************************************************
175  *              isdigit (MSVCRT.@)
176  */
177 int CDECL MSVCRT_isdigit(int c)
178 {
179   return MSVCRT__isctype( c, MSVCRT__DIGIT );
180 }
181
182 /*********************************************************************
183  *              _isgraph_l (MSVCRT.@)
184  */
185 int CDECL MSVCRT__isgraph_l(int c, MSVCRT__locale_t locale)
186 {
187   return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT, locale );
188 }
189
190 /*********************************************************************
191  *              isgraph (MSVCRT.@)
192  */
193 int CDECL MSVCRT_isgraph(int c)
194 {
195   return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
196 }
197
198 /*********************************************************************
199  *              _isleadbyte_l (MSVCRT.@)
200  */
201 int CDECL MSVCRT__isleadbyte_l(int c, MSVCRT__locale_t locale)
202 {
203   return MSVCRT__isctype_l( c, MSVCRT__LEADBYTE, locale );
204 }
205
206 /*********************************************************************
207  *              isleadbyte (MSVCRT.@)
208  */
209 int CDECL MSVCRT_isleadbyte(int c)
210 {
211   return MSVCRT__isctype( c, MSVCRT__LEADBYTE );
212 }
213
214 /*********************************************************************
215  *              _islower_l (MSVCRT.@)
216  */
217 int CDECL MSVCRT__islower_l(int c, MSVCRT__locale_t locale)
218 {
219   return MSVCRT__isctype_l( c, MSVCRT__LOWER, locale );
220 }
221
222 /*********************************************************************
223  *              islower (MSVCRT.@)
224  */
225 int CDECL MSVCRT_islower(int c)
226 {
227   return MSVCRT__isctype( c, MSVCRT__LOWER );
228 }
229
230 /*********************************************************************
231  *              _isprint_l (MSVCRT.@)
232  */
233 int CDECL MSVCRT__isprint_l(int c, MSVCRT__locale_t locale)
234 {
235   return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT, locale );
236 }
237
238 /*********************************************************************
239  *              isprint (MSVCRT.@)
240  */
241 int CDECL MSVCRT_isprint(int c)
242 {
243   return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
244 }
245
246 /*********************************************************************
247  *              ispunct (MSVCRT.@)
248  */
249 int CDECL MSVCRT_ispunct(int c)
250 {
251   return MSVCRT__isctype( c, MSVCRT__PUNCT );
252 }
253
254 /*********************************************************************
255  *              _isspace_l (MSVCRT.@)
256  */
257 int CDECL MSVCRT__isspace_l(int c, MSVCRT__locale_t locale)
258 {
259   return MSVCRT__isctype_l( c, MSVCRT__SPACE, locale );
260 }
261
262 /*********************************************************************
263  *              isspace (MSVCRT.@)
264  */
265 int CDECL MSVCRT_isspace(int c)
266 {
267   return MSVCRT__isctype( c, MSVCRT__SPACE );
268 }
269
270 /*********************************************************************
271  *              _isupper_l (MSVCRT.@)
272  */
273 int CDECL MSVCRT__isupper_l(int c, MSVCRT__locale_t locale)
274 {
275   return MSVCRT__isctype_l( c, MSVCRT__UPPER, locale );
276 }
277
278 /*********************************************************************
279  *              isupper (MSVCRT.@)
280  */
281 int CDECL MSVCRT_isupper(int c)
282 {
283   return MSVCRT__isctype( c, MSVCRT__UPPER );
284 }
285
286 /*********************************************************************
287  *              _isxdigit_l (MSVCRT.@)
288  */
289 int CDECL MSVCRT__isxdigit_l(int c, MSVCRT__locale_t locale)
290 {
291   return MSVCRT__isctype_l( c, MSVCRT__HEX, locale );
292 }
293
294 /*********************************************************************
295  *              isxdigit (MSVCRT.@)
296  */
297 int CDECL MSVCRT_isxdigit(int c)
298 {
299   return MSVCRT__isctype( c, MSVCRT__HEX );
300 }
301
302 /*********************************************************************
303  *              __isascii (MSVCRT.@)
304  */
305 int CDECL MSVCRT___isascii(int c)
306 {
307   return isascii((unsigned)c);
308 }
309
310 /*********************************************************************
311  *              __toascii (MSVCRT.@)
312  */
313 int CDECL MSVCRT___toascii(int c)
314 {
315   return (unsigned)c & 0x7f;
316 }
317
318 /*********************************************************************
319  *              iswascii (MSVCRT.@)
320  *
321  */
322 int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
323 {
324   return ((unsigned)c < 0x80);
325 }
326
327 /*********************************************************************
328  *              __iscsym (MSVCRT.@)
329  */
330 int CDECL MSVCRT___iscsym(int c)
331 {
332   return (c < 127 && (isalnum(c) || c == '_'));
333 }
334
335 /*********************************************************************
336  *              __iscsymf (MSVCRT.@)
337  */
338 int CDECL MSVCRT___iscsymf(int c)
339 {
340   return (c < 127 && (isalpha(c) || c == '_'));
341 }
342
343 /*********************************************************************
344  *              _toupper_l (MSVCRT.@)
345  */
346 int CDECL MSVCRT__toupper_l(int c, MSVCRT__locale_t locale)
347 {
348     MSVCRT_pthreadlocinfo locinfo;
349
350     if(!locale)
351         locinfo = get_locinfo();
352     else
353         locinfo = locale->locinfo;
354
355     if(c < 256)
356         return locinfo->pcumap[(unsigned char)c];
357
358     if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
359     {
360         WCHAR wide, upper;
361         char str[2], *p = str;
362         *p++ = (c>>8) & 255;
363         *p++ = c & 255;
364
365         if(!MultiByteToWideChar(locinfo->lc_codepage,
366                     MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
367             return c;
368
369         upper = toupperW(wide);
370         if(upper == wide)
371             return c;
372
373         WideCharToMultiByte(locinfo->lc_codepage, 0,
374                 &upper, 1, str, 2, NULL, NULL);
375
376         return str[0] + (str[1]<<8);
377     }
378
379     return c;
380 }
381
382 /*********************************************************************
383  *              toupper (MSVCRT.@)
384  */
385 int CDECL MSVCRT_toupper(int c)
386 {
387     return MSVCRT__toupper_l(c, NULL);
388 }
389
390 /*********************************************************************
391  *              _toupper (MSVCRT.@)
392  */
393 int CDECL MSVCRT__toupper(int c)
394 {
395     return c - 0x20;  /* sic */
396 }
397
398 /*********************************************************************
399  *              _tolower_l (MSVCRT.@)
400  */
401 int CDECL MSVCRT__tolower_l(int c, MSVCRT__locale_t locale)
402 {
403     MSVCRT_pthreadlocinfo locinfo;
404
405     if(!locale)
406         locinfo = get_locinfo();
407     else
408         locinfo = locale->locinfo;
409
410     if(c < 256)
411         return locinfo->pclmap[(unsigned char)c];
412
413     if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
414     {
415         WCHAR wide, upper;
416         char str[2], *p = str;
417         *p++ = (c>>8) & 255;
418         *p++ = c & 255;
419
420         if(!MultiByteToWideChar(locinfo->lc_codepage,
421                     MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
422             return c;
423
424         upper = tolowerW(wide);
425         if(upper == wide)
426             return c;
427
428         WideCharToMultiByte(locinfo->lc_codepage, 0,
429                 &upper, 1, str, 2, NULL, NULL);
430
431         return str[0] + (str[1]<<8);
432     }
433
434     return c;
435 }
436
437 /*********************************************************************
438  *              tolower (MSVCRT.@)
439  */
440 int CDECL MSVCRT_tolower(int c)
441 {
442         return MSVCRT__tolower_l(c, NULL);
443 }
444
445 /*********************************************************************
446  *              _tolower (MSVCRT.@)
447  */
448 int CDECL MSVCRT__tolower(int c)
449 {
450     return c + 0x20;  /* sic */
451 }