11 my $Is_EBCDIC = (ord('i') == 0x89 & ord('J') == 0xd1);
13 $_ = "abcdefghijklmnopqrstuvwxyz";
17 is($_, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'uc');
21 is($_, "abcdefghijklmnopqrstuvwxyz", 'lc');
24 is($_, "aBCDEFGHIJKLMNOPQRSTUVWXYz", 'partial uc');
27 # In EBCDIC 'I' is \xc9 and 'J' is \0xd1, 'i' is \x89 and 'j' is \x91.
28 # Yes, discontinuities. Regardless, the \xca in the below should stay
29 # untouched (and not became \x8a).
36 is($_, "i\xcaj", 'EBCDIC discontinuity');
42 (my $y = 12) =~ tr/1/3/;
43 ($f = 1.5) =~ tr/1/3/;
44 (my $g = 1.5) =~ tr/1/3/;
45 is($x + $y + $f + $g, 71, 'tr cancels IOK and NOK');
49 is y/dam/ve/rd, 'eve', '/r';
50 is $_, 'adam', '/r leaves param alone';
52 is $g =~ y/bury/repl/r, 'perl', '/r with explicit param';
53 is $g, 'ruby', '/r leaves explicit param alone';
54 is "aaa" =~ y\a\b\r, 'bbb', '/r with constant param';
55 ok !eval '$_ !~ y///r', "!~ y///r is forbidden";
56 like $@, qr\^Using !~ with tr///r doesn't make sense\,
57 "!~ y///r error message";
61 local $SIG{__WARN__} = sub { $w = shift; ++$wc };
64 like $w, qr '^Useless use of non-destructive transliteration \(tr///r\)',
65 '/r warns in void context';
66 is $wc, 1, '/r warns just once';
69 # perlbug [ID 20000511.005]
74 is($_, 'Fred', 'harmless if explicitly not updating');
77 # A variant of the above, added in 5.7.2
80 eval '$1 =~ tr/A-Z/A-Z/;';
82 is($_, 'Fred', 'harmless if implicitly not updating');
83 is($@, '', ' no error');
86 # check tr handles UTF8 correctly
87 ($x = 256.65.258) =~ tr/a/b/;
88 is($x, 256.65.258, 'handles UTF8');
93 if (ord("\t") == 9) { # ASCII
100 # EBCDIC variants of the above tests
101 ($x = 256.193.258) =~ tr/a/b/;
107 if (ord("\t") == 9) { # ASCII
116 my $l = chr(300); my $r = chr(400);
118 $x =~ tr/\x{12c}/\x{190}/;
120 'changing UTF8 chars in a UTF8 string, same length');
124 $x =~ tr/\x{12c}/\x{be8}/;
125 is($x, 200.3048.400, ' more bytes');
129 $x =~ tr/\x{64}/\x{190}/;
130 is($x, 400.125.60, 'Putting UT8 chars into a non-UTF8 string');
134 $x =~ tr/\x{190}/\x{64}/;
135 is($x, 100.125.60, 'Removing UTF8 chars from UTF8 string');
139 $y = $x =~ tr/\x{190}/\x{190}/;
140 is($y, 2, 'Counting UTF8 chars in UTF8 string');
142 $x = 60.400.125.60.400;
143 $y = $x =~ tr/\x{3c}/\x{3c}/;
144 is($y, 2, ' non-UTF8 chars in UTF8 string');
146 # 17 - counting UTF8 chars in non-UTF8 string
148 $y = $x =~ tr/\x{190}/\x{190}/;
149 is($y, 0, ' UTF8 chars in non-UTFs string');
152 $_ = "abcdefghijklmnopqrstuvwxyz";
154 like($@, qr/^Ambiguous range in transliteration operator/, 'tr/a-z-9//');
156 # 19-21: Make sure leading and trailing hyphens still work
159 is($_, '..r.rot9', 'hyphens, leading');
163 is($_, '..r.rot9', ' trailing');
167 is($_, '..r.rot9', ' both');
169 $_ = "abcdefghijklmnop";
171 is($_, '.bcd....ijklm.op');
173 $_ = "abcdefghijklmnop";
175 is($_, '...de......lm...');
177 $_ = "abcdefghijklmnop";
179 is($_, '...d.f...j.l...p');
184 like($@, qr/^Invalid range "m-d" in transliteration operator/,
185 'reversed range check');
188 is(eval '$1 =~ tr/abcd//', 3, 'explicit read-only count');
189 is($@, '', ' no error');
192 is(eval '$1 =~ tr/abcd/abcd/', 3, 'implicit read-only count');
193 is($@, '', ' no error');
195 is(eval '"123" =~ tr/12//', 2, 'LHS of non-updating tr');
197 eval '"123" =~ tr/1/2/';
198 like($@, qr|^Can't modify constant item in transliteration \(tr///\)|,
199 'LHS bad on updating tr');
202 # v300 (0x12c) is UTF-8-encoded as 196 172 (0xc4 0xac)
203 # v400 (0x190) is UTF-8-encoded as 198 144 (0xc6 0x90)
205 # Transliterate a byte to a byte, all four ways.
207 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/;
208 is($a, v300.197.172.300.197.172, 'byte2byte transliteration');
210 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{c5}/;
211 is($a, v300.197.172.300.197.172);
213 ($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\xc5/;
214 is($a, v300.197.172.300.197.172);
216 ($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\x{c5}/;
217 is($a, v300.197.172.300.197.172);
220 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/;
221 is($a, v300.301.172.300.301.172, 'byte2wide transliteration');
223 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc3/;
224 is($a, v195.196.172.195.196.172, ' wide2byte');
226 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/;
227 is($a, v301.196.172.301.196.172, ' wide2wide');
230 ($a = v300.196.172.300.196.172) =~ tr/\xc4\x{12c}/\x{12d}\xc3/;
231 is($a, v195.301.172.195.301.172, 'byte2wide & wide2byte');
234 ($a = v300.196.172.300.196.172.400.198.144) =~
235 tr/\xac\xc4\x{12c}\x{190}/\xad\x{12d}\xc5\x{191}/;
236 is($a, v197.301.173.197.301.173.401.198.144, 'all together now!');
239 is((($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/), 2,
240 'transliterate and count');
242 is((($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/), 2);
245 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/c;
246 is($a, v301.196.301.301.196.301, 'translit w/complement');
248 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc5/c;
249 is($a, v300.197.197.300.197.197);
252 ($a = v300.196.172.300.196.172) =~ tr/\xc4//d;
253 is($a, v300.172.300.172, 'translit w/deletion');
255 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}//d;
256 is($a, v196.172.196.172);
259 ($a = v196.196.172.300.300.196.172) =~ tr/\xc4/\xc5/s;
260 is($a, v197.172.300.300.197.172, 'translit w/squeeze');
262 ($a = v196.172.300.300.196.172.172) =~ tr/\x{12c}/\x{12d}/s;
263 is($a, v196.172.301.196.172.172);
266 # Tricky cases (When Simon Cozens Attacks)
267 ($a = v196.172.200) =~ tr/\x{12c}/a/;
268 is(sprintf("%vd", $a), '196.172.200');
270 ($a = v196.172.200) =~ tr/\x{12c}/\x{12c}/;
271 is(sprintf("%vd", $a), '196.172.200');
273 ($a = v196.172.200) =~ tr/\x{12c}//d;
274 is(sprintf("%vd", $a), '196.172.200');
277 # UTF8 range tests from Inaba Hiroto
279 # Not working in EBCDIC as of 12674.
280 ($a = v300.196.172.302.197.172) =~ tr/\x{12c}-\x{130}/\xc0-\xc4/;
281 is($a, v192.196.172.194.197.172, 'UTF range');
283 ($a = v300.196.172.302.197.172) =~ tr/\xc4-\xc8/\x{12c}-\x{130}/;
284 is($a, v300.300.172.302.301.172);
287 # UTF8 range tests from Karsten Sperling (patch #9008 required)
289 ($a = "\x{0100}") =~ tr/\x00-\x{100}/X/;
292 ($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}/X/c;
295 ($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
298 ($a = v256) =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
302 # UTF8 range tests from Inaba Hiroto
304 ($a = "\x{200}") =~ tr/\x00-\x{100}/X/c;
307 ($a = "\x{200}") =~ tr/\x00-\x{100}/X/cs;
311 # Tricky on EBCDIC: while [a-z] [A-Z] must not match the gap characters,
312 # (i-j, r-s, I-J, R-S), [\x89-\x91] [\xc9-\xd1] has to match them,
313 # from Karsten Sperling.
315 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/\x89-\x91/X/;
319 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/\xc9-\xd1/X/;
324 skip "not EBCDIC", 4 unless $Is_EBCDIC;
326 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/i-j/X/;
328 is($a, "X\x8a\x8b\x8c\x8d\x8f\x90X");
330 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/I-J/X/;
332 is($a, "X\xca\xcb\xcc\xcd\xcf\xd0X");
335 ($a = "\x{100}") =~ tr/\x00-\xff/X/c;
336 is(ord($a), ord("X"));
338 ($a = "\x{100}") =~ tr/\x00-\xff/X/cs;
339 is(ord($a), ord("X"));
341 ($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//c;
342 is($a, "\x{100}\x{100}");
344 ($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//cs;
347 $a = "\xfe\xff"; $a =~ tr/\xfe\xff/\x{1ff}\x{1fe}/;
348 is($a, "\x{1ff}\x{1fe}");
352 ($a = "R0_001") =~ tr/R_//d;
356 @a = (1,2); map { y/1/./ for $_ } @a;
359 @a = (1,2); map { y/1/./ for $_.'' } @a;
363 # Additional test for Inaba Hiroto patch (robin@kitsite.com)
364 ($a = "\x{100}\x{102}\x{101}") =~ tr/\x00-\377/XYZ/c;
368 # Used to fail with "Modification of a read-only value attempted"
372 is($_, 'n', 'pp_trans needs to unshare shared hash keys');
373 is($@, '', ' no error');
377 $x = eval '"1213" =~ tr/1/1/';
378 is($x, 2, 'implicit count on constant');
379 is($@, '', ' no error');
383 eval '$foo[-1] =~ tr/N/N/';
384 is( $@, '', 'implicit count outside array bounds, index negative' );
385 is( scalar @foo, 0, " doesn't extend the array");
387 eval '$foo[1] =~ tr/N/N/';
388 is( $@, '', 'implicit count outside array bounds, index positive' );
389 is( scalar @foo, 0, " doesn't extend the array");
393 eval '$foo{bar} =~ tr/N/N/';
394 is( $@, '', 'implicit count outside hash bounds' );
395 is( scalar keys %foo, 0, " doesn't extend the hash");
398 is( $x =~ tr/A/A/, 2, 'non-modifying tr/// on a scalar ref' );
399 is( ref $x, 'SCALAR', " doesn't stringify its argument" );
401 # rt.perl.org 36622. Perl didn't like a y/// at end of file. No trailing
403 fresh_perl_is(q[$_ = "foo"; y/A-Z/a-z/], '');
406 { # [perl #38293] chr(65535) should be allowed in regexes
407 no warnings 'utf8'; # to allow non-characters
409 $s = "\x{d800}\x{ffff}";
411 is($s, "\x{d800}\x{ffff}", "do_trans_simple");
413 $s = "\x{d800}\x{ffff}";
415 is($i, 0, "do_trans_count");
417 $s = "\x{d800}\x{ffff}";
419 is($s, "\x{d800}\x{ffff}", "do_trans_complex, SQUASH");
421 $s = "\x{d800}\x{ffff}";
423 is($s, "AA", "do_trans_complex, COMPLEMENT");
426 $s =~ tr/\x{ffff}/\x{1ffff}/;
427 is($s, "A\x{1ffff}B", "utf8, SEARCHLIST");
429 $s = "\x{fffd}\x{fffe}\x{ffff}";
430 $s =~ tr/\x{fffd}-\x{ffff}/ABC/;
431 is($s, "ABC", "utf8, SEARCHLIST range");
434 $s =~ tr/ABC/\x{ffff}/;
435 is($s, "\x{ffff}"x3, "utf8, REPLACEMENTLIST");
438 $s =~ tr/ABC/\x{fffd}-\x{ffff}/;
439 is($s, "\x{fffd}\x{fffe}\x{ffff}", "utf8, REPLACEMENTLIST range");
441 $s = "A\x{ffff}B\x{100}\0\x{fffe}\x{ffff}";
442 $i = $s =~ tr/\x{ffff}//;
443 is($i, 2, "utf8, count");
445 $s = "A\x{ffff}\x{ffff}C";
446 $s =~ tr/\x{ffff}/\x{100}/s;
447 is($s, "A\x{100}C", "utf8, SQUASH");
449 $s = "A\x{ffff}\x{ffff}\x{fffe}\x{fffe}\x{fffe}C";
450 $s =~ tr/\x{fffe}\x{ffff}//s;
451 is($s, "A\x{ffff}\x{fffe}C", "utf8, SQUASH");
454 $s =~ tr/AB/\x{ffff}/s;
455 is($s, "x\x{ffff}y", "utf8, SQUASH");
458 $s =~ tr/AB/\x{fffe}\x{ffff}/s;
459 is($s, "x\x{fffe}\x{ffff}y", "utf8, SQUASH");
461 $s = "A\x{ffff}B\x{fffe}C";
462 $s =~ tr/\x{fffe}\x{ffff}/x/c;
463 is($s, "x\x{ffff}x\x{fffe}x", "utf8, COMPLEMENT");
465 $s = "A\x{10000}B\x{2abcd}C";
466 $s =~ tr/\0-\x{ffff}/x/c;
467 is($s, "AxBxC", "utf8, COMPLEMENT range");
469 $s = "A\x{fffe}B\x{ffff}C";
470 $s =~ tr/\x{fffe}\x{ffff}/x/d;
471 is($s, "AxBC", "utf8, DELETE");
473 } # non-characters end
475 { # related to [perl #27940]
478 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ tr/\c@-\c_//d;
479 is($c, "\x20\x30\x40\x50\x60", "tr/\\c\@-\\c_//d");
481 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ tr/\x00-\x1f//d;
482 is($c, "\x20\x30\x40\x50\x60", "tr/\\x00-\\x1f//d");
485 ($s) = keys %{{pie => 3}};
487 if (!eval { require B }) { skip "no B", 1 }
488 my $wasro = B::svref_2object(\$s)->FLAGS & &B::SVf_READONLY;
489 $wasro or local $TODO = "didn't have a COW";
491 ok( B::svref_2object(\$s)->FLAGS & &B::SVf_READONLY,
492 "count-only tr doesn't deCOW COWs" );
497 # under threads, unicode tr within a cloned closure would SEGV or assert
498 # fail, since the pointer in the pad to the swash was getting zeroed out
504 $x =~ tr[\x{142}][\x{143}];
506 is($x,"\x{143}", "utf8 + closure");