ReactOS 0.4.16-dev-822-gbcedb53
mblen.cpp
Go to the documentation of this file.
1//
2// mblen.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The mblen() and _mblen_l() functions, which return the number of bytes
7// contained in a multibyte character.
8//
11
12using namespace __crt_mbstring;
13
14// Computes the number of bytes contained in a multibyte character. If the string
15// is null, zero is returned to indicate that we support only state-independent
16// character encodings. If the next max_count bytes of the string are not a valid
17// multibyte character, -1 is returned. Otherwise, the number of bytes that
18// compose the next multibyte character are returned.
20 char const* const string,
21 size_t const max_count,
22 __crt_cached_ptd_host& ptd
23 )
24{
26 if (!string || *string == '\0' || max_count == 0)
27 {
28 internal_state = {};
29 return 0;
30 }
31
32 _locale_t const locale = ptd.get_locale();
33
34 if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
35 {
36 int result = static_cast<int>(__mbrtowc_utf8(nullptr, string, max_count, &internal_state, ptd));
37 if (result < 0)
38 {
39 result = -1;
40 }
41 return result;
42 }
43
45 locale->locinfo->_public._locale_mb_cur_max == 1 ||
46 locale->locinfo->_public._locale_mb_cur_max == 2);
47
48 if (_isleadbyte_fast_internal(static_cast<unsigned char>(*string), locale))
49 {
50 _ASSERTE(locale->locinfo->_public._locale_lc_codepage != CP_UTF8 && L"UTF-8 isn't supported in this _mblen_l function yet!!!");
51
52 // If this is a lead byte, then the codepage better be a multibyte codepage
53 _ASSERTE(locale->locinfo->_public._locale_mb_cur_max > 1);
54
55 // Multi-byte character; verify that it is valid:
56 if (locale->locinfo->_public._locale_mb_cur_max <= 1)
57 {
58 return -1;
59 }
60
61 if (max_count > INT_MAX || static_cast<int>(max_count) < locale->locinfo->_public._locale_mb_cur_max)
62 {
63 return -1;
64 }
65
66 int const status = __acrt_MultiByteToWideChar(
67 locale->locinfo->_public._locale_lc_codepage,
69 string,
70 locale->locinfo->_public._locale_mb_cur_max,
71 nullptr,
72 0);
73
74 if (status == 0)
75 {
76 return -1;
77 }
78
79 return locale->locinfo->_public._locale_mb_cur_max;
80 }
81 else
82 {
83 // Single byte character; verify that it is valid:
84 // CP_ACP is known to be valid for all values
85 if (locale->locinfo->_public._locale_lc_codepage != CP_ACP)
86 {
87 int const status = __acrt_MultiByteToWideChar(
88 locale->locinfo->_public._locale_lc_codepage,
90 string,
91 1,
92 nullptr,
93 0);
94
95 if (status == 0)
96 {
97 return -1;
98 }
99 }
100
101 return sizeof(char);
102 }
103}
104
105extern "C" int __cdecl _mblen_l(
106 char const* const string,
107 size_t const max_count,
108 _locale_t const locale
109 )
110{
111 __crt_cached_ptd_host ptd(locale);
112 return _mblen_internal(string, max_count, ptd);
113}
114
115
116
117extern "C" int __cdecl mblen(
118 char const* const string,
119 size_t const max_count
120 )
121{
122 __crt_cached_ptd_host ptd;
123 return _mblen_internal(string, max_count, ptd);
124}
#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 _ASSERTE(expr)
Definition: crtdbg.h:114
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
#define CP_ACP
Definition: compat.h:109
unsigned char
Definition: typeof.h:29
Character const *const size_t const max_count
Definition: fullpath.cpp:66
GLuint64EXT * result
Definition: glext.h:11304
#define MB_ERR_INVALID_CHARS
Definition: unicode.h:41
#define INT_MAX
Definition: intsafe.h:150
int __cdecl _mblen_l(char const *const string, size_t const max_count, _locale_t const locale)
Definition: mblen.cpp:105
static int __cdecl _mblen_internal(char const *const string, size_t const max_count, __crt_cached_ptd_host &ptd)
Definition: mblen.cpp:19
int __cdecl mblen(char const *const string, size_t const max_count)
Definition: mblen.cpp:117
size_t __cdecl __mbrtowc_utf8(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps, __crt_cached_ptd_host &ptd)
#define L(x)
Definition: ntvdm.h:50
#define CP_UTF8
Definition: nls.h:20
Definition: ps.c:97
#define MB_PRECOMPOSED
Definition: winnls.h:283