ReactOS 0.4.16-dev-853-g88d9285
isctype.cpp
Go to the documentation of this file.
1//
2// isctype.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Multibyte and debug support functionality for the character classification
7// functions and macros.
8//
9#include <corecrt_internal.h>
10#include <ctype.h>
11#include <locale.h>
12
13
14
15// Ensure that the masks used by GetCharType() match the CRT masks, so that we
16// can simply reinterpret the masks.
17#if _UPPER != C1_UPPER || \
18 _LOWER != C1_LOWER || \
19 _DIGIT != C1_DIGIT || \
20 _SPACE != C1_SPACE || \
21 _PUNCT != C1_PUNCT || \
22 _CONTROL != C1_CNTRL
23 #error Character type masks do not agree in ctype and winnls
24#endif
25
26
27
28// The _chvalidator function is called by the character classification functions
29// in the debug CRT. This function tests the character argument to ensure that
30// it is not out of range. For performance reasons, this function is not used
31// in the retail CRT.
32#if defined _DEBUG
33
34extern "C" int __cdecl _chvalidator(int const c, int const mask)
35{
36 _ASSERTE(c >= -1 && c <= 255);
37 return _chvalidator_l(nullptr, c, mask);
38}
39
40extern "C" int __cdecl _chvalidator_l(_locale_t const locale, int const c, int const mask)
41{
42 _ASSERTE(c >= -1 && c <= 255);
43
44 _LocaleUpdate locale_update(locale);
45
46 return __acrt_locale_get_ctype_array_value(locale_update.GetLocaleT()->locinfo->_public._locale_pctype, c, mask);
47}
48
49#endif // _DEBUG
50
51
52
53// The _isctype and _isctype_l functions provide support to the character
54// classification functions and macros for two-byte multibyte characters.
55// It returns nonzero or zero depending on whether the character argument
56// does or does not satisfy the character class property encoded by the mask.
57//
58// The leadbyte and trailbyte should be packed into the integer c like so:
59// H.......|.......|.......|.......L
60// 0 0 leadbyte trailbyte
61//
62extern "C" int __cdecl _isctype_l(int const c, int const mask, _locale_t const locale)
63{
64 _LocaleUpdate locale_update(locale);
65
66 if (c >= -1 && c <= 255)
67 {
68 // Direct access to _locale_pctype is allowed due to bounds check.
69 return locale_update.GetLocaleT()->locinfo->_public._locale_pctype[c] & mask;
70 }
71
72 size_t const buffer_count{3};
73
74 int buffer_length;
75 char buffer[buffer_count];
76 if (_isleadbyte_fast_internal(c >> 8 & 0xff, locale_update.GetLocaleT()))
77 {
78 buffer[0] = (c >> 8 & 0xff); // Put lead-byte at start of the string
79 buffer[1] = static_cast<char>(c);
80 buffer[2] = '\0';
81 buffer_length = 2;
82 }
83 else
84 {
85 buffer[0] = static_cast<char>(c);
86 buffer[1] = '\0';
87 buffer_length = 1;
88 }
89
90
91 unsigned short character_type[buffer_count]{};
93 locale_update.GetLocaleT(),
95 buffer,
96 buffer_length,
97 character_type,
98 locale_update.GetLocaleT()->locinfo->_public._locale_lc_codepage,
99 TRUE
100 ) == 0)
101 {
102 return 0;
103 }
104
105 return static_cast<int>(character_type[0] & mask);
106}
107
108extern "C" int __cdecl _isctype(int const c, int const mask)
109{
110 if (__acrt_locale_changed())
111 {
112 return _isctype_l(c, mask, nullptr);
113 }
114
116}
BOOL __cdecl __acrt_GetStringTypeA(_locale_t const locale, DWORD const info_type, LPCSTR const string, int const string_size_in_bytes, unsigned short *const char_type, int const code_page, BOOL const error)
#define __cdecl
Definition: accygwin.h:79
Definition: _locale.h:75
__crt_locale_data __acrt_initial_locale_data
Definition: nlsdata.cpp:92
_Check_return_ __forceinline unsigned short __cdecl _isleadbyte_fast_internal(_In_ unsigned char const c, _In_ _locale_t const locale)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
#define TRUE
Definition: types.h:120
static int __CRTDECL __acrt_locale_get_ctype_array_value(_In_reads_(_Char_value+1) unsigned short const *const locale_pctype_array, _In_range_(-1, 255) int const c, _In_ int const mask)
Definition: stubs.c:113
GLuint buffer
Definition: glext.h:5915
const GLubyte * c
Definition: glext.h:8905
GLenum GLint GLuint mask
Definition: glext.h:6028
int __cdecl _isctype(int const c, int const mask)
Definition: isctype.cpp:108
int __cdecl _isctype_l(int const c, int const mask, _locale_t const locale)
Definition: isctype.cpp:62
#define c
Definition: ke_i.h:80
unsigned short const * _locale_pctype
Definition: stubs.c:95
__crt_locale_data_public _public
#define CT_CTYPE1
Definition: winnls.h:239
size_t const buffer_count
Definition: xtoa.cpp:36