This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5.22.3-RC5 and 5.24.1-RC5 today
[perl5.git] / t / op / tr.t
CommitLineData
c8e3bb4c 1# tr.t
f4240379 2$|=1;
c8e3bb4c 3
f05dd7cc
JH
4BEGIN {
5 chdir 't' if -d 't';
953ab6e5 6 require './test.pl';
43ece5b1 7 set_up_inc('../lib');
47918419
JH
8 if (is_miniperl()) {
9 eval 'require utf8';
caab73c0 10 if ($@) { skip_all("miniperl, no 'utf8'") }
47918419 11 }
f05dd7cc 12}
a5095b95 13
47918419
JH
14use utf8;
15
3a34ca0b 16plan tests => 166;
953ab6e5 17
f605e527
FC
18# Test this first before we extend the stack with other operations.
19# This caused an asan failure due to a bad write past the end of the stack.
20eval { my $x; die 1..127, $x =~ y/// };
21
c8e3bb4c
GS
22$_ = "abcdefghijklmnopqrstuvwxyz";
23
24tr/a-z/A-Z/;
25
953ab6e5 26is($_, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'uc');
c8e3bb4c
GS
27
28tr/A-Z/a-z/;
29
953ab6e5 30is($_, "abcdefghijklmnopqrstuvwxyz", 'lc');
c8e3bb4c
GS
31
32tr/b-y/B-Y/;
953ab6e5 33is($_, "aBCDEFGHIJKLMNOPQRSTUVWXYz", 'partial uc');
c8e3bb4c 34
f4240379
KW
35eval 'tr/a/\N{KATAKANA LETTER AINU P}/;';
36like $@,
37 qr/\\N\{KATAKANA LETTER AINU P} must not be a named sequence in transliteration operator/,
38 "Illegal to tr/// named sequence";
39
40eval 'tr/\x{101}-\x{100}//;';
41like $@,
42 qr/Invalid range "\\x\{0101}-\\x\{0100}" in transliteration operator/,
43 "UTF-8 range with min > max";
44
45SKIP: { # Test literal range end point special handling
46 unless ($::IS_EBCDIC) {
47 skip "Valid only for EBCDIC", 24;
48 }
49
50 $_ = "\x89"; # is 'i'
51 tr/i-j//d;
52 is($_, "", '"\x89" should match [i-j]');
53 $_ = "\x8A";
54 tr/i-j//d;
55 is($_, "\x8A", '"\x8A" shouldnt match [i-j]');
56 $_ = "\x90";
57 tr/i-j//d;
58 is($_, "\x90", '"\x90" shouldnt match [i-j]');
59 $_ = "\x91"; # is 'j'
60 tr/i-j//d;
61 is($_, "", '"\x91" should match [i-j]');
62
63 $_ = "\x89";
64 tr/i-\N{LATIN SMALL LETTER J}//d;
65 is($_, "", '"\x89" should match [i-\N{LATIN SMALL LETTER J}]');
66 $_ = "\x8A";
67 tr/i-\N{LATIN SMALL LETTER J}//d;
68 is($_, "\x8A", '"\x8A" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
69 $_ = "\x90";
70 tr/i-\N{LATIN SMALL LETTER J}//d;
71 is($_, "\x90", '"\x90" shouldnt match [i-\N{LATIN SMALL LETTER J}]');
72 $_ = "\x91";
73 tr/i-\N{LATIN SMALL LETTER J}//d;
74 is($_, "", '"\x91" should match [i-\N{LATIN SMALL LETTER J}]');
75
76 $_ = "\x89";
77 tr/i-\N{U+6A}//d;
78 is($_, "", '"\x89" should match [i-\N{U+6A}]');
79 $_ = "\x8A";
80 tr/i-\N{U+6A}//d;
81 is($_, "\x8A", '"\x8A" shouldnt match [i-\N{U+6A}]');
82 $_ = "\x90";
83 tr/i-\N{U+6A}//d;
84 is($_, "\x90", '"\x90" shouldnt match [i-\N{U+6A}]');
85 $_ = "\x91";
86 tr/i-\N{U+6A}//d;
87 is($_, "", '"\x91" should match [i-\N{U+6A}]');
88
89 $_ = "\x89";
90 tr/\N{U+69}-\N{U+6A}//d;
91 is($_, "", '"\x89" should match [\N{U+69}-\N{U+6A}]');
92 $_ = "\x8A";
93 tr/\N{U+69}-\N{U+6A}//d;
94 is($_, "\x8A", '"\x8A" shouldnt match [\N{U+69}-\N{U+6A}]');
95 $_ = "\x90";
96 tr/\N{U+69}-\N{U+6A}//d;
97 is($_, "\x90", '"\x90" shouldnt match [\N{U+69}-\N{U+6A}]');
98 $_ = "\x91";
99 tr/\N{U+69}-\N{U+6A}//d;
100 is($_, "", '"\x91" should match [\N{U+69}-\N{U+6A}]');
101
102 $_ = "\x89";
103 tr/i-\x{91}//d;
104 is($_, "", '"\x89" should match [i-\x{91}]');
105 $_ = "\x8A";
106 tr/i-\x{91}//d;
107 is($_, "", '"\x8A" should match [i-\x{91}]');
108 $_ = "\x90";
109 tr/i-\x{91}//d;
110 is($_, "", '"\x90" should match [i-\x{91}]');
111 $_ = "\x91";
112 tr/i-\x{91}//d;
113 is($_, "", '"\x91" should match [i-\x{91}]');
114
115 # Need to use eval, because tries to compile on ASCII platforms even
116 # though the tests are skipped, and fails because 0x89-j is an illegal
117 # range there.
118 $_ = "\x89";
119 eval 'tr/\x{89}-j//d';
120 is($_, "", '"\x89" should match [\x{89}-j]');
121 $_ = "\x8A";
122 eval 'tr/\x{89}-j//d';
123 is($_, "", '"\x8A" should match [\x{89}-j]');
124 $_ = "\x90";
125 eval 'tr/\x{89}-j//d';
126 is($_, "", '"\x90" should match [\x{89}-j]');
127 $_ = "\x91";
128 eval 'tr/\x{89}-j//d';
129 is($_, "", '"\x91" should match [\x{89}-j]');
130}
131
c8e3bb4c
GS
132
133# In EBCDIC 'I' is \xc9 and 'J' is \0xd1, 'i' is \x89 and 'j' is \x91.
134# Yes, discontinuities. Regardless, the \xca in the below should stay
135# untouched (and not became \x8a).
5e037136 136{
5e037136 137 $_ = "I\xcaJ";
c8e3bb4c 138
5e037136 139 tr/I-J/i-j/;
c8e3bb4c 140
ff36f15d 141 is($_, "i\xcaj", 'EBCDIC discontinuity');
5e037136 142}
c8e3bb4c 143#
2de7b02f 144
2de7b02f
GS
145($x = 12) =~ tr/1/3/;
146(my $y = 12) =~ tr/1/3/;
147($f = 1.5) =~ tr/1/3/;
148(my $g = 1.5) =~ tr/1/3/;
953ab6e5
MS
149is($x + $y + $f + $g, 71, 'tr cancels IOK and NOK');
150
bb16bae8
FC
151# /r
152$_ = 'adam';
153is y/dam/ve/rd, 'eve', '/r';
154is $_, 'adam', '/r leaves param alone';
155$g = 'ruby';
156is $g =~ y/bury/repl/r, 'perl', '/r with explicit param';
157is $g, 'ruby', '/r leaves explicit param alone';
158is "aaa" =~ y\a\b\r, 'bbb', '/r with constant param';
159ok !eval '$_ !~ y///r', "!~ y///r is forbidden";
160like $@, qr\^Using !~ with tr///r doesn't make sense\,
161 "!~ y///r error message";
162{
163 my $w;
164 my $wc;
165 local $SIG{__WARN__} = sub { $w = shift; ++$wc };
166 local $^W = 1;
167 eval 'y///r; 1';
168 like $w, qr '^Useless use of non-destructive transliteration \(tr///r\)',
169 '/r warns in void context';
170 is $wc, 1, '/r warns just once';
171}
2de7b02f 172
ee95e30c 173# perlbug [ID 20000511.005 (#3237)]
2de7b02f
GS
174$_ = 'fred';
175/([a-z]{2})/;
176$1 =~ tr/A-Z//;
177s/^(\s*)f/$1F/;
953ab6e5
MS
178is($_, 'Fred', 'harmless if explicitly not updating');
179
180
181# A variant of the above, added in 5.7.2
182$_ = 'fred';
183/([a-z]{2})/;
184eval '$1 =~ tr/A-Z/A-Z/;';
185s/^(\s*)f/$1F/;
186is($_, 'Fred', 'harmless if implicitly not updating');
187is($@, '', ' no error');
188
2de7b02f
GS
189
190# check tr handles UTF8 correctly
191($x = 256.65.258) =~ tr/a/b/;
953ab6e5
MS
192is($x, 256.65.258, 'handles UTF8');
193is(length $x, 3);
194
2de7b02f 195$x =~ tr/A/B/;
953ab6e5 196is(length $x, 3);
83bcbc61 197if ($::IS_ASCII) { # ASCII
953ab6e5 198 is($x, 256.66.258);
67a17885
PP
199}
200else {
953ab6e5 201 is($x, 256.65.258);
67a17885 202}
953ab6e5 203
cbe7f703
PP
204# EBCDIC variants of the above tests
205($x = 256.193.258) =~ tr/a/b/;
953ab6e5
MS
206is(length $x, 3);
207is($x, 256.193.258);
208
cbe7f703 209$x =~ tr/A/B/;
953ab6e5 210is(length $x, 3);
83bcbc61 211if ($::IS_ASCII) { # ASCII
953ab6e5 212 is($x, 256.193.258);
cbe7f703
PP
213}
214else {
953ab6e5 215 is($x, 256.194.258);
cbe7f703 216}
953ab6e5 217
036b4402
GS
218
219{
953ab6e5
MS
220 my $l = chr(300); my $r = chr(400);
221 $x = 200.300.400;
222 $x =~ tr/\x{12c}/\x{190}/;
223 is($x, 200.400.400,
224 'changing UTF8 chars in a UTF8 string, same length');
225 is(length $x, 3);
226
227 $x = 200.300.400;
228 $x =~ tr/\x{12c}/\x{be8}/;
229 is($x, 200.3048.400, ' more bytes');
230 is(length $x, 3);
231
232 $x = 100.125.60;
233 $x =~ tr/\x{64}/\x{190}/;
234 is($x, 400.125.60, 'Putting UT8 chars into a non-UTF8 string');
235 is(length $x, 3);
236
237 $x = 400.125.60;
238 $x =~ tr/\x{190}/\x{64}/;
239 is($x, 100.125.60, 'Removing UTF8 chars from UTF8 string');
240 is(length $x, 3);
241
242 $x = 400.125.60.400;
243 $y = $x =~ tr/\x{190}/\x{190}/;
244 is($y, 2, 'Counting UTF8 chars in UTF8 string');
245
246 $x = 60.400.125.60.400;
247 $y = $x =~ tr/\x{3c}/\x{3c}/;
248 is($y, 2, ' non-UTF8 chars in UTF8 string');
249
250 # 17 - counting UTF8 chars in non-UTF8 string
251 $x = 200.125.60;
252 $y = $x =~ tr/\x{190}/\x{190}/;
253 is($y, 0, ' UTF8 chars in non-UTFs string');
036b4402 254}
c2e66d9e 255
c2e66d9e 256$_ = "abcdefghijklmnopqrstuvwxyz";
953ab6e5
MS
257eval 'tr/a-z-9/ /';
258like($@, qr/^Ambiguous range in transliteration operator/, 'tr/a-z-9//');
c2e66d9e 259
cbe7f703 260# 19-21: Make sure leading and trailing hyphens still work
c2e66d9e
GS
261$_ = "car-rot9";
262tr/-a-m/./;
953ab6e5 263is($_, '..r.rot9', 'hyphens, leading');
c2e66d9e
GS
264
265$_ = "car-rot9";
266tr/a-m-/./;
953ab6e5 267is($_, '..r.rot9', ' trailing');
c2e66d9e
GS
268
269$_ = "car-rot9";
270tr/-a-m-/./;
953ab6e5 271is($_, '..r.rot9', ' both');
c2e66d9e
GS
272
273$_ = "abcdefghijklmnop";
274tr/ae-hn/./;
953ab6e5 275is($_, '.bcd....ijklm.op');
c2e66d9e
GS
276
277$_ = "abcdefghijklmnop";
278tr/a-cf-kn-p/./;
953ab6e5 279is($_, '...de......lm...');
c2e66d9e
GS
280
281$_ = "abcdefghijklmnop";
282tr/a-ceg-ikm-o/./;
953ab6e5
MS
283is($_, '...d.f...j.l...p');
284
c2e66d9e 285
c2e66d9e
GS
286# 20000705 MJD
287eval "tr/m-d/ /";
321ecc04 288like($@, qr/^Invalid range "m-d" in transliteration operator/,
953ab6e5 289 'reversed range check');
c2e66d9e 290
d897a58d 291'abcdef' =~ /(bcd)/;
953ab6e5
MS
292is(eval '$1 =~ tr/abcd//', 3, 'explicit read-only count');
293is($@, '', ' no error');
d897a58d 294
953ab6e5
MS
295'abcdef' =~ /(bcd)/;
296is(eval '$1 =~ tr/abcd/abcd/', 3, 'implicit read-only count');
297is($@, '', ' no error');
298
299is(eval '"123" =~ tr/12//', 2, 'LHS of non-updating tr');
d897a58d 300
94bfe852 301eval '"123" =~ tr/1/2/';
953ab6e5
MS
302like($@, qr|^Can't modify constant item in transliteration \(tr///\)|,
303 'LHS bad on updating tr');
304
d897a58d 305
381d18bc
JH
306# v300 (0x12c) is UTF-8-encoded as 196 172 (0xc4 0xac)
307# v400 (0x190) is UTF-8-encoded as 198 144 (0xc6 0x90)
308
309# Transliterate a byte to a byte, all four ways.
310
311($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/;
953ab6e5 312is($a, v300.197.172.300.197.172, 'byte2byte transliteration');
381d18bc
JH
313
314($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{c5}/;
953ab6e5 315is($a, v300.197.172.300.197.172);
381d18bc
JH
316
317($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\xc5/;
953ab6e5 318is($a, v300.197.172.300.197.172);
381d18bc
JH
319
320($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\x{c5}/;
953ab6e5 321is($a, v300.197.172.300.197.172);
381d18bc 322
381d18bc
JH
323
324($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/;
953ab6e5 325is($a, v300.301.172.300.301.172, 'byte2wide transliteration');
381d18bc
JH
326
327($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc3/;
953ab6e5 328is($a, v195.196.172.195.196.172, ' wide2byte');
381d18bc
JH
329
330($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/;
953ab6e5 331is($a, v301.196.172.301.196.172, ' wide2wide');
381d18bc 332
381d18bc
JH
333
334($a = v300.196.172.300.196.172) =~ tr/\xc4\x{12c}/\x{12d}\xc3/;
953ab6e5 335is($a, v195.301.172.195.301.172, 'byte2wide & wide2byte');
381d18bc 336
381d18bc
JH
337
338($a = v300.196.172.300.196.172.400.198.144) =~
339 tr/\xac\xc4\x{12c}\x{190}/\xad\x{12d}\xc5\x{191}/;
953ab6e5 340is($a, v197.301.173.197.301.173.401.198.144, 'all together now!');
381d18bc 341
381d18bc 342
953ab6e5
MS
343is((($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/), 2,
344 'transliterate and count');
381d18bc 345
953ab6e5 346is((($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/), 2);
381d18bc 347
381d18bc
JH
348
349($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/c;
953ab6e5 350is($a, v301.196.301.301.196.301, 'translit w/complement');
381d18bc
JH
351
352($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc5/c;
953ab6e5 353is($a, v300.197.197.300.197.197);
381d18bc 354
381d18bc
JH
355
356($a = v300.196.172.300.196.172) =~ tr/\xc4//d;
953ab6e5 357is($a, v300.172.300.172, 'translit w/deletion');
381d18bc
JH
358
359($a = v300.196.172.300.196.172) =~ tr/\x{12c}//d;
953ab6e5 360is($a, v196.172.196.172);
381d18bc 361
381d18bc
JH
362
363($a = v196.196.172.300.300.196.172) =~ tr/\xc4/\xc5/s;
953ab6e5 364is($a, v197.172.300.300.197.172, 'translit w/squeeze');
381d18bc
JH
365
366($a = v196.172.300.300.196.172.172) =~ tr/\x{12c}/\x{12d}/s;
953ab6e5 367is($a, v196.172.301.196.172.172);
381d18bc 368
a1874b66 369
953ab6e5 370# Tricky cases (When Simon Cozens Attacks)
a1874b66 371($a = v196.172.200) =~ tr/\x{12c}/a/;
953ab6e5 372is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
373
374($a = v196.172.200) =~ tr/\x{12c}/\x{12c}/;
953ab6e5 375is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
376
377($a = v196.172.200) =~ tr/\x{12c}//d;
953ab6e5
MS
378is(sprintf("%vd", $a), '196.172.200');
379
a1874b66 380
8973db79 381# UTF8 range tests from Inaba Hiroto
f9a63242
JH
382
383($a = v300.196.172.302.197.172) =~ tr/\x{12c}-\x{130}/\xc0-\xc4/;
953ab6e5 384is($a, v192.196.172.194.197.172, 'UTF range');
f9a63242
JH
385
386($a = v300.196.172.302.197.172) =~ tr/\xc4-\xc8/\x{12c}-\x{130}/;
953ab6e5
MS
387is($a, v300.300.172.302.301.172);
388
8973db79
JH
389
390# UTF8 range tests from Karsten Sperling (patch #9008 required)
391
392($a = "\x{0100}") =~ tr/\x00-\x{100}/X/;
953ab6e5 393is($a, "X");
8973db79
JH
394
395($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}/X/c;
953ab6e5 396is($a, "X");
8973db79
JH
397
398($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5 399is($a, "X");
8973db79
JH
400
401($a = v256) =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5
MS
402is($a, "X");
403
8973db79 404
94472101
JH
405# UTF8 range tests from Inaba Hiroto
406
407($a = "\x{200}") =~ tr/\x00-\x{100}/X/c;
953ab6e5 408is($a, "X");
94472101
JH
409
410($a = "\x{200}") =~ tr/\x00-\x{100}/X/cs;
953ab6e5
MS
411is($a, "X");
412
685b06b5
KW
413# Tricky on EBCDIC: while [a-z] [A-Z] must not match the gap characters (as
414# well as i-j, r-s, I-J, R-S), [\x89-\x91] [\xc9-\xd1] has to match them,
6b6bd37b
JH
415# from Karsten Sperling.
416
417$c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/\x89-\x91/X/;
953ab6e5
MS
418is($c, 8);
419is($a, "XXXXXXXX");
4c3a8340 420
6b6bd37b 421$c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/\xc9-\xd1/X/;
953ab6e5
MS
422is($c, 8);
423is($a, "XXXXXXXX");
6b6bd37b 424
4c3a8340 425SKIP: {
f4240379 426 skip "EBCDIC-centric tests", 4 unless $::IS_EBCDIC;
953ab6e5
MS
427
428 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/i-j/X/;
429 is($c, 2);
430 is($a, "X\x8a\x8b\x8c\x8d\x8f\x90X");
431
432 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/I-J/X/;
433 is($c, 2);
434 is($a, "X\xca\xcb\xcc\xcd\xcf\xd0X");
6b6bd37b 435}
1ed601ec
JH
436
437($a = "\x{100}") =~ tr/\x00-\xff/X/c;
953ab6e5 438is(ord($a), ord("X"));
1ed601ec
JH
439
440($a = "\x{100}") =~ tr/\x00-\xff/X/cs;
953ab6e5 441is(ord($a), ord("X"));
1ed601ec
JH
442
443($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//c;
953ab6e5 444is($a, "\x{100}\x{100}");
1ed601ec
JH
445
446($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//cs;
953ab6e5 447is($a, "\x{100}");
1ed601ec 448
629b4584 449$a = "\xfe\xff"; $a =~ tr/\xfe\xff/\x{1ff}\x{1fe}/;
953ab6e5
MS
450is($a, "\x{1ff}\x{1fe}");
451
76ef7183
JH
452
453# From David Dyck
454($a = "R0_001") =~ tr/R_//d;
953ab6e5 455is(hex($a), 1);
76ef7183 456
800b4dc4
JH
457# From Inaba Hiroto
458@a = (1,2); map { y/1/./ for $_ } @a;
953ab6e5 459is("@a", ". 2");
800b4dc4
JH
460
461@a = (1,2); map { y/1/./ for $_.'' } @a;
953ab6e5
MS
462is("@a", "1 2");
463
800b4dc4 464
bec89253
RH
465# Additional test for Inaba Hiroto patch (robin@kitsite.com)
466($a = "\x{100}\x{102}\x{101}") =~ tr/\x00-\377/XYZ/c;
953ab6e5
MS
467is($a, "XZY");
468
bec89253 469
2233f375
NC
470# Used to fail with "Modification of a read-only value attempted"
471%a = (N=>1);
472foreach (keys %a) {
953ab6e5
MS
473 eval 'tr/N/n/';
474 is($_, 'n', 'pp_trans needs to unshare shared hash keys');
475 is($@, '', ' no error');
2233f375 476}
94bfe852 477
953ab6e5 478
94bfe852 479$x = eval '"1213" =~ tr/1/1/';
953ab6e5
MS
480is($x, 2, 'implicit count on constant');
481is($@, '', ' no error');
482
483
484my @foo = ();
485eval '$foo[-1] =~ tr/N/N/';
486is( $@, '', 'implicit count outside array bounds, index negative' );
487is( scalar @foo, 0, " doesn't extend the array");
488
489eval '$foo[1] =~ tr/N/N/';
490is( $@, '', 'implicit count outside array bounds, index positive' );
491is( scalar @foo, 0, " doesn't extend the array");
492
493
494my %foo = ();
495eval '$foo{bar} =~ tr/N/N/';
496is( $@, '', 'implicit count outside hash bounds' );
497is( scalar keys %foo, 0, " doesn't extend the hash");
d59e14db
RGS
498
499$x = \"foo";
500is( $x =~ tr/A/A/, 2, 'non-modifying tr/// on a scalar ref' );
501is( ref $x, 'SCALAR', " doesn't stringify its argument" );
0d65d7d5
MS
502
503# rt.perl.org 36622. Perl didn't like a y/// at end of file. No trailing
504# newline allowed.
2a91eb11 505fresh_perl_is(q[$_ = "foo"; y/A-Z/a-z/], '', {}, 'RT #36622 y/// at end of file');
9f7f3913
TS
506
507
508{ # [perl #38293] chr(65535) should be allowed in regexes
509no warnings 'utf8'; # to allow non-characters
510
511$s = "\x{d800}\x{ffff}";
512$s =~ tr/\0/A/;
513is($s, "\x{d800}\x{ffff}", "do_trans_simple");
514
515$s = "\x{d800}\x{ffff}";
516$i = $s =~ tr/\0//;
517is($i, 0, "do_trans_count");
518
519$s = "\x{d800}\x{ffff}";
520$s =~ tr/\0/A/s;
521is($s, "\x{d800}\x{ffff}", "do_trans_complex, SQUASH");
522
523$s = "\x{d800}\x{ffff}";
524$s =~ tr/\0/A/c;
525is($s, "AA", "do_trans_complex, COMPLEMENT");
526
527$s = "A\x{ffff}B";
528$s =~ tr/\x{ffff}/\x{1ffff}/;
529is($s, "A\x{1ffff}B", "utf8, SEARCHLIST");
530
531$s = "\x{fffd}\x{fffe}\x{ffff}";
532$s =~ tr/\x{fffd}-\x{ffff}/ABC/;
533is($s, "ABC", "utf8, SEARCHLIST range");
534
535$s = "ABC";
536$s =~ tr/ABC/\x{ffff}/;
537is($s, "\x{ffff}"x3, "utf8, REPLACEMENTLIST");
538
539$s = "ABC";
540$s =~ tr/ABC/\x{fffd}-\x{ffff}/;
541is($s, "\x{fffd}\x{fffe}\x{ffff}", "utf8, REPLACEMENTLIST range");
542
543$s = "A\x{ffff}B\x{100}\0\x{fffe}\x{ffff}";
544$i = $s =~ tr/\x{ffff}//;
545is($i, 2, "utf8, count");
546
547$s = "A\x{ffff}\x{ffff}C";
548$s =~ tr/\x{ffff}/\x{100}/s;
549is($s, "A\x{100}C", "utf8, SQUASH");
550
551$s = "A\x{ffff}\x{ffff}\x{fffe}\x{fffe}\x{fffe}C";
552$s =~ tr/\x{fffe}\x{ffff}//s;
553is($s, "A\x{ffff}\x{fffe}C", "utf8, SQUASH");
554
555$s = "xAABBBy";
556$s =~ tr/AB/\x{ffff}/s;
557is($s, "x\x{ffff}y", "utf8, SQUASH");
558
559$s = "xAABBBy";
560$s =~ tr/AB/\x{fffe}\x{ffff}/s;
561is($s, "x\x{fffe}\x{ffff}y", "utf8, SQUASH");
562
563$s = "A\x{ffff}B\x{fffe}C";
564$s =~ tr/\x{fffe}\x{ffff}/x/c;
565is($s, "x\x{ffff}x\x{fffe}x", "utf8, COMPLEMENT");
566
567$s = "A\x{10000}B\x{2abcd}C";
568$s =~ tr/\0-\x{ffff}/x/c;
569is($s, "AxBxC", "utf8, COMPLEMENT range");
570
571$s = "A\x{fffe}B\x{ffff}C";
572$s =~ tr/\x{fffe}\x{ffff}/x/d;
573is($s, "AxBC", "utf8, DELETE");
574
575} # non-characters end
576
1749ea0d
TS
577{ # related to [perl #27940]
578 my $c;
579
580 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ tr/\c@-\c_//d;
581 is($c, "\x20\x30\x40\x50\x60", "tr/\\c\@-\\c_//d");
582
583 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ tr/\x00-\x1f//d;
584 is($c, "\x20\x30\x40\x50\x60", "tr/\\x00-\\x1f//d");
585}
586
3788ef8f 587($s) = keys %{{pie => 3}};
3e89ba19 588SKIP: {
e3918bb7
FC
589 if (!eval { require XS::APItest }) { skip "no XS::APItest", 2 }
590 my $wasro = XS::APItest::SvIsCOW($s);
2203fb5a 591 ok $wasro, "have a COW";
3788ef8f 592 $s =~ tr/i//;
e3918bb7 593 ok( XS::APItest::SvIsCOW($s),
3e89ba19 594 "count-only tr doesn't deCOW COWs" );
3788ef8f 595}
a5446a64
DM
596
597# [ RT #61520 ]
598#
599# under threads, unicode tr within a cloned closure would SEGV or assert
600# fail, since the pointer in the pad to the swash was getting zeroed out
601# in the proto-CV
602
603{
604 my $x = "\x{142}";
605 sub {
606 $x =~ tr[\x{142}][\x{143}];
607 }->();
608 is($x,"\x{143}", "utf8 + closure");
609}
610
9100eeb1
Z
611# Freeing of trans ops prior to pmtrans() [perl #102858].
612eval q{ $a ~= tr/a/b/; };
613ok 1;
614SKIP: {
55673181 615 no warnings "deprecated";
9100eeb1
Z
616 skip "no encoding", 1 unless eval { require encoding; 1 };
617 eval q{ use encoding "utf8"; $a ~= tr/a/b/; };
618 ok 1;
619}
a5446a64 620
cb6d3474
KW
621{ # [perl #113584]
622
623 my $x = "Perlα";
624 $x =~ tr/αα/βγ/;
baacc348 625 { no warnings 'utf8'; print "# $x\n"; } # No note() to avoid wide warning.
cb6d3474
KW
626 is($x, "Perlβ", "Only first of multiple transliterations is used");
627}
628
d8b516a1
FC
629# tr/a/b/ should fail even on zero-length read-only strings
630use constant nullrocow => (keys%{{""=>undef}})[0];
631for ("", nullrocow) {
632 eval { $_ =~ y/a/b/ };
633 like $@, qr/^Modification of a read-only value attempted at /,
634 'tr/a/b/ fails on zero-length ro string';
635}
636
a53bfdae
HS
637# Whether they're permitted or not, non-modifying tr/// should not write
638# to read-only values, even with funky flags.
639{ # [perl #123759]
640 eval q{ ('a' =~ /./) =~ tr///d };
641 ok(1, "tr///d on PL_Yes does not assert");
642 eval q{ ('a' =~ /./) =~ tr/a-z/a-z/d };
643 ok(1, "tr/a-z/a-z/d on PL_Yes does not assert");
644 eval q{ ('a' =~ /./) =~ tr///s };
645 ok(1, "tr///s on PL_Yes does not assert");
646 eval q{ *x =~ tr///d };
647 ok(1, "tr///d on glob does not assert");
648}
649
3a34ca0b 650{ # [perl #128734
ce4eed6e 651 my $string = chr utf8::unicode_to_native(0x00e0);
3a34ca0b
KW
652 $string =~ tr/\N{U+00e0}/A/;
653 is($string, "A", 'tr// of \N{U+...} works for upper-Latin1');
ce4eed6e 654 my $string = chr utf8::unicode_to_native(0x00e1);
3a34ca0b
KW
655 $string =~ tr/\N{LATIN SMALL LETTER A WITH ACUTE}/A/;
656 is($string, "A", 'tr// of \N{name} works for upper-Latin1');
657}
658
9100eeb1 6591;