1 /* size_t mbrtoc16( char16_t *, const char *, size_t, mbstate_t * )
\r
3 This file is part of the Public Domain C Library (PDCLib).
\r
4 Permission is granted to use, modify, and / or redistribute at will.
\r
12 #include "_PDCLIB_encoding.h"
\r
13 #include "_PDCLIB_locale.h"
\r
16 char16_t *restrict pc16,
\r
17 const char *restrict s,
\r
19 mbstate_t *restrict ps,
\r
26 if(!l->_Codec->__mbstoc16s) {
\r
27 // No UTF-16 support in codec. Must synthesize on top of UCS-4 support.
\r
29 if(ps->_Surrogate) {
\r
30 // If a pending surrogate is stored in the state
\r
31 *pc16 = ps->_Surrogate;
\r
37 size_t res = mbrtoc32_l(&c32, s, n, ps, l);
\r
38 if(res != (size_t) -1) {
\r
39 // Conversion was successful. Check for surrogates
\r
44 // Supplementary char
\r
45 *pc16 = 0xD800 | (c32 >> 10);
\r
46 ps->_Surrogate = 0xDC00 | (c32 & 0x3FF);
\r
50 } else if(l->_Codec->__mbstoc16s(&pc16, &dstlen, &s, &nr, ps)) {
\r
51 // Successful conversion
\r
53 // A character was output
\r
55 // The output character resulted entirely from stored state
\r
57 } else if(pc16[-1] == 0) {
\r
58 // Was null character
\r
61 // Count of processed characters
\r
65 assert(nr == 0 && "Must have processed whole input");
\r
69 // Failed conversion
\r
76 char16_t *restrict pc16,
\r
77 const char *restrict s,
\r
79 mbstate_t *restrict ps
\r
82 return mbrtoc16_l(pc16, s, n, ps, _PDCLIB_threadlocale());
\r
88 #include "_PDCLIB_test.h"
\r
92 TESTCASE( NO_TESTDRIVER );
\r
93 return TEST_RESULTS;
\r