4 mbstate_t *restrict ps);
\r
6 This file is part of the Public Domain C Library (PDCLib).
\r
7 Permission is granted to use, modify, and / or redistribute at will.
\r
16 #include <_PDCLIB_encoding.h>
\r
17 #include <_PDCLIB_locale.h>
\r
22 mbstate_t *restrict ps,
\r
26 const char16_t *restrict psrc = &c16;
\r
27 char buf[s ? 0 : MB_CUR_MAX];
\r
30 if(!l->_Codec->__c16stombs) {
\r
31 // Codec doesn't support direct conversion - translate via UCS-4
\r
32 if(ps->_Surrogate == 0) {
\r
33 // No pending surrogate
\r
34 if((c16 & 0xF800) == 0xD800) {
\r
36 if((c16 & 0x0400) == 0) {
\r
37 // 0xD800 -> 0xDBFF leading surrogate
\r
38 ps->_Surrogate = c16;
\r
41 // Return 0 - we haven't output anything yet
\r
43 /* STD: ISO/IEC 9899:2011 is very implcifit about this being
\r
44 * the correct return value. N1040, from which the
\r
45 * function was adopted, is explicit about 0 being a
\r
50 // 0xDC00 -> 0xDFFF trailing surrogate
\r
55 // BMP range - UTF16 == UCS-4, pass through to c32rtomb_l
\r
56 return c32rtomb_l(s, c16, ps, l);
\r
59 // We have a stored surrogate
\r
60 if((c16 & 0xFC00) == 0xDC00) {
\r
61 // Trailing surrogate
\r
62 char32_t c32 = (ps->_Surrogate & 0x3FF) << 10 | (c16 & 0x3FF);
\r
64 return c32rtomb_l(s, c32, ps, l);
\r
66 // Not a trailing surrogate - encoding error
\r
73 // Codec supports direct conversion
\r
75 size_t dstsz = MB_CUR_MAX;
\r
76 size_t dstrem = dstsz;
\r
78 if(l->_Codec->__c16stombs(&s, &dstrem, &psrc, &srcsz, ps)) {
\r
79 // Successful conversion
\r
80 return dstsz - dstrem;
\r
91 mbstate_t *restrict ps
\r
94 return c16rtomb_l(s, c16, ps, _PDCLIB_threadlocale());
\r
100 #include <_PDCLIB_test.h>
\r
104 TESTCASE( NO_TESTDRIVER );
\r
105 return TEST_RESULTS;
\r