| 139 | static |
| 140 | #endif |
| 141 | string utf16_to_8(const utf16_t *s) |
| 142 | { |
| 143 | string d; |
| 144 | char32_t c; |
| 145 | |
| 146 | while (*s) |
| 147 | { |
| 148 | if (*s >= 0xD800 && *s <= 0xDBFF) |
| 149 | if (s[1] >= 0xDC00 && s[1] <= 0xDFFF) |
| 150 | { |
| 151 | c = (((char32_t)s[0]) << 10) + s[1] - 0x35fdc00; |
| 152 | s++; |
| 153 | } |
| 154 | else |
| 155 | c = 0xFFFD; // leading surrogate without its tail |
| 156 | else if (*s >= 0xDC00 && *s <= 0xDFFF) |
| 157 | c = 0xFFFD; // unpaired trailing surrogate |
| 158 | else |
| 159 | c = *s; |
| 160 | s++; |
| 161 | |
| 162 | char buf[4]; |
| 163 | int l = wctoutf8(buf, c); |
| 164 | for (int i = 0; i < l; i++) |
| 165 | d.push_back(buf[i]); |
| 166 | } |
| 167 | |
| 168 | return d; |
| 169 | } |
| 170 | |
| 171 | string utf8_to_mb(const char *s) |
| 172 | { |