This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
POSIX::localeconv() Use new fcn; avoid recalcs
authorKarl Williamson <khw@cpan.org>
Wed, 17 Jan 2018 19:40:40 +0000 (12:40 -0700)
committerKarl Williamson <khw@cpan.org>
Wed, 31 Jan 2018 05:58:15 +0000 (22:58 -0700)
This calls strlen() once, instead of passing 0 to the the subsidiary
functions which causes them to call it each time.  It also uses the new
function is_utf8_non_invariant_string() instead of doing here what that
function does.

ext/POSIX/POSIX.xs

index 76fc9a3..2ecf2d9 100644 (file)
@@ -2171,19 +2171,19 @@ localeconv()
                const char *value = *((const char **)(ptr + strings->offset));
 
                if (value && *value) {
+                    const STRLEN value_len = strlen(value);
+
+                    /* We mark it as UTF-8 if a utf8 locale and is valid and
+                     * variant under UTF-8 */
+                    const bool is_utf8 = is_utf8_locale
+                                     &&  is_utf8_non_invariant_string(
+                                                                (U8*) value,
+                                                                value_len);
                    (void) hv_store(RETVAL,
-                        strings->name,
-                        strlen(strings->name),
-                        newSVpvn_utf8(
-                                value,
-                                strlen(value),
-
-                                /* We mark it as UTF-8 if a utf8 locale and is
-                                 * valid and variant under UTF-8 */
-                                     is_utf8_locale
-                                && ! is_utf8_invariant_string((U8 *) value, 0)
-                                &&   is_utf8_string((U8 *) value, 0)),
-                    0);
+                                    strings->name,
+                                    strlen(strings->name),
+                                    newSVpvn_utf8(value, value_len, is_utf8),
+                                    0);
             }
                 strings++;
            }