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

ext/POSIX/POSIX.xs

index 2ecf2d9..07864ed 100644 (file)
@@ -3252,10 +3252,27 @@ write(fd, buffer, nbytes)
 void
 abort()
 
+#ifdef I_WCHAR
+#  include <wchar.h>
+#endif
+
 int
 mblen(s, n)
        char *          s
        size_t          n
+    PREINIT:
+#if defined(USE_ITHREADS) && defined(HAS_MBRLEN)
+        mbstate_t ps;
+#endif
+    CODE:
+#if defined(USE_ITHREADS) && defined(HAS_MBRLEN)
+        PERL_UNUSED_RESULT(mbrlen(NULL, 0, &ps));   /* Initialize state */
+        RETVAL = mbrlen(s, n, &ps); /* Prefer reentrant version */
+#else
+        RETVAL = mblen(s, n);
+#endif
+    OUTPUT:
+        RETVAL
 
 size_t
 mbstowcs(s, pwcs, n)