xref: /reactos/sdk/lib/ucrt/convert/towupper.cpp (revision b09b5584)
1 /***
2 *towupper.cpp - convert wide character to upper case
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       Defines towupper().
8 *
9 *******************************************************************************/
10 #include <corecrt_internal.h>
11 #include <ctype.h>
12 #include <locale.h>
13 
14 /***
15 *wint_t _towupper_l(c, ptloci) - convert wide character to upper case
16 *
17 *Purpose:
18 *       Multi-thread function only! Non-locking version of towupper.
19 *
20 *Entry:
21 *
22 *Exit:
23 *
24 *Exceptions:
25 *
26 *******************************************************************************/
27 
28 extern "C" wint_t __cdecl _towupper_l(
29         wint_t const c,
30         _locale_t const plocinfo
31         )
32 {
33     if (c == WEOF)
34     {
35         return c;
36     }
37 
38     _LocaleUpdate _loc_update(plocinfo);
39     auto const locinfo = _loc_update.GetLocaleT()->locinfo;
40 
41     if (locinfo->_public._locale_lc_codepage == CP_UTF8)
42     {
43         // For 128 <= c < 256, the toupper map would consider the wide character as it is in UTF-8, not UTF-16.
44         // Below 128, UTF-8 and UTF-16 have the same encodings, so we can use the table.
45         if (c < 128)
46         {
47             return _towupper_fast_internal(static_cast<unsigned char>(c), _loc_update.GetLocaleT());
48         }
49     }
50     else
51     {
52         // For 128 <= c < 256, the toupper map will consider the wide character as it would be in the current narrow code page,
53         // which can lead to unexpected behavior. This behavior is maintained for backwards compatibility.
54         if (c < 256)
55         {
56             return _towupper_fast_internal(static_cast<unsigned char>(c), _loc_update.GetLocaleT());
57         }
58 
59         if (locinfo->locale_name[LC_CTYPE] == nullptr)
60         {
61             // If the locale is C, then the only characters that would be transformed are <256
62             // and have been processed already.
63             return c;
64         }
65     }
66 
67     wint_t widechar;
68     if (0 == __acrt_LCMapStringW(
69                 locinfo->locale_name[LC_CTYPE],
70                 LCMAP_UPPERCASE,
71                 (LPCWSTR)&c,
72                 1,
73                 (LPWSTR)&widechar,
74                 1))
75     {
76         return c;
77     }
78 
79     return widechar;
80 }
81 
82 /***
83 *wint_t towupper(c) - convert wide character to upper case
84 *
85 *Purpose:
86 *       towupper() returns the uppercase equivalent of its argument
87 *
88 *Entry:
89 *       c - wint_t value of character to be converted
90 *
91 *Exit:
92 *       if c is a lower case letter, returns wint_t value of upper case
93 *       representation of c. otherwise, it returns c.
94 *
95 *Exceptions:
96 *
97 *******************************************************************************/
98 
99 extern "C" wint_t __cdecl towupper (
100         wint_t c
101         )
102 {
103 
104     return _towupper_l(c, nullptr);
105 }
106