2 * This code was written by Rich Felker in 2010; no copyright is claimed.
3 * This code is in the public domain. Attribution is appreciated but
12 size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
15 if ((unsigned)wc < 0x80) {
18 } else if (MB_CUR_MAX == 1) {
19 if (!IS_CODEUNIT(wc)) {
25 } else if ((unsigned)wc < 0x800) {
26 *s++ = 0xc0 | (wc>>6);
27 *s = 0x80 | (wc&0x3f);
29 } else if ((unsigned)wc < 0xd800 || (unsigned)wc-0xe000 < 0x2000) {
30 *s++ = 0xe0 | (wc>>12);
31 *s++ = 0x80 | ((wc>>6)&0x3f);
32 *s = 0x80 | (wc&0x3f);
34 } else if ((unsigned)wc-0x10000 < 0x100000) {
35 *s++ = 0xf0 | (wc>>18);
36 *s++ = 0x80 | ((wc>>12)&0x3f);
37 *s++ = 0x80 | ((wc>>6)&0x3f);
38 *s = 0x80 | (wc&0x3f);