]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
manual: Move mbstouwcs to an example C file
authorFlorian Weimer <fweimer@redhat.com>
Thu, 5 Apr 2018 10:50:58 +0000 (12:50 +0200)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 5 Apr 2018 10:50:58 +0000 (12:50 +0200)
ChangeLog
manual/charset.texi
manual/examples/mbstouwcs.c [new file with mode: 0644]

index af6e325aaa8bcb0f671545d2a46ead27489172ad..9b30d0ce3b873ebf01e44836edf395f8f0b7a12e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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-05  Samuel Thibault  <samuel.thibault@ens-lyon.org>
 
        * include/dirent.h (dirfd): Add hidden proto.
index 1867ace48589d2d023589e77515958104445cdde..6831ebec273723fd0b354456975b1a04b9c0149d 100644 (file)
@@ -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
diff --git a/manual/examples/mbstouwcs.c b/manual/examples/mbstouwcs.c
new file mode 100644 (file)
index 0000000..5d223da
--- /dev/null
@@ -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;
+}