1 /*-------------------------------------------------------------------------
2 mbstoc16s.c - convert a multibyte string to a wide character string
4 Copyright (C) 2018, Philipp Klaus Krause, krauseph@informatik.uni-freiburg.de
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
35 #ifndef __STDC_UTF_16__
36 #error Encoding for char16_t strings must be UTF-16
38 #ifndef __STDC_ISO_10646__
39 #error Encoding for wchar_t strings must be UCS-4
42 // This implementation assumes that mbtowc() does not keep internal state
43 // and that mbtowc() does not read beyond a terminating 0.
44 size_t __mbstoc16s(char16_t
*restrict c16s
, const char *restrict s
, size_t n
)
53 l
= mbtowc(&codepoint
, s
, MB_LEN_MAX
);
64 if (codepoint
<= 0xffff) // Basic multilingual plane
77 codepoint
-= 0x100000;
78 *c16s
++ = ((codepoint
>> 10) & 0x3ff) + 0xd800;
79 *c16s
++ = (codepoint
& 0x3ff) + 0xdc00;