This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
POSIX.xs: Prefer mbrtowc() over mbtowc()
authorKarl Williamson <khw@cpan.org>
Thu, 11 Jan 2018 05:35:12 +0000 (22:35 -0700)
committerKarl Williamson <khw@cpan.org>
Wed, 31 Jan 2018 13:33:03 +0000 (06:33 -0700)
mbrtowc is reentrant, so use it on threaded perls if available when
POSIX::mbtowc() is called.

ext/POSIX/POSIX.xs

index 07864ed..7d481c4 100644 (file)
@@ -3285,6 +3285,21 @@ mbtowc(pwc, s, n)
        wchar_t *       pwc
        char *          s
        size_t          n
+    PREINIT:
+#if defined(USE_ITHREADS) && defined(HAS_MBRTOWC)
+        mbstate_t ps;
+#endif
+    CODE:
+#if defined(USE_ITHREADS) && defined(HAS_MBRTOWC)
+        memset(&ps, 0, sizeof(ps));;
+        PERL_UNUSED_RESULT(mbrtowc(pwc, NULL, 0, &ps));/* Reset any shift state */
+        errno = 0;
+        RETVAL = mbrtowc(pwc, s, n, &ps);   /* Prefer reentrant version */
+#else
+        RETVAL = mbtowc(pwc, s, n);
+#endif
+    OUTPUT:
+        RETVAL
 
 int
 wcstombs(s, pwcs, n)