This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add ${^GLOBAL_PHASE}
[perl5.git] / t / op / tr.t
CommitLineData
c8e3bb4c
GS
1# tr.t
2
f05dd7cc
JH
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
953ab6e5 6 require './test.pl';
f05dd7cc 7}
a5095b95 8
bb16bae8 9plan tests => 128;
953ab6e5
MS
10
11my $Is_EBCDIC = (ord('i') == 0x89 & ord('J') == 0xd1);
c8e3bb4c
GS
12
13$_ = "abcdefghijklmnopqrstuvwxyz";
14
15tr/a-z/A-Z/;
16
953ab6e5 17is($_, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'uc');
c8e3bb4c
GS
18
19tr/A-Z/a-z/;
20
953ab6e5 21is($_, "abcdefghijklmnopqrstuvwxyz", 'lc');
c8e3bb4c
GS
22
23tr/b-y/B-Y/;
953ab6e5 24is($_, "aBCDEFGHIJKLMNOPQRSTUVWXYz", 'partial uc');
c8e3bb4c 25
c8e3bb4c
GS
26
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).
5e037136
GS
30{
31 no utf8;
32 $_ = "I\xcaJ";
c8e3bb4c 33
5e037136 34 tr/I-J/i-j/;
c8e3bb4c 35
ff36f15d 36 is($_, "i\xcaj", 'EBCDIC discontinuity');
5e037136 37}
c8e3bb4c 38#
2de7b02f 39
953ab6e5 40
2de7b02f
GS
41($x = 12) =~ tr/1/3/;
42(my $y = 12) =~ tr/1/3/;
43($f = 1.5) =~ tr/1/3/;
44(my $g = 1.5) =~ tr/1/3/;
953ab6e5
MS
45is($x + $y + $f + $g, 71, 'tr cancels IOK and NOK');
46
bb16bae8
FC
47# /r
48$_ = 'adam';
49is y/dam/ve/rd, 'eve', '/r';
50is $_, 'adam', '/r leaves param alone';
51$g = 'ruby';
52is $g =~ y/bury/repl/r, 'perl', '/r with explicit param';
53is $g, 'ruby', '/r leaves explicit param alone';
54is "aaa" =~ y\a\b\r, 'bbb', '/r with constant param';
55ok !eval '$_ !~ y///r', "!~ y///r is forbidden";
56like $@, qr\^Using !~ with tr///r doesn't make sense\,
57 "!~ y///r error message";
58{
59 my $w;
60 my $wc;
61 local $SIG{__WARN__} = sub { $w = shift; ++$wc };
62 local $^W = 1;
63 eval 'y///r; 1';
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';
67}
2de7b02f 68
953ab6e5 69# perlbug [ID 20000511.005]
2de7b02f
GS
70$_ = 'fred';
71/([a-z]{2})/;
72$1 =~ tr/A-Z//;
73s/^(\s*)f/$1F/;
953ab6e5
MS
74is($_, 'Fred', 'harmless if explicitly not updating');
75
76
77# A variant of the above, added in 5.7.2
78$_ = 'fred';
79/([a-z]{2})/;
80eval '$1 =~ tr/A-Z/A-Z/;';
81s/^(\s*)f/$1F/;
82is($_, 'Fred', 'harmless if implicitly not updating');
83is($@, '', ' no error');
84
2de7b02f
GS
85
86# check tr handles UTF8 correctly
87($x = 256.65.258) =~ tr/a/b/;
953ab6e5
MS
88is($x, 256.65.258, 'handles UTF8');
89is(length $x, 3);
90
2de7b02f 91$x =~ tr/A/B/;
953ab6e5 92is(length $x, 3);
67a17885 93if (ord("\t") == 9) { # ASCII
953ab6e5 94 is($x, 256.66.258);
67a17885
PP
95}
96else {
953ab6e5 97 is($x, 256.65.258);
67a17885 98}
953ab6e5 99
cbe7f703
PP
100# EBCDIC variants of the above tests
101($x = 256.193.258) =~ tr/a/b/;
953ab6e5
MS
102is(length $x, 3);
103is($x, 256.193.258);
104
cbe7f703 105$x =~ tr/A/B/;
953ab6e5 106is(length $x, 3);
cbe7f703 107if (ord("\t") == 9) { # ASCII
953ab6e5 108 is($x, 256.193.258);
cbe7f703
PP
109}
110else {
953ab6e5 111 is($x, 256.194.258);
cbe7f703 112}
953ab6e5 113
036b4402
GS
114
115{
953ab6e5
MS
116 my $l = chr(300); my $r = chr(400);
117 $x = 200.300.400;
118 $x =~ tr/\x{12c}/\x{190}/;
119 is($x, 200.400.400,
120 'changing UTF8 chars in a UTF8 string, same length');
121 is(length $x, 3);
122
123 $x = 200.300.400;
124 $x =~ tr/\x{12c}/\x{be8}/;
125 is($x, 200.3048.400, ' more bytes');
126 is(length $x, 3);
127
128 $x = 100.125.60;
129 $x =~ tr/\x{64}/\x{190}/;
130 is($x, 400.125.60, 'Putting UT8 chars into a non-UTF8 string');
131 is(length $x, 3);
132
133 $x = 400.125.60;
134 $x =~ tr/\x{190}/\x{64}/;
135 is($x, 100.125.60, 'Removing UTF8 chars from UTF8 string');
136 is(length $x, 3);
137
138 $x = 400.125.60.400;
139 $y = $x =~ tr/\x{190}/\x{190}/;
140 is($y, 2, 'Counting UTF8 chars in UTF8 string');
141
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');
145
146 # 17 - counting UTF8 chars in non-UTF8 string
147 $x = 200.125.60;
148 $y = $x =~ tr/\x{190}/\x{190}/;
149 is($y, 0, ' UTF8 chars in non-UTFs string');
036b4402 150}
c2e66d9e 151
c2e66d9e 152$_ = "abcdefghijklmnopqrstuvwxyz";
953ab6e5
MS
153eval 'tr/a-z-9/ /';
154like($@, qr/^Ambiguous range in transliteration operator/, 'tr/a-z-9//');
c2e66d9e 155
cbe7f703 156# 19-21: Make sure leading and trailing hyphens still work
c2e66d9e
GS
157$_ = "car-rot9";
158tr/-a-m/./;
953ab6e5 159is($_, '..r.rot9', 'hyphens, leading');
c2e66d9e
GS
160
161$_ = "car-rot9";
162tr/a-m-/./;
953ab6e5 163is($_, '..r.rot9', ' trailing');
c2e66d9e
GS
164
165$_ = "car-rot9";
166tr/-a-m-/./;
953ab6e5 167is($_, '..r.rot9', ' both');
c2e66d9e
GS
168
169$_ = "abcdefghijklmnop";
170tr/ae-hn/./;
953ab6e5 171is($_, '.bcd....ijklm.op');
c2e66d9e
GS
172
173$_ = "abcdefghijklmnop";
174tr/a-cf-kn-p/./;
953ab6e5 175is($_, '...de......lm...');
c2e66d9e
GS
176
177$_ = "abcdefghijklmnop";
178tr/a-ceg-ikm-o/./;
953ab6e5
MS
179is($_, '...d.f...j.l...p');
180
c2e66d9e 181
c2e66d9e
GS
182# 20000705 MJD
183eval "tr/m-d/ /";
321ecc04 184like($@, qr/^Invalid range "m-d" in transliteration operator/,
953ab6e5 185 'reversed range check');
c2e66d9e 186
d897a58d 187'abcdef' =~ /(bcd)/;
953ab6e5
MS
188is(eval '$1 =~ tr/abcd//', 3, 'explicit read-only count');
189is($@, '', ' no error');
d897a58d 190
953ab6e5
MS
191'abcdef' =~ /(bcd)/;
192is(eval '$1 =~ tr/abcd/abcd/', 3, 'implicit read-only count');
193is($@, '', ' no error');
194
195is(eval '"123" =~ tr/12//', 2, 'LHS of non-updating tr');
d897a58d 196
94bfe852 197eval '"123" =~ tr/1/2/';
953ab6e5
MS
198like($@, qr|^Can't modify constant item in transliteration \(tr///\)|,
199 'LHS bad on updating tr');
200
d897a58d 201
381d18bc
JH
202# v300 (0x12c) is UTF-8-encoded as 196 172 (0xc4 0xac)
203# v400 (0x190) is UTF-8-encoded as 198 144 (0xc6 0x90)
204
205# Transliterate a byte to a byte, all four ways.
206
207($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/;
953ab6e5 208is($a, v300.197.172.300.197.172, 'byte2byte transliteration');
381d18bc
JH
209
210($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{c5}/;
953ab6e5 211is($a, v300.197.172.300.197.172);
381d18bc
JH
212
213($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\xc5/;
953ab6e5 214is($a, v300.197.172.300.197.172);
381d18bc
JH
215
216($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\x{c5}/;
953ab6e5 217is($a, v300.197.172.300.197.172);
381d18bc 218
381d18bc
JH
219
220($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/;
953ab6e5 221is($a, v300.301.172.300.301.172, 'byte2wide transliteration');
381d18bc
JH
222
223($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc3/;
953ab6e5 224is($a, v195.196.172.195.196.172, ' wide2byte');
381d18bc
JH
225
226($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/;
953ab6e5 227is($a, v301.196.172.301.196.172, ' wide2wide');
381d18bc 228
381d18bc
JH
229
230($a = v300.196.172.300.196.172) =~ tr/\xc4\x{12c}/\x{12d}\xc3/;
953ab6e5 231is($a, v195.301.172.195.301.172, 'byte2wide & wide2byte');
381d18bc 232
381d18bc
JH
233
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}/;
953ab6e5 236is($a, v197.301.173.197.301.173.401.198.144, 'all together now!');
381d18bc 237
381d18bc 238
953ab6e5
MS
239is((($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/), 2,
240 'transliterate and count');
381d18bc 241
953ab6e5 242is((($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/), 2);
381d18bc 243
381d18bc
JH
244
245($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/c;
953ab6e5 246is($a, v301.196.301.301.196.301, 'translit w/complement');
381d18bc
JH
247
248($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc5/c;
953ab6e5 249is($a, v300.197.197.300.197.197);
381d18bc 250
381d18bc
JH
251
252($a = v300.196.172.300.196.172) =~ tr/\xc4//d;
953ab6e5 253is($a, v300.172.300.172, 'translit w/deletion');
381d18bc
JH
254
255($a = v300.196.172.300.196.172) =~ tr/\x{12c}//d;
953ab6e5 256is($a, v196.172.196.172);
381d18bc 257
381d18bc
JH
258
259($a = v196.196.172.300.300.196.172) =~ tr/\xc4/\xc5/s;
953ab6e5 260is($a, v197.172.300.300.197.172, 'translit w/squeeze');
381d18bc
JH
261
262($a = v196.172.300.300.196.172.172) =~ tr/\x{12c}/\x{12d}/s;
953ab6e5 263is($a, v196.172.301.196.172.172);
381d18bc 264
a1874b66 265
953ab6e5 266# Tricky cases (When Simon Cozens Attacks)
a1874b66 267($a = v196.172.200) =~ tr/\x{12c}/a/;
953ab6e5 268is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
269
270($a = v196.172.200) =~ tr/\x{12c}/\x{12c}/;
953ab6e5 271is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
272
273($a = v196.172.200) =~ tr/\x{12c}//d;
953ab6e5
MS
274is(sprintf("%vd", $a), '196.172.200');
275
a1874b66 276
8973db79 277# UTF8 range tests from Inaba Hiroto
f9a63242 278
a26bfc40 279# Not working in EBCDIC as of 12674.
f9a63242 280($a = v300.196.172.302.197.172) =~ tr/\x{12c}-\x{130}/\xc0-\xc4/;
953ab6e5 281is($a, v192.196.172.194.197.172, 'UTF range');
f9a63242
JH
282
283($a = v300.196.172.302.197.172) =~ tr/\xc4-\xc8/\x{12c}-\x{130}/;
953ab6e5
MS
284is($a, v300.300.172.302.301.172);
285
8973db79
JH
286
287# UTF8 range tests from Karsten Sperling (patch #9008 required)
288
289($a = "\x{0100}") =~ tr/\x00-\x{100}/X/;
953ab6e5 290is($a, "X");
8973db79
JH
291
292($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}/X/c;
953ab6e5 293is($a, "X");
8973db79
JH
294
295($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5 296is($a, "X");
8973db79
JH
297
298($a = v256) =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5
MS
299is($a, "X");
300
8973db79 301
94472101
JH
302# UTF8 range tests from Inaba Hiroto
303
304($a = "\x{200}") =~ tr/\x00-\x{100}/X/c;
953ab6e5 305is($a, "X");
94472101
JH
306
307($a = "\x{200}") =~ tr/\x00-\x{100}/X/cs;
953ab6e5
MS
308is($a, "X");
309
94472101 310
6b6bd37b
JH
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.
314
315$c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/\x89-\x91/X/;
953ab6e5
MS
316is($c, 8);
317is($a, "XXXXXXXX");
4c3a8340 318
6b6bd37b 319$c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/\xc9-\xd1/X/;
953ab6e5
MS
320is($c, 8);
321is($a, "XXXXXXXX");
6b6bd37b 322
4c3a8340 323SKIP: {
953ab6e5
MS
324 skip "not EBCDIC", 4 unless $Is_EBCDIC;
325
326 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/i-j/X/;
327 is($c, 2);
328 is($a, "X\x8a\x8b\x8c\x8d\x8f\x90X");
329
330 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/I-J/X/;
331 is($c, 2);
332 is($a, "X\xca\xcb\xcc\xcd\xcf\xd0X");
6b6bd37b 333}
1ed601ec
JH
334
335($a = "\x{100}") =~ tr/\x00-\xff/X/c;
953ab6e5 336is(ord($a), ord("X"));
1ed601ec
JH
337
338($a = "\x{100}") =~ tr/\x00-\xff/X/cs;
953ab6e5 339is(ord($a), ord("X"));
1ed601ec
JH
340
341($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//c;
953ab6e5 342is($a, "\x{100}\x{100}");
1ed601ec
JH
343
344($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//cs;
953ab6e5 345is($a, "\x{100}");
1ed601ec 346
629b4584 347$a = "\xfe\xff"; $a =~ tr/\xfe\xff/\x{1ff}\x{1fe}/;
953ab6e5
MS
348is($a, "\x{1ff}\x{1fe}");
349
76ef7183
JH
350
351# From David Dyck
352($a = "R0_001") =~ tr/R_//d;
953ab6e5 353is(hex($a), 1);
76ef7183 354
800b4dc4
JH
355# From Inaba Hiroto
356@a = (1,2); map { y/1/./ for $_ } @a;
953ab6e5 357is("@a", ". 2");
800b4dc4
JH
358
359@a = (1,2); map { y/1/./ for $_.'' } @a;
953ab6e5
MS
360is("@a", "1 2");
361
800b4dc4 362
bec89253
RH
363# Additional test for Inaba Hiroto patch (robin@kitsite.com)
364($a = "\x{100}\x{102}\x{101}") =~ tr/\x00-\377/XYZ/c;
953ab6e5
MS
365is($a, "XZY");
366
bec89253 367
2233f375
NC
368# Used to fail with "Modification of a read-only value attempted"
369%a = (N=>1);
370foreach (keys %a) {
953ab6e5
MS
371 eval 'tr/N/n/';
372 is($_, 'n', 'pp_trans needs to unshare shared hash keys');
373 is($@, '', ' no error');
2233f375 374}
94bfe852 375
953ab6e5 376
94bfe852 377$x = eval '"1213" =~ tr/1/1/';
953ab6e5
MS
378is($x, 2, 'implicit count on constant');
379is($@, '', ' no error');
380
381
382my @foo = ();
383eval '$foo[-1] =~ tr/N/N/';
384is( $@, '', 'implicit count outside array bounds, index negative' );
385is( scalar @foo, 0, " doesn't extend the array");
386
387eval '$foo[1] =~ tr/N/N/';
388is( $@, '', 'implicit count outside array bounds, index positive' );
389is( scalar @foo, 0, " doesn't extend the array");
390
391
392my %foo = ();
393eval '$foo{bar} =~ tr/N/N/';
394is( $@, '', 'implicit count outside hash bounds' );
395is( scalar keys %foo, 0, " doesn't extend the hash");
d59e14db
RGS
396
397$x = \"foo";
398is( $x =~ tr/A/A/, 2, 'non-modifying tr/// on a scalar ref' );
399is( ref $x, 'SCALAR', " doesn't stringify its argument" );
0d65d7d5
MS
400
401# rt.perl.org 36622. Perl didn't like a y/// at end of file. No trailing
402# newline allowed.
403fresh_perl_is(q[$_ = "foo"; y/A-Z/a-z/], '');
9f7f3913
TS
404
405
406{ # [perl #38293] chr(65535) should be allowed in regexes
407no warnings 'utf8'; # to allow non-characters
408
409$s = "\x{d800}\x{ffff}";
410$s =~ tr/\0/A/;
411is($s, "\x{d800}\x{ffff}", "do_trans_simple");
412
413$s = "\x{d800}\x{ffff}";
414$i = $s =~ tr/\0//;
415is($i, 0, "do_trans_count");
416
417$s = "\x{d800}\x{ffff}";
418$s =~ tr/\0/A/s;
419is($s, "\x{d800}\x{ffff}", "do_trans_complex, SQUASH");
420
421$s = "\x{d800}\x{ffff}";
422$s =~ tr/\0/A/c;
423is($s, "AA", "do_trans_complex, COMPLEMENT");
424
425$s = "A\x{ffff}B";
426$s =~ tr/\x{ffff}/\x{1ffff}/;
427is($s, "A\x{1ffff}B", "utf8, SEARCHLIST");
428
429$s = "\x{fffd}\x{fffe}\x{ffff}";
430$s =~ tr/\x{fffd}-\x{ffff}/ABC/;
431is($s, "ABC", "utf8, SEARCHLIST range");
432
433$s = "ABC";
434$s =~ tr/ABC/\x{ffff}/;
435is($s, "\x{ffff}"x3, "utf8, REPLACEMENTLIST");
436
437$s = "ABC";
438$s =~ tr/ABC/\x{fffd}-\x{ffff}/;
439is($s, "\x{fffd}\x{fffe}\x{ffff}", "utf8, REPLACEMENTLIST range");
440
441$s = "A\x{ffff}B\x{100}\0\x{fffe}\x{ffff}";
442$i = $s =~ tr/\x{ffff}//;
443is($i, 2, "utf8, count");
444
445$s = "A\x{ffff}\x{ffff}C";
446$s =~ tr/\x{ffff}/\x{100}/s;
447is($s, "A\x{100}C", "utf8, SQUASH");
448
449$s = "A\x{ffff}\x{ffff}\x{fffe}\x{fffe}\x{fffe}C";
450$s =~ tr/\x{fffe}\x{ffff}//s;
451is($s, "A\x{ffff}\x{fffe}C", "utf8, SQUASH");
452
453$s = "xAABBBy";
454$s =~ tr/AB/\x{ffff}/s;
455is($s, "x\x{ffff}y", "utf8, SQUASH");
456
457$s = "xAABBBy";
458$s =~ tr/AB/\x{fffe}\x{ffff}/s;
459is($s, "x\x{fffe}\x{ffff}y", "utf8, SQUASH");
460
461$s = "A\x{ffff}B\x{fffe}C";
462$s =~ tr/\x{fffe}\x{ffff}/x/c;
463is($s, "x\x{ffff}x\x{fffe}x", "utf8, COMPLEMENT");
464
465$s = "A\x{10000}B\x{2abcd}C";
466$s =~ tr/\0-\x{ffff}/x/c;
467is($s, "AxBxC", "utf8, COMPLEMENT range");
468
469$s = "A\x{fffe}B\x{ffff}C";
470$s =~ tr/\x{fffe}\x{ffff}/x/d;
471is($s, "AxBC", "utf8, DELETE");
472
473} # non-characters end
474
1749ea0d
TS
475{ # related to [perl #27940]
476 my $c;
477
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");
480
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");
483}
484
3788ef8f
YST
485($s) = keys %{{pie => 3}};
486my $wasro = Internals::SvREADONLY($s);
487{
488 $wasro or local $TODO = "didn't have a COW";
489 $s =~ tr/i//;
490 ok( Internals::SvREADONLY($s), "count-only tr doesn't deCOW COWs" );
491}
a5446a64
DM
492
493# [ RT #61520 ]
494#
495# under threads, unicode tr within a cloned closure would SEGV or assert
496# fail, since the pointer in the pad to the swash was getting zeroed out
497# in the proto-CV
498
499{
500 my $x = "\x{142}";
501 sub {
502 $x =~ tr[\x{142}][\x{143}];
503 }->();
504 is($x,"\x{143}", "utf8 + closure");
505}
506
507