1 /* Convert string to wide string.
2 Copyright (C) 2008-2022 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 FUNC (DCHAR_T
*dest
, const char **srcp
, size_t len
, mbstate_t *ps
)
24 const char *src
= *srcp
;
28 DCHAR_T
*destptr
= dest
;
30 for (; len
> 0; destptr
++, len
--)
35 /* An optimized variant of
36 src_avail = strnlen1 (src, MB_LEN_MAX); */
39 else if (src
[1] == '\0')
41 else if (src
[2] == '\0')
43 else if (MB_LEN_MAX
<= 4 || src
[3] == '\0')
46 src_avail
= 4 + strnlen1 (src
+ 4, MB_LEN_MAX
- 4);
48 /* Parse the next multibyte character. */
49 ret
= MBRTOWC (destptr
, src
, src_avail
, ps
);
51 if (ret
== (size_t)(-2))
52 /* Encountered a multibyte character that extends past a '\0' byte
53 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
56 if (ret
== (size_t)(-1))
61 /* Here mbsinit (ps). */
68 return destptr
- dest
;
72 /* Ignore dest and len, don't store *srcp at the end, and
74 mbstate_t state
= *ps
;
75 size_t totalcount
= 0;
82 /* An optimized variant of
83 src_avail = strnlen1 (src, MB_LEN_MAX); */
86 else if (src
[1] == '\0')
88 else if (src
[2] == '\0')
90 else if (MB_LEN_MAX
<= 4 || src
[3] == '\0')
93 src_avail
= 4 + strnlen1 (src
+ 4, MB_LEN_MAX
- 4);
95 /* Parse the next multibyte character. */
96 ret
= MBRTOWC (NULL
, src
, src_avail
, &state
);
98 if (ret
== (size_t)(-2))
99 /* Encountered a multibyte character that extends past a '\0' byte
100 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
103 if (ret
== (size_t)(-1))
107 /* Here mbsinit (&state). */