Index: libcxx/include/__config =================================================================== --- libcxx/include/__config +++ libcxx/include/__config @@ -315,6 +315,11 @@ # endif #endif // __sun__ +#if defined(_AIX) && !defined(__64BIT__) + // The size of wchar is 2 byte on 32-bit mode on AIX. +# define _LIBCPP_SHORT_WCHAR 1 +#endif + #if defined(__OpenBSD__) || defined(__CloudABI__) // Certain architectures provide arc4random(). Prefer using // arc4random() over /dev/{u,}random to make it possible to obtain Index: libcxx/src/locale.cpp =================================================================== --- libcxx/src/locale.cpp +++ libcxx/src/locale.cpp @@ -3517,15 +3517,25 @@ { const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); +#if defined(_LIBCPP_SHORT_WCHAR) + return utf8_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#else return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#endif } int __codecvt_utf8::do_max_length() const noexcept { +#if defined(_LIBCPP_SHORT_WCHAR) + if (_Mode_ & consume_header) + return 6; + return 3; +#else if (_Mode_ & consume_header) return 7; return 4; +#endif } // __codecvt_utf8 @@ -3685,14 +3695,25 @@ const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const { +#if defined(_LIBCPP_SHORT_WCHAR) + const uint16_t* _frm = reinterpret_cast(frm); + const uint16_t* _frm_end = reinterpret_cast(frm_end); + const uint16_t* _frm_nxt = _frm; +#else const uint32_t* _frm = reinterpret_cast(frm); const uint32_t* _frm_end = reinterpret_cast(frm_end); const uint32_t* _frm_nxt = _frm; +#endif uint8_t* _to = reinterpret_cast(to); uint8_t* _to_end = reinterpret_cast(to_end); uint8_t* _to_nxt = _to; +#if defined(_LIBCPP_SHORT_WCHAR) + result r = ucs2_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, + _Maxcode_, _Mode_); +#else result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); +#endif frm_nxt = frm + (_frm_nxt - _frm); to_nxt = to + (_to_nxt - _to); return r; @@ -3706,11 +3727,19 @@ const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); const uint8_t* _frm_nxt = _frm; +#if defined(_LIBCPP_SHORT_WCHAR) + uint16_t* _to = reinterpret_cast(to); + uint16_t* _to_end = reinterpret_cast(to_end); + uint16_t* _to_nxt = _to; + result r = utf16be_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, + _Maxcode_, _Mode_); +#else uint32_t* _to = reinterpret_cast(to); uint32_t* _to_end = reinterpret_cast(to_end); uint32_t* _to_nxt = _to; result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); +#endif frm_nxt = frm + (_frm_nxt - _frm); to_nxt = to + (_to_nxt - _to); return r; @@ -3742,15 +3771,25 @@ { const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); +#if defined(_LIBCPP_SHORT_WCHAR) + return utf16be_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#else return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#endif } int __codecvt_utf16::do_max_length() const noexcept { +#if defined(_LIBCPP_SHORT_WCHAR) + if (_Mode_ & consume_header) + return 4; + return 2; +#else if (_Mode_ & consume_header) return 6; return 4; +#endif } // __codecvt_utf16 @@ -3760,14 +3799,25 @@ const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const { +#if defined(_LIBCPP_SHORT_WCHAR) + const uint16_t* _frm = reinterpret_cast(frm); + const uint16_t* _frm_end = reinterpret_cast(frm_end); + const uint16_t* _frm_nxt = _frm; +#else const uint32_t* _frm = reinterpret_cast(frm); const uint32_t* _frm_end = reinterpret_cast(frm_end); const uint32_t* _frm_nxt = _frm; +#endif uint8_t* _to = reinterpret_cast(to); uint8_t* _to_end = reinterpret_cast(to_end); uint8_t* _to_nxt = _to; +#if defined(_LIBCPP_SHORT_WCHAR) + result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, + _Maxcode_, _Mode_); +#else result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); +#endif frm_nxt = frm + (_frm_nxt - _frm); to_nxt = to + (_to_nxt - _to); return r; @@ -3781,11 +3831,19 @@ const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); const uint8_t* _frm_nxt = _frm; +#if defined(_LIBCPP_SHORT_WCHAR) + uint16_t* _to = reinterpret_cast(to); + uint16_t* _to_end = reinterpret_cast(to_end); + uint16_t* _to_nxt = _to; + result r = utf16le_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, + _Maxcode_, _Mode_); +#else uint32_t* _to = reinterpret_cast(to); uint32_t* _to_end = reinterpret_cast(to_end); uint32_t* _to_nxt = _to; result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); +#endif frm_nxt = frm + (_frm_nxt - _frm); to_nxt = to + (_to_nxt - _to); return r; @@ -3817,15 +3875,25 @@ { const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); +#if defined(_LIBCPP_SHORT_WCHAR) + return utf16le_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#else return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_); +#endif } int __codecvt_utf16::do_max_length() const noexcept { +#if defined(_LIBCPP_SHORT_WCHAR) + if (_Mode_ & consume_header) + return 4; + return 2; +#else if (_Mode_ & consume_header) return 6; return 4; +#endif } // __codecvt_utf16 @@ -4135,9 +4203,15 @@ const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const { +#if defined(_LIBCPP_SHORT_WCHAR) + const uint16_t* _frm = reinterpret_cast(frm); + const uint16_t* _frm_end = reinterpret_cast(frm_end); + const uint16_t* _frm_nxt = _frm; +#else const uint32_t* _frm = reinterpret_cast(frm); const uint32_t* _frm_end = reinterpret_cast(frm_end); const uint32_t* _frm_nxt = _frm; +#endif uint8_t* _to = reinterpret_cast(to); uint8_t* _to_end = reinterpret_cast(to_end); uint8_t* _to_nxt = _to; @@ -4156,9 +4230,15 @@ const uint8_t* _frm = reinterpret_cast(frm); const uint8_t* _frm_end = reinterpret_cast(frm_end); const uint8_t* _frm_nxt = _frm; +#if defined(_LIBCPP_SHORT_WCHAR) + uint16_t* _to = reinterpret_cast(to); + uint16_t* _to_end = reinterpret_cast(to_end); + uint16_t* _to_nxt = _to; +#else uint32_t* _to = reinterpret_cast(to); uint32_t* _to_end = reinterpret_cast(to_end); uint32_t* _to_nxt = _to; +#endif result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt, _Maxcode_, _Mode_); frm_nxt = frm + (_frm_nxt - _frm); Index: libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_length.pass.cpp =================================================================== --- libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_length.pass.cpp +++ libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_length.pass.cpp @@ -24,429 +24,306 @@ #include "test_macros.h" -int main(int, char**) -{ - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 4); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 6); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 4); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 6); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 4); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 6); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 4); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 6); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 2); - - n[0] = char(0x10); - n[1] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x04); - n[1] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[0] = char(0x00); - n[1] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 2); - assert(r == 0); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 0); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)}; - std::mbstate_t m; - int r = c.length(m, n, n+6, 2); - assert(r == 2); - - n[1] = char(0x10); - n[0] = char(0x05); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x04); - n[0] = char(0x53); - r = c.length(m, n, n+2, 2); - assert(r == 2); - - n[1] = char(0x00); - n[0] = char(0x56); - r = c.length(m, n, n+2, 2); - assert(r == 2); - } +template +struct TestHelper; + +template +struct TestHelper { + static void test(); +}; + +template +struct TestHelper { + static void test(); +}; + +template +void TestHelper::test() { + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 0); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 6, 2); + assert(r == 2); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 0); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 6, 2); + assert(r == 2); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } +} + +template +void TestHelper::test() { + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 4); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 0); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)}; + std::mbstate_t m; + int r = c.length(m, n, n + 6, 2); + assert(r == 6); + + n[0] = char(0x10); + n[1] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x04); + n[1] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[0] = char(0x00); + n[1] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 4); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 2); + assert(r == 0); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 0); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)}; + std::mbstate_t m; + int r = c.length(m, n, n + 6, 2); + assert(r == 6); + + n[1] = char(0x10); + n[0] = char(0x05); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x04); + n[0] = char(0x53); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + + n[1] = char(0x00); + n[0] = char(0x56); + r = c.length(m, n, n + 2, 2); + assert(r == 2); + } +} +int main(int, char**) { + TestHelper::test(); + TestHelper::test(); + TestHelper::test(); return 0; } Index: libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_max_length.pass.cpp =================================================================== --- libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_max_length.pass.cpp +++ libcxx/test/std/localization/locale.stdcvt/codecvt_utf16_max_length.pass.cpp @@ -23,44 +23,54 @@ #include "test_macros.h" -int main(int, char**) -{ - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 4); - } - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 6); - } - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 2); - } - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 4); - } - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 4); - } - { - typedef std::codecvt_utf16 C; - C c; - int r = c.max_length(); - assert(r == 6); - } +template +struct TestHelper; +template +struct TestHelper { + static void test(); +}; + +template +struct TestHelper { + static void test(); +}; + +template +void TestHelper::test() { + { + typedef std::codecvt_utf16 C; + C c; + int r = c.max_length(); + assert(r == 2); + } + { + typedef std::codecvt_utf16 C; + C c; + int r = c.max_length(); + assert(r == 4); + } +} + +template +void TestHelper::test() { + { + typedef std::codecvt_utf16 C; + C c; + int r = c.max_length(); + assert(r == 4); + } + { + typedef std::codecvt_utf16 C; + C c; + int r = c.max_length(); + assert(r == 6); + } +} + +int main(int, char**) { + TestHelper::test(); + TestHelper::test(); + TestHelper::test(); return 0; } Index: libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_length.pass.cpp =================================================================== --- libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_length.pass.cpp +++ libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_length.pass.cpp @@ -24,224 +24,174 @@ #include "test_macros.h" -int main(int, char**) -{ - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 4); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 0); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 0); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+7, 1); - assert(r == 7); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xEF); - n[1] = char(0xBB); - n[2] = char(0xBF); - n[3] = char(0xD1); - n[4] = char(0x93); - r = c.length(m, n, n+5, 3); - assert(r == 5); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 4); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 0); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 0); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+7, 1); - assert(r == 7); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xEF); - n[1] = char(0xBB); - n[2] = char(0xBF); - n[3] = char(0xD1); - n[4] = char(0x93); - r = c.length(m, n, n+5, 3); - assert(r == 5); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 0); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+4, 1); - assert(r == 0); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 0); - - n[0] = char(0xD1); - n[1] = char(0x93); - r = c.length(m, n, n+2, 3); - assert(r == 2); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } - { - typedef std::codecvt_utf8 C; - C c; - char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)}; - std::mbstate_t m; - int r = c.length(m, n, n+7, 1); - assert(r == 3); - - n[0] = char(0xE1); - n[1] = char(0x80); - n[2] = char(0x85); - r = c.length(m, n, n+3, 2); - assert(r == 3); - - n[0] = char(0xEF); - n[1] = char(0xBB); - n[2] = char(0xBF); - n[3] = char(0xD1); - n[4] = char(0x93); - r = c.length(m, n, n+5, 3); - assert(r == 5); - - n[0] = char(0x56); - r = c.length(m, n, n+1, 3); - assert(r == 1); - } +template +struct TestHelper; + +template +struct TestHelper { + static void test(); +}; + +template +struct TestHelper { + static void test(); +}; + +template +void TestHelper::test() { + { + typedef std::codecvt_utf8 C; + C c; + char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 1); + assert(r == 0); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 3); + + n[0] = char(0xD1); + n[1] = char(0x93); + r = c.length(m, n, n + 2, 3); + assert(r == 2); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } + { + typedef std::codecvt_utf8 C; + C c; + char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 1); + assert(r == 0); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 0); + + n[0] = char(0xD1); + n[1] = char(0x93); + r = c.length(m, n, n + 2, 3); + assert(r == 2); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } + { + typedef std::codecvt_utf8 C; + C c; + char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 7, 1); + assert(r == 3); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 3); + + n[0] = char(0xEF); + n[1] = char(0xBB); + n[2] = char(0xBF); + n[3] = char(0xD1); + n[4] = char(0x93); + r = c.length(m, n, n + 5, 3); + assert(r == 5); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } +} + +template +void TestHelper::test() { + { + typedef std::codecvt_utf8 C; + C c; + char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 1); + assert(r == 4); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 3); + + n[0] = char(0xD1); + n[1] = char(0x93); + r = c.length(m, n, n + 2, 3); + assert(r == 2); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } + { + typedef std::codecvt_utf8 C; + C c; + char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 4, 1); + assert(r == 0); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 0); + + n[0] = char(0xD1); + n[1] = char(0x93); + r = c.length(m, n, n + 2, 3); + assert(r == 2); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } + { + typedef std::codecvt_utf8 C; + C c; + char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)}; + std::mbstate_t m; + int r = c.length(m, n, n + 7, 1); + assert(r == 7); + + n[0] = char(0xE1); + n[1] = char(0x80); + n[2] = char(0x85); + r = c.length(m, n, n + 3, 2); + assert(r == 3); + + n[0] = char(0xEF); + n[1] = char(0xBB); + n[2] = char(0xBF); + n[3] = char(0xD1); + n[4] = char(0x93); + r = c.length(m, n, n + 5, 3); + assert(r == 5); + + n[0] = char(0x56); + r = c.length(m, n, n + 1, 3); + assert(r == 1); + } +} +int main(int, char**) { + TestHelper::test(); + TestHelper::test(); + TestHelper::test(); return 0; } Index: libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_max_length.pass.cpp =================================================================== --- libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_max_length.pass.cpp +++ libcxx/test/std/localization/locale.stdcvt/codecvt_utf8_max_length.pass.cpp @@ -23,44 +23,54 @@ #include "test_macros.h" -int main(int, char**) -{ - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 4); - } - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 7); - } - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 3); - } - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 6); - } - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 4); - } - { - typedef std::codecvt_utf8 C; - C c; - int r = c.max_length(); - assert(r == 7); - } +template +struct TestHelper; +template +struct TestHelper { + static void test(); +}; + +template +struct TestHelper { + static void test(); +}; + +template +void TestHelper::test() { + { + typedef std::codecvt_utf8 C; + C c; + int r = c.max_length(); + assert(r == 3); + } + { + typedef std::codecvt_utf8 C; + C c; + int r = c.max_length(); + assert(r == 6); + } +} + +template +void TestHelper::test() { + { + typedef std::codecvt_utf8 C; + C c; + int r = c.max_length(); + assert(r == 4); + } + { + typedef std::codecvt_utf8 C; + C c; + int r = c.max_length(); + assert(r == 7); + } +} + +int main(int, char**) { + TestHelper::test(); + TestHelper::test(); + TestHelper::test(); return 0; }