1 /* Convert string to wide string.
2 Copyright (C) 2008 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 extern mbstate_t _gl_mbsrtowcs_state;
33 mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
36 ps = &_gl_mbsrtowcs_state;
38 const char *src = *srcp;
42 wchar_t *destptr = dest;
44 for (; len > 0; destptr++, len--)
49 /* An optimized variant of
50 src_avail = strnlen1 (src, MB_LEN_MAX); */
53 else if (src[1] == '\0')
55 else if (src[2] == '\0')
57 else if (MB_LEN_MAX <= 4 || src[3] == '\0')
60 src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
62 /* Parse the next multibyte character. */
63 ret = mbrtowc (destptr, src, src_avail, ps);
65 if (ret == (size_t)(-2))
66 /* Encountered a multibyte character that extends past a '\0' byte
67 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
70 if (ret == (size_t)(-1))
75 /* Here mbsinit (ps). */
82 return destptr - dest;
86 /* Ignore dest and len, don't store *srcp at the end, and
88 mbstate_t state = *ps;
89 size_t totalcount = 0;
96 /* An optimized variant of
97 src_avail = strnlen1 (src, MB_LEN_MAX); */
100 else if (src[1] == '\0')
102 else if (src[2] == '\0')
104 else if (MB_LEN_MAX <= 4 || src[3] == '\0')
107 src_avail = 4 + strnlen1 (src + 4, MB_LEN_MAX - 4);
109 /* Parse the next multibyte character. */
110 ret = mbrtowc (NULL, src, src_avail, &state);
112 if (ret == (size_t)(-2))
113 /* Encountered a multibyte character that extends past a '\0' byte
114 or that is longer than MB_LEN_MAX bytes. Cannot happen. */
117 if (ret == (size_t)(-1))
121 /* Here mbsinit (&state). */