ReactOS 0.4.16-dev-853-g88d9285
tolower_toupper.cpp
Go to the documentation of this file.
1//
2// tolower_toupper.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The narrow-string tolower and toupper functions, which convert the integer
7// representation of a character to its lowercase equivalent. These functions
8// are defined as macros in the headers.
9//
10#include <corecrt_internal.h>
11#include <ctype.h>
12#include <locale.h>
13#include <stdio.h>
14
15typedef unsigned char (__cdecl internal_map_type)(unsigned char, _locale_t);
16
17static __forceinline int __cdecl common_tox_win_lookup(int const c, DWORD const map_flag, _locale_t const locale) throw()
18{
19 // Convert the character to a multibyte string:
20 size_t in_count {};
21 unsigned char in_buffer[3]{};
22 if (locale->locinfo->_public._locale_mb_cur_max > 1 && _isleadbyte_fast_internal(c >> 8 & 0xff, locale))
23 {
24 in_buffer[0] = c >> 8 & 0xff; // Put the lead byte at start of the string
25 in_buffer[1] = static_cast<unsigned char>(c);
26 in_buffer[2] = 0;
27 in_count = 2;
28 }
29 else
30 {
31 // This is undefined behavior: should probably use the wide form instead
32 errno = EILSEQ;
33 in_buffer[0] = static_cast<unsigned char>(c);
34 in_buffer[1] = 0;
35 in_count = 1;
36 }
37
38 // Convert the wide character equivalent to the target case:
39 unsigned char out_buffer[3]{};
41 locale,
42 locale->locinfo->locale_name[LC_CTYPE],
43 map_flag,
44 reinterpret_cast<char const*>(in_buffer),
45 static_cast<int>(in_count),
46 reinterpret_cast<char*>(out_buffer),
47 static_cast<int>(_countof(out_buffer)),
48 locale->locinfo->_public._locale_lc_codepage,
49 TRUE);
50
51 if (out_count == 0)
52 {
53 return c;
54 }
55
56 // Form the integer return value:
57 if (out_count == 1)
58 {
59 return static_cast<int>(out_buffer[0]);
60 }
61
62 return static_cast<int>(out_buffer[1]) | (static_cast<int>(out_buffer[0]) << 8);
63}
64
65template <internal_map_type& MapType>
66static __forceinline int __cdecl common_tox_l(int const c, DWORD const map_flag, _locale_t const locale) throw()
67{
68 if (c == EOF)
69 {
70 return EOF;
71 }
72
73 _LocaleUpdate locale_update(locale);
74
75 if (static_cast<unsigned>(c) < 256)
76 {
77 return MapType(static_cast<unsigned char>(c), locale_update.GetLocaleT());
78 }
79
80 return common_tox_win_lookup(c, map_flag, locale_update.GetLocaleT());
81}
82
83extern "C" int __cdecl _tolower_l(int const c, _locale_t const locale)
84{
85 return common_tox_l<_tolower_fast_internal>(c, LCMAP_LOWERCASE, locale);
86}
87
88extern "C" int __cdecl tolower(int const c)
89{
90 return __acrt_locale_changed()
91 ? _tolower_l(c, nullptr)
93}
94
95extern "C" int (__cdecl _tolower)(int const c)
96{
97 return c - 'A' + 'a';
98}
99
100
101
102extern "C" int __cdecl _toupper_l(int const c, _locale_t const locale)
103{
104 return common_tox_l<_toupper_fast_internal>(c, LCMAP_UPPERCASE, locale);
105}
106
107extern "C" int __cdecl toupper(int const c)
108{
109 return __acrt_locale_changed()
110 ? _toupper_l(c, nullptr)
112}
113
114extern "C" int (__cdecl _toupper)(int const c)
115{
116 return c - 'a' + 'A';
117}
int __cdecl __acrt_LCMapStringA(_locale_t const plocinfo, PCWSTR const LocaleName, DWORD const dwMapFlags, PCCH const lpSrcStr, int const cchSrc, PCH const lpDestStr, int const cchDest, int const code_page, BOOL const bError)
#define __cdecl
Definition: accygwin.h:79
Definition: _locale.h:75
_Check_return_ __forceinline unsigned short __cdecl _isleadbyte_fast_internal(_In_ unsigned char const c, _In_ _locale_t const locale)
#define TRUE
Definition: types.h:120
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned long DWORD
Definition: ntddk_ex.h:95
const GLubyte * c
Definition: glext.h:8905
static unsigned char * in_buffer
Definition: iccvid.c:87
#define _tolower(_Char)
Definition: ctype.h:654
#define _toupper(_Char)
Definition: ctype.h:655
#define LC_CTYPE
Definition: locale.h:19
#define EOF
Definition: stdio.h:24
__forceinline int __CRTDECL __ascii_tolower(int const _C)
Definition: ctype.h:152
__forceinline int __CRTDECL __ascii_toupper(int const _C)
Definition: ctype.h:161
#define c
Definition: ke_i.h:80
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK ULONG PVOID ULONG PVOID out_buffer
Definition: file.c:100
static UINT UINT * out_count
Definition: clipboard.c:35
#define errno
Definition: errno.h:18
#define EILSEQ
Definition: errno.h:109
#define _countof(array)
Definition: sndvol32.h:70
int __cdecl toupper(int const c)
int __cdecl tolower(int const c)
static __forceinline int __cdecl common_tox_l(int const c, DWORD const map_flag, _locale_t const locale)
static __forceinline int __cdecl common_tox_win_lookup(int const c, DWORD const map_flag, _locale_t const locale)
unsigned char(__cdecl internal_map_type)(unsigned char
int __cdecl _toupper_l(int const c, _locale_t const locale)
unsigned _locale_t
int __cdecl _tolower_l(int const c, _locale_t const locale)
#define LCMAP_UPPERCASE
Definition: winnls.h:187
#define LCMAP_LOWERCASE
Definition: winnls.h:186
#define const
Definition: zconf.h:233