5 require './test.pl'; require './charset_tools.pl';
8 skip_all_without_perlio();
10 no utf8; # needed for use utf8 not griping about the raw octets
17 my $a_file = tempfile();
19 open(F,"+>:utf8",$a_file);
20 print F chr(0x100).'£';
21 cmp_ok( tell(F), '==', 4, tell(F) );
23 cmp_ok( tell(F), '>=', 5, tell(F) );
25 is( getc(F), chr(0x100) );
31 # Byte representation of these characters
32 my $U_100 = byte_utf8a_to_utf8n("\xc4\x80");
33 my $POUND_SIGN = byte_utf8a_to_utf8n("\xc2\xa3");
35 my $chr = substr($U_100, 0, 1);
37 $chr = substr($U_100, 1, 1);
39 $chr = substr($POUND_SIGN, 0, 1);
41 $chr = substr($POUND_SIGN, 1, 1);
46 is( scalar(<F>), "\x{100}£\n" );
49 $count = read(F,$buf,2,1);
50 cmp_ok( $count, '==', 2 );
51 is( $buf, "\x{200}\x{100}£" );
55 $a = chr(300); # This *is* UTF-encoded
56 $b = chr(130); # This is not.
58 open F, ">:utf8", $a_file or die $!;
62 open F, "<:utf8", $a_file or die $!;
67 open F, $a_file or die $!; # Not UTF
71 $chr = byte_utf8a_to_utf8n(chr(196).chr(172));
75 open F, ">:utf8", $a_file or die $!;
76 binmode(F); # we write a "\n" and then tell() - avoid CRLF issues.
77 binmode(F,":utf8"); # turn UTF-8-ness back on
81 { use bytes; $y = length($a);}
82 cmp_ok( $x, '==', $y );
85 { # Check byte length of $b
86 use bytes; my $y = length($b);
87 cmp_ok( $y, '==', 1 );
90 print F $b,"\n"; # Don't upgrade $b
92 { # Check byte length of $b
93 use bytes; my $y = length($b);
94 cmp_ok( $y, '==', 1 );
99 { use bytes; if ($::IS_EBCDIC){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
100 cmp_ok( $x, '==', $y );
105 open F, $a_file or die $!; # Not UTF
106 binmode(F, ":bytes");
109 $chr = v196.172.194.130;
110 if ($::IS_EBCDIC) { $chr = v141.83.130; } # EBCDIC
111 is( $x, $chr, sprintf('(%vd)', $x) );
113 open F, "<:utf8", $a_file or die $!;
117 is( $x, chr(300).chr(130), sprintf('(%vd)', $x) );
119 open F, ">", $a_file or die $!;
120 binmode(F, ":bytes:");
122 # Now let's make it suffer.
126 local $SIG{__WARN__} = sub { $w = $_[0] };
129 like($w, qr/Wide character in print/i );
133 # Hm. Time to get more evil.
134 open F, ">:utf8", $a_file or die $!;
136 binmode(F, ":bytes");
137 print F chr(130)."\n";
140 open F, "<", $a_file or die $!;
141 binmode(F, ":bytes");
144 if ($::IS_EBCDIC) { $chr = v141.83.130; } # EBCDIC
148 open F, ">:utf8", $a_file or die $!;
151 open F, ">>", $a_file or die $!;
152 binmode(F, ":bytes");
153 print F chr(130)."\n";
156 open F, "<", $a_file or die $!;
157 binmode(F, ":bytes");
160 skip("Defaulting to UTF-8 output means that we can't generate a mangled file")
165 # Now we have a deformed file.
169 skip("EBCDIC The file isn't deformed in UTF-EBCDIC", 2);
172 open F, "<:utf8", $a_file or die $!;
174 local $SIG{__WARN__} = sub { push @warnings, $_[0]; };
175 eval { sprintf "%vd\n", $x };
176 is (scalar @warnings, 1);
177 like ($warnings[0], qr/Malformed UTF-8 character: \\x82 \(unexpected continuation byte 0x82, with no preceding start byte/);
184 open F, ">:utf8", $a_file;
185 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
186 unshift @a, chr(0); # ... and a null byte in front just for fun
192 # read() should work on characters, not bytes
193 open F, "<:utf8", $a_file;
197 unless (($c = read(F, $b, 1) == 1) &&
199 ord($b) == ord($_) &&
200 tell(F) == ($a += bytes::length($b))) {
201 print '# ord($_) == ', ord($_), "\n";
202 print '# ord($b) == ', ord($b), "\n";
203 print '# length($b) == ', length($b), "\n";
204 print '# bytes::length($b) == ', bytes::length($b), "\n";
205 print '# tell(F) == ', tell(F), "\n";
206 print '# $a == ', $a, "\n";
207 print '# $c == ', $c, "\n";
216 # Check that warnings are on on I/O, and that they can be muffled.
218 local $SIG{__WARN__} = sub { $@ = shift };
222 binmode(F, ":bytes");
226 like( $@, qr/Wide character in print/ );
229 open F, ">:utf8", $a_file;
233 isnt( defined $@, !0 );
241 isnt( defined $@, !0 );
250 isnt( defined $@, !0 );
256 binmode(F, ":bytes");
260 like( $@, qr/Wide character in print/ );
264 open F, ">:bytes",$a_file; print F "\xde"; close F;
266 open F, "<:bytes", $a_file;
269 is( $b, chr(0x100).chr(0xde), "21395 '.= <>' utf8 vs. bytes" );
274 open F, ">:utf8",$a_file; print F chr 0x100; close F;
276 open F, "<:utf8", $a_file;
279 is( $b, chr(0xde).chr(0x100), "21395 '.= <>' bytes vs. utf8" );
284 my @a = ( [ 0x007F, "bytes" ],
287 [ 0x0100, "utf8" ] );
291 # print "# @$u - @$v\n";
293 binmode(F, ":" . $u->[1]);
294 print F chr($u->[0]);
298 binmode(F, ":" . $u->[1]);
300 my $s = chr($v->[0]);
301 utf8::upgrade($s) if $v->[1] eq "utf8";
304 is( $s, chr($v->[0]) . chr($u->[0]), 'rcatline utf8' );
313 # [perl #23428] Somethings rotten in unicode semantics
316 no warnings qw(deprecated);
317 syswrite(F, $a = chr(0x100));
319 is( ord($a), 0x100, '23428 syswrite should not downgrade scalar' );
320 like( $a, qr/^\w+/, '23428 syswrite should not downgrade scalar' );
323 # sysread() and syswrite() tested in lib/open.t since Fcntl is used
326 # <FH> on a :utf8 stream should complain immediately with -w
327 # if it finds bad UTF-8 (:encoding(utf8) works this way)
330 local $SIG{__WARN__} = sub { $@ = shift };
333 my ($chrE4, $chrF6) = (chr(0xE4), chr(0xF6));
334 if ($::IS_EBCDIC) # EBCDIC
335 { ($chrE4, $chrF6) = (chr(0x43), chr(0xEC)); }
336 print F "foo", $chrE4, "\n";
337 print F "foo", $chrF6, "\n";
339 open F, "<:utf8", $a_file;
342 my ($chrE4, $chrF6) = ("E4", "F6");
343 if ($::IS_EBCDIC) { ($chrE4, $chrF6) = ("43", "EC"); } # EBCDIC
344 like( $@, qr/utf8 "\\x$chrE4" does not map to Unicode .+ <F> line 1/,
345 "<:utf8 readline must warn about bad utf8");
348 like( $@, qr/utf8 "\\x$chrF6" does not map to Unicode .+ <F> line 2/,
349 "<:utf8 rcatline must warn about bad utf8");
355 open F, ">:utf8", $a_file;
358 print F "\xC0\xC8\xCC\xD2";
362 open F, "<:utf8", $a_file;
365 is($line, "foo\xE4", "readline with \$/ = \\4");
367 is($line, "foo\xE4bar\xFE", "rcatline with \$/ = \\4");
369 is($line, "\xC0\xC8\xCC\xD2", "readline with several encoded characters");
371 is($line, "a\xE4ab", "readline with another boundary condition");
373 is($line, "a\xE4a", "readline with boundary condition");
376 # badly encoded at EOF
377 open F, ">:raw", $a_file;
378 print F "foo\xEF\xAC"; # truncated \x{FB04} small ligature ffl
382 open F, "<:utf8", $a_file;
384 local $SIG{__WARN__} = sub { $@ = shift };
387 like( $@, qr/utf8 "\\xEF" does not map to Unicode .+ <F> chunk 1/,
388 "<:utf8 readline (fixed) must warn about bad utf8");
392 # getc should reset the utf8 flag and not be affected by previous
395 skip "no PerlIO::scalar on miniperl", 2, if is_miniperl();
396 open my $fh, "<:raw", \($buf = chr 255);
397 open my $uh, "<:utf8", \($uuf = $U_100);
398 for([$uh,chr 256], [$fh,chr 255]) {
399 is getc $$_[0], $$_[1],
400 'getc returning non-utf8 after utf8';