This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor bytes_to_utf16() into a more generic routine that also handles UTF-8
authorNicholas Clark <nick@ccl4.org>
Fri, 9 Oct 2009 08:48:57 +0000 (10:48 +0200)
committerNicholas Clark <nick@ccl4.org>
Fri, 9 Oct 2009 08:51:12 +0000 (10:51 +0200)
Remove the direct code for testing UTF-8, calling bytes_to_utf() from a loop for
all 3 tested encodings.

t/comp/require.t

index 06a3421..a0170f0 100644 (file)
@@ -258,18 +258,25 @@ EOT
 
 if ($Is_EBCDIC || $Is_UTF8) { exit; }
 
-my $utf8 = pack "C0U", 0xFEFF;
+my %templates = (
+                utf8 => 'C0U',
+                utf16be => 'n',
+                utf16le => 'v',
+               );
+
+sub bytes_to_utf {
+    my ($enc, $content, $do_bom) = @_;
+    my $template = $templates{$enc};
+    die "Unsupported encoding $enc" unless $template;
+    return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content;
+}
 
-$i++; do_require(qq(${utf8}print "ok $i\n"; 1;\n));
+foreach (sort keys %templates) {
+    print "# $_\n";
 
-sub bytes_to_utf16 {
-    my $utf16 = pack("$_[0]*", unpack("C*", $_[1]));
-    return @_ == 3 && $_[2] ? pack("$_[0]", 0xFEFF) . $utf16 : $utf16;
+    $i++; do_require(bytes_to_utf($_, qq(print "ok $i\\n"; 1;\n), 1));
 }
 
-$i++; do_require(bytes_to_utf16('n', qq(print "ok $i\\n"; 1;\n), 1)); # BE
-$i++; do_require(bytes_to_utf16('v', qq(print "ok $i\\n"; 1;\n), 1)); # LE
-
 END {
     foreach my $file (@fjles_to_delete) {
        1 while unlink $file;