13 # Test this first before we extend the stack with other operations.
14 # This caused an asan failure due to a bad write past the end of the stack.
15 eval { my $x; die 1..127, $x =~ y/// };
17 my $Is_EBCDIC = (ord('i') == 0x89 & ord('J') == 0xd1);
19 $_ = "abcdefghijklmnopqrstuvwxyz";
23 is($_, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'uc');
27 is($_, "abcdefghijklmnopqrstuvwxyz", 'lc');
30 is($_, "aBCDEFGHIJKLMNOPQRSTUVWXYz", 'partial uc');
33 # In EBCDIC 'I' is \xc9 and 'J' is \0xd1, 'i' is \x89 and 'j' is \x91.
34 # Yes, discontinuities. Regardless, the \xca in the below should stay
35 # untouched (and not became \x8a).
42 is($_, "i\xcaj", 'EBCDIC discontinuity');
48 (my $y = 12) =~ tr/1/3/;
49 ($f = 1.5) =~ tr/1/3/;
50 (my $g = 1.5) =~ tr/1/3/;
51 is($x + $y + $f + $g, 71, 'tr cancels IOK and NOK');
55 is y/dam/ve/rd, 'eve', '/r';
56 is $_, 'adam', '/r leaves param alone';
58 is $g =~ y/bury/repl/r, 'perl', '/r with explicit param';
59 is $g, 'ruby', '/r leaves explicit param alone';
60 is "aaa" =~ y\a\b\r, 'bbb', '/r with constant param';
61 ok !eval '$_ !~ y///r', "!~ y///r is forbidden";
62 like $@, qr\^Using !~ with tr///r doesn't make sense\,
63 "!~ y///r error message";
67 local $SIG{__WARN__} = sub { $w = shift; ++$wc };
70 like $w, qr '^Useless use of non-destructive transliteration \(tr///r\)',
71 '/r warns in void context';
72 is $wc, 1, '/r warns just once';
75 # perlbug [ID 20000511.005]
80 is($_, 'Fred', 'harmless if explicitly not updating');
83 # A variant of the above, added in 5.7.2
86 eval '$1 =~ tr/A-Z/A-Z/;';
88 is($_, 'Fred', 'harmless if implicitly not updating');
89 is($@, '', ' no error');
92 # check tr handles UTF8 correctly
93 ($x = 256.65.258) =~ tr/a/b/;
94 is($x, 256.65.258, 'handles UTF8');
99 if (ord("\t") == 9) { # ASCII
106 # EBCDIC variants of the above tests
107 ($x = 256.193.258) =~ tr/a/b/;
113 if (ord("\t") == 9) { # ASCII
122 my $l = chr(300); my $r = chr(400);
124 $x =~ tr/\x{12c}/\x{190}/;
126 'changing UTF8 chars in a UTF8 string, same length');
130 $x =~ tr/\x{12c}/\x{be8}/;
131 is($x, 200.3048.400, ' more bytes');
135 $x =~ tr/\x{64}/\x{190}/;
136 is($x, 400.125.60, 'Putting UT8 chars into a non-UTF8 string');
140 $x =~ tr/\x{190}/\x{64}/;
141 is($x, 100.125.60, 'Removing UTF8 chars from UTF8 string');
145 $y = $x =~ tr/\x{190}/\x{190}/;
146 is($y, 2, 'Counting UTF8 chars in UTF8 string');
148 $x = 60.400.125.60.400;
149 $y = $x =~ tr/\x{3c}/\x{3c}/;
150 is($y, 2, ' non-UTF8 chars in UTF8 string');
152 # 17 - counting UTF8 chars in non-UTF8 string
154 $y = $x =~ tr/\x{190}/\x{190}/;
155 is($y, 0, ' UTF8 chars in non-UTFs string');
158 $_ = "abcdefghijklmnopqrstuvwxyz";
160 like($@, qr/^Ambiguous range in transliteration operator/, 'tr/a-z-9//');
162 # 19-21: Make sure leading and trailing hyphens still work
165 is($_, '..r.rot9', 'hyphens, leading');
169 is($_, '..r.rot9', ' trailing');
173 is($_, '..r.rot9', ' both');
175 $_ = "abcdefghijklmnop";
177 is($_, '.bcd....ijklm.op');
179 $_ = "abcdefghijklmnop";
181 is($_, '...de......lm...');
183 $_ = "abcdefghijklmnop";
185 is($_, '...d.f...j.l...p');
190 like($@, qr/^Invalid range "m-d" in transliteration operator/,
191 'reversed range check');
194 is(eval '$1 =~ tr/abcd//', 3, 'explicit read-only count');
195 is($@, '', ' no error');
198 is(eval '$1 =~ tr/abcd/abcd/', 3, 'implicit read-only count');
199 is($@, '', ' no error');
201 is(eval '"123" =~ tr/12//', 2, 'LHS of non-updating tr');
203 eval '"123" =~ tr/1/2/';
204 like($@, qr|^Can't modify constant item in transliteration \(tr///\)|,
205 'LHS bad on updating tr');
208 # v300 (0x12c) is UTF-8-encoded as 196 172 (0xc4 0xac)
209 # v400 (0x190) is UTF-8-encoded as 198 144 (0xc6 0x90)
211 # Transliterate a byte to a byte, all four ways.
213 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/;
214 is($a, v300.197.172.300.197.172, 'byte2byte transliteration');
216 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{c5}/;
217 is($a, v300.197.172.300.197.172);
219 ($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\xc5/;
220 is($a, v300.197.172.300.197.172);
222 ($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\x{c5}/;
223 is($a, v300.197.172.300.197.172);
226 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/;
227 is($a, v300.301.172.300.301.172, 'byte2wide transliteration');
229 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc3/;
230 is($a, v195.196.172.195.196.172, ' wide2byte');
232 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/;
233 is($a, v301.196.172.301.196.172, ' wide2wide');
236 ($a = v300.196.172.300.196.172) =~ tr/\xc4\x{12c}/\x{12d}\xc3/;
237 is($a, v195.301.172.195.301.172, 'byte2wide & wide2byte');
240 ($a = v300.196.172.300.196.172.400.198.144) =~
241 tr/\xac\xc4\x{12c}\x{190}/\xad\x{12d}\xc5\x{191}/;
242 is($a, v197.301.173.197.301.173.401.198.144, 'all together now!');
245 is((($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/), 2,
246 'transliterate and count');
248 is((($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/), 2);
251 ($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/c;
252 is($a, v301.196.301.301.196.301, 'translit w/complement');
254 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc5/c;
255 is($a, v300.197.197.300.197.197);
258 ($a = v300.196.172.300.196.172) =~ tr/\xc4//d;
259 is($a, v300.172.300.172, 'translit w/deletion');
261 ($a = v300.196.172.300.196.172) =~ tr/\x{12c}//d;
262 is($a, v196.172.196.172);
265 ($a = v196.196.172.300.300.196.172) =~ tr/\xc4/\xc5/s;
266 is($a, v197.172.300.300.197.172, 'translit w/squeeze');
268 ($a = v196.172.300.300.196.172.172) =~ tr/\x{12c}/\x{12d}/s;
269 is($a, v196.172.301.196.172.172);
272 # Tricky cases (When Simon Cozens Attacks)
273 ($a = v196.172.200) =~ tr/\x{12c}/a/;
274 is(sprintf("%vd", $a), '196.172.200');
276 ($a = v196.172.200) =~ tr/\x{12c}/\x{12c}/;
277 is(sprintf("%vd", $a), '196.172.200');
279 ($a = v196.172.200) =~ tr/\x{12c}//d;
280 is(sprintf("%vd", $a), '196.172.200');
283 # UTF8 range tests from Inaba Hiroto
285 # Not working in EBCDIC as of 12674.
286 ($a = v300.196.172.302.197.172) =~ tr/\x{12c}-\x{130}/\xc0-\xc4/;
287 is($a, v192.196.172.194.197.172, 'UTF range');
289 ($a = v300.196.172.302.197.172) =~ tr/\xc4-\xc8/\x{12c}-\x{130}/;
290 is($a, v300.300.172.302.301.172);
293 # UTF8 range tests from Karsten Sperling (patch #9008 required)
295 ($a = "\x{0100}") =~ tr/\x00-\x{100}/X/;
298 ($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}/X/c;
301 ($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
304 ($a = v256) =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
308 # UTF8 range tests from Inaba Hiroto
310 ($a = "\x{200}") =~ tr/\x00-\x{100}/X/c;
313 ($a = "\x{200}") =~ tr/\x00-\x{100}/X/cs;
316 # Tricky on EBCDIC: while [a-z] [A-Z] must not match the gap characters (as
317 # well as i-j, r-s, I-J, R-S), [\x89-\x91] [\xc9-\xd1] has to match them,
318 # from Karsten Sperling.
320 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/\x89-\x91/X/;
324 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/\xc9-\xd1/X/;
329 skip "not EBCDIC", 4 unless $Is_EBCDIC;
331 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/i-j/X/;
333 is($a, "X\x8a\x8b\x8c\x8d\x8f\x90X");
335 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/I-J/X/;
337 is($a, "X\xca\xcb\xcc\xcd\xcf\xd0X");
340 ($a = "\x{100}") =~ tr/\x00-\xff/X/c;
341 is(ord($a), ord("X"));
343 ($a = "\x{100}") =~ tr/\x00-\xff/X/cs;
344 is(ord($a), ord("X"));
346 ($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//c;
347 is($a, "\x{100}\x{100}");
349 ($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//cs;
352 $a = "\xfe\xff"; $a =~ tr/\xfe\xff/\x{1ff}\x{1fe}/;
353 is($a, "\x{1ff}\x{1fe}");
357 ($a = "R0_001") =~ tr/R_//d;
361 @a = (1,2); map { y/1/./ for $_ } @a;
364 @a = (1,2); map { y/1/./ for $_.'' } @a;
368 # Additional test for Inaba Hiroto patch (robin@kitsite.com)
369 ($a = "\x{100}\x{102}\x{101}") =~ tr/\x00-\377/XYZ/c;
373 # Used to fail with "Modification of a read-only value attempted"
377 is($_, 'n', 'pp_trans needs to unshare shared hash keys');
378 is($@, '', ' no error');
382 $x = eval '"1213" =~ tr/1/1/';
383 is($x, 2, 'implicit count on constant');
384 is($@, '', ' no error');
388 eval '$foo[-1] =~ tr/N/N/';
389 is( $@, '', 'implicit count outside array bounds, index negative' );
390 is( scalar @foo, 0, " doesn't extend the array");
392 eval '$foo[1] =~ tr/N/N/';
393 is( $@, '', 'implicit count outside array bounds, index positive' );
394 is( scalar @foo, 0, " doesn't extend the array");
398 eval '$foo{bar} =~ tr/N/N/';
399 is( $@, '', 'implicit count outside hash bounds' );
400 is( scalar keys %foo, 0, " doesn't extend the hash");
403 is( $x =~ tr/A/A/, 2, 'non-modifying tr/// on a scalar ref' );
404 is( ref $x, 'SCALAR', " doesn't stringify its argument" );
406 # rt.perl.org 36622. Perl didn't like a y/// at end of file. No trailing
408 fresh_perl_is(q[$_ = "foo"; y/A-Z/a-z/], '');
411 { # [perl #38293] chr(65535) should be allowed in regexes
412 no warnings 'utf8'; # to allow non-characters
414 $s = "\x{d800}\x{ffff}";
416 is($s, "\x{d800}\x{ffff}", "do_trans_simple");
418 $s = "\x{d800}\x{ffff}";
420 is($i, 0, "do_trans_count");
422 $s = "\x{d800}\x{ffff}";
424 is($s, "\x{d800}\x{ffff}", "do_trans_complex, SQUASH");
426 $s = "\x{d800}\x{ffff}";
428 is($s, "AA", "do_trans_complex, COMPLEMENT");
431 $s =~ tr/\x{ffff}/\x{1ffff}/;
432 is($s, "A\x{1ffff}B", "utf8, SEARCHLIST");
434 $s = "\x{fffd}\x{fffe}\x{ffff}";
435 $s =~ tr/\x{fffd}-\x{ffff}/ABC/;
436 is($s, "ABC", "utf8, SEARCHLIST range");
439 $s =~ tr/ABC/\x{ffff}/;
440 is($s, "\x{ffff}"x3, "utf8, REPLACEMENTLIST");
443 $s =~ tr/ABC/\x{fffd}-\x{ffff}/;
444 is($s, "\x{fffd}\x{fffe}\x{ffff}", "utf8, REPLACEMENTLIST range");
446 $s = "A\x{ffff}B\x{100}\0\x{fffe}\x{ffff}";
447 $i = $s =~ tr/\x{ffff}//;
448 is($i, 2, "utf8, count");
450 $s = "A\x{ffff}\x{ffff}C";
451 $s =~ tr/\x{ffff}/\x{100}/s;
452 is($s, "A\x{100}C", "utf8, SQUASH");
454 $s = "A\x{ffff}\x{ffff}\x{fffe}\x{fffe}\x{fffe}C";
455 $s =~ tr/\x{fffe}\x{ffff}//s;
456 is($s, "A\x{ffff}\x{fffe}C", "utf8, SQUASH");
459 $s =~ tr/AB/\x{ffff}/s;
460 is($s, "x\x{ffff}y", "utf8, SQUASH");
463 $s =~ tr/AB/\x{fffe}\x{ffff}/s;
464 is($s, "x\x{fffe}\x{ffff}y", "utf8, SQUASH");
466 $s = "A\x{ffff}B\x{fffe}C";
467 $s =~ tr/\x{fffe}\x{ffff}/x/c;
468 is($s, "x\x{ffff}x\x{fffe}x", "utf8, COMPLEMENT");
470 $s = "A\x{10000}B\x{2abcd}C";
471 $s =~ tr/\0-\x{ffff}/x/c;
472 is($s, "AxBxC", "utf8, COMPLEMENT range");
474 $s = "A\x{fffe}B\x{ffff}C";
475 $s =~ tr/\x{fffe}\x{ffff}/x/d;
476 is($s, "AxBC", "utf8, DELETE");
478 } # non-characters end
480 { # related to [perl #27940]
483 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ tr/\c@-\c_//d;
484 is($c, "\x20\x30\x40\x50\x60", "tr/\\c\@-\\c_//d");
486 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ tr/\x00-\x1f//d;
487 is($c, "\x20\x30\x40\x50\x60", "tr/\\x00-\\x1f//d");
490 ($s) = keys %{{pie => 3}};
492 if (!eval { require XS::APItest }) { skip "no XS::APItest", 2 }
493 my $wasro = XS::APItest::SvIsCOW($s);
494 ok $wasro, "have a COW";
496 ok( XS::APItest::SvIsCOW($s),
497 "count-only tr doesn't deCOW COWs" );
502 # under threads, unicode tr within a cloned closure would SEGV or assert
503 # fail, since the pointer in the pad to the swash was getting zeroed out
509 $x =~ tr[\x{142}][\x{143}];
511 is($x,"\x{143}", "utf8 + closure");
514 # Freeing of trans ops prior to pmtrans() [perl #102858].
515 eval q{ $a ~= tr/a/b/; };
518 no warnings "deprecated";
519 skip "no encoding", 1 unless eval { require encoding; 1 };
520 eval q{ use encoding "utf8"; $a ~= tr/a/b/; };
528 { no warnings 'utf8'; print "# $x\n"; } # No note() to avoid wide warning.
529 is($x, "Perlβ", "Only first of multiple transliterations is used");
532 # tr/a/b/ should fail even on zero-length read-only strings
533 use constant nullrocow => (keys%{{""=>undef}})[0];
534 for ("", nullrocow) {
535 eval { $_ =~ y/a/b/ };
536 like $@, qr/^Modification of a read-only value attempted at /,
537 'tr/a/b/ fails on zero-length ro string';
540 # Whether they're permitted or not, non-modifying tr/// should not write
541 # to read-only values, even with funky flags.
543 eval q{ ('a' =~ /./) =~ tr///d };
544 ok(1, "tr///d on PL_Yes does not assert");
545 eval q{ ('a' =~ /./) =~ tr/a-z/a-z/d };
546 ok(1, "tr/a-z/a-z/d on PL_Yes does not assert");
547 eval q{ ('a' =~ /./) =~ tr///s };
548 ok(1, "tr///s on PL_Yes does not assert");
549 eval q{ *x =~ tr///d };
550 ok(1, "tr///d on glob does not assert");