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
28 if(!l->_Codec->__c16stombs) {
\r
29 // Codec doesn't support direct conversion - translate via UCS-4
\r
30 if(ps->_Surrogate == 0) {
\r
31 // No pending surrogate
\r
32 if((c16 & 0xF800) == 0xD800) {
\r
34 if((c16 & 0x0400) == 0) {
\r
35 // 0xD800 -> 0xDBFF leading surrogate
\r
36 ps->_Surrogate = c16;
\r
39 // Return 0 - we haven't output anything yet
\r
41 /* STD: ISO/IEC 9899:2011 is very implcifit about this being
\r
42 * the correct return value. N1040, from which the
\r
43 * function was adopted, is explicit about 0 being a
\r
48 // 0xDC00 -> 0xDFFF trailing surrogate
\r
53 // BMP range - UTF16 == UCS-4, pass through to c32rtomb_l
\r
54 return c32rtomb_l(s, c16, ps, l);
\r
57 // We have a stored surrogate
\r
58 if((c16 & 0xFC00) == 0xDC00) {
\r
59 // Trailing surrogate
\r
60 char32_t c32 = (ps->_Surrogate & 0x3FF) << 10 | (c16 & 0x3FF);
\r
61 return c32rtomb_l(s, c32, ps, l);
\r
63 // Not a trailing surrogate - encoding error
\r
70 // Codec supports direct conversion
\r
72 size_t dstsz = MB_CUR_MAX;
\r
73 size_t dstrem = dstsz;
\r
75 if(l->_Codec->__c16stombs(&s, &dstrem, &psrc, &srcsz, ps)) {
\r
76 // Successful conversion
\r
77 return dstsz - dstrem;
\r
88 mbstate_t *restrict ps
\r
91 return c16rtomb_l(s, c16, ps, _PDCLIB_threadlocale());
\r
97 #include <_PDCLIB_test.h>
\r
101 TESTCASE( NO_TESTDRIVER );
\r
102 return TEST_RESULTS;
\r