mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-12 22:30:12 +00:00
manual: Move mbstouwcs to an example C file
(cherry picked from commit 0f33925269
)
This commit is contained in:
parent
c01dc8b1ff
commit
d6df404c01
@ -1,3 +1,8 @@
|
||||
2018-04-05 Florian Weimer <fweimer@redhat.com>
|
||||
|
||||
* manual/examples/mbstouwcs.c: New file.
|
||||
* manual/charset.texi (Converting a Character): Include it.
|
||||
|
||||
2018-04-03 H.J. Lu <hongjiu.lu@intel.com>
|
||||
|
||||
[BZ #22947]
|
||||
|
@ -686,28 +686,7 @@ converting all lowercase characters into uppercase could look like this
|
||||
checking, and sometimes leaks memory):
|
||||
|
||||
@smallexample
|
||||
wchar_t *
|
||||
mbstouwcs (const char *s)
|
||||
@{
|
||||
size_t len = strlen (s);
|
||||
wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
|
||||
wchar_t *wcp = result;
|
||||
wchar_t tmp[1];
|
||||
mbstate_t state;
|
||||
size_t nbytes;
|
||||
|
||||
memset (&state, '\0', sizeof (state));
|
||||
while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0)
|
||||
@{
|
||||
if (nbytes >= (size_t) -2)
|
||||
/* Invalid input string. */
|
||||
return NULL;
|
||||
*wcp++ = towupper (tmp[0]);
|
||||
len -= nbytes;
|
||||
s += nbytes;
|
||||
@}
|
||||
return result;
|
||||
@}
|
||||
@include mbstouwcs.c.texi
|
||||
@end smallexample
|
||||
|
||||
The use of @code{mbrtowc} should be clear. A single wide character is
|
||||
|
28
manual/examples/mbstouwcs.c
Normal file
28
manual/examples/mbstouwcs.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/* Do not include the above headers in the example.
|
||||
*/
|
||||
wchar_t *
|
||||
mbstouwcs (const char *s)
|
||||
{
|
||||
size_t len = strlen (s);
|
||||
wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
|
||||
wchar_t *wcp = result;
|
||||
wchar_t tmp[1];
|
||||
mbstate_t state;
|
||||
size_t nbytes;
|
||||
|
||||
memset (&state, '\0', sizeof (state));
|
||||
while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0)
|
||||
{
|
||||
if (nbytes >= (size_t) -2)
|
||||
/* Invalid input string. */
|
||||
return NULL;
|
||||
*wcp++ = towupper (tmp[0]);
|
||||
len -= nbytes;
|
||||
s += nbytes;
|
||||
}
|
||||
return result;
|
||||
}
|
Loading…
Reference in New Issue
Block a user