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
14 size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st)
17 if ((unsigned)wc < 0x80) {
20 } else if ((unsigned)wc < 0x800) {
21 *s++ = 0xc0 | (wc>>6);
22 *s = 0x80 | (wc&0x3f);
24 } else if ((unsigned)wc < 0xd800 || (unsigned)wc-0xe000 < 0x2000) {
25 *s++ = 0xe0 | (wc>>12);
26 *s++ = 0x80 | ((wc>>6)&0x3f);
27 *s = 0x80 | (wc&0x3f);
29 } else if ((unsigned)wc-0x10000 < 0x100000) {
30 *s++ = 0xf0 | (wc>>18);
31 *s++ = 0x80 | ((wc>>12)&0x3f);
32 *s++ = 0x80 | ((wc>>6)&0x3f);
33 *s = 0x80 | (wc&0x3f);