6 require Config; import Config;
12 # Stolen from re/ReTest.pl. Can't just use the file since it doesn't support
13 # like() and it conflicts with test.pl
15 my ($code, $pattern, $name) = @_;
17 local $SIG {__WARN__} = sub {$w .= join "" => @_};
19 ref $code ? &$code : eval $code;
20 my $r = $w && $w =~ /$pattern/;
22 ok( $r, $name // "Got warning /$pattern/", $r ? undef :
23 "# expected: /$pattern/\n" .
29 ok( $_ eq 'david' && $a eq 'rules', 'non-destructive substitute' );
31 $a = "david" =~ s/david/rules/r;
32 ok( $a eq 'rules', 's///r with constant' );
34 $a = "david" =~ s/david/"is"."great"/er;
35 ok( $a eq 'isgreat', 's///er' );
37 $a = "daviddavid" =~ s/david/cool/gr;
38 ok( $a eq 'coolcool', 's///gr' );
41 $b = $a =~ s/david/sucks/r =~ s/sucks/rules/r;
42 ok( $a eq 'david' && $b eq 'rules', 'chained s///r' );
45 $b = $a =~ s/xxx/sucks/r;
46 ok( $a eq 'david' && $b eq 'david', 'non matching s///r' );
50 ok( 'david' =~ s/$a/rules/ro eq 'rules', 's///ro '.$_ );
54 eval '$b = $a !~ s/david/is great/r';
55 like( $@, qr{Using !~ with s///r doesn't make sense}, 's///r !~ operator gives error' );
58 no warnings 'uninitialized';
60 $b = $a =~ s/left/right/r;
61 ok ( !defined $a && !defined $b, 's///r with undef input' );
64 must_warn sub { $b = $a =~ s/left/right/r }, '^Use of uninitialized value', 's///r Uninitialized warning';
67 must_warn 's/david/sucks/r; 1', '^Useless use of non-destructive substitution', 's///r void context warning';
71 $b = $a =~ s/david/rules/r;
72 ok( $a eq '' && $b eq '', 's///r on empty string' );
76 ok( $_ eq 'david' && $b[0] eq 'rules', 's///r in list context' );
78 # Magic value and s///r
80 tie $m, 'Tie::StdScalar'; # makes $a magical
82 $b = $m =~ s/david/rules/r;
83 ok( $m eq 'david' && $b eq 'rules', 's///r with magic input' );
85 $m = $b =~ s/rules/david/r;
86 ok( defined tied($m), 's///r magic isn\'t lost' );
88 $b = $m =~ s/xxx/yyy/r;
89 ok( ! defined tied($b), 's///r magic isn\'t contagious' );
91 my $ref = \("aaa" =~ s/aaa/bbb/r);
92 is (Internals::SvREFCNT($$ref), 1, 's///r does not leak');
93 $ref = \("aaa" =~ s/aaa/bbb/rg);
94 is (Internals::SvREFCNT($$ref), 1, 's///rg does not leak');
99 ok( $_ eq '$x', ":$_: eq :\$x:" );
103 ok( $_ eq 'foo', ":$_: eq :foo:" );
107 ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
110 ($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
111 ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
114 ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
116 ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
118 ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
121 ok( /a/i && s///gi && $_ eq 'BCD' );
124 ok( length($_) == 4 );
126 ok( $_ eq '\\' x 8 && $snum == 4 );
129 ok( length($_) == 8 );
131 ok( $_ eq '\\//' x 4 && $snum == 4 );
132 ok( length($_) == 12 );
136 ok( $_ eq 'aaXXXXbbb' );
140 ok( $_ eq 'aaXXXXbbb' );
144 ok( $_ eq 'baaXXXXbbb' );
148 ok( $_ eq 'baaXXXXbbb' );
152 ok( $_ eq 'aXXXXbbb' );
156 ok( $_ eq 'baXXXXbbb' );
160 ok( $_ eq 'aaaXXXXbb' );
164 ok( $_ eq 'aaaXXXXbb' );
168 ok( $_ eq 'aaaXXXXb' );
172 ok( $_ eq 'aayXXXbbb' );
176 ok( $_ eq 'aaaXXXzbb' );
184 ok( $_ eq 'aaaXXXXxb' );
186 # now for some unoptimized versions of the same.
190 ok( $_ eq 'aaXXXXbbb' );
194 ok( $_ eq 'aaXXXXbbb' );
198 ok( $_ eq 'baaXXXXbbb' );
202 ok( $_ eq 'baaXXXXbbb' );
206 ok( $_ eq 'aXXXXbbb' );
210 ok( $_ eq 'baXXXXbbb' );
214 ok( $_ eq 'aaaXXXXbb' );
218 ok( $_ eq 'aaaXXXXbb' );
222 ok( $_ eq 'aaaXXXXb' );
226 ok( $_ eq 'aayXXXbbb' );
230 ok( $_ eq 'aaaXXXzbb' );
233 $x ne $x || s/aaX.*Xbb//;
238 ok( $_ eq 'aaaXXXXxb' );
241 s/(\d+)/$1*2/e; # yields 'abc246xyz'
242 ok( $_ eq 'abc246xyz' );
243 s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
244 ok( $_ eq 'abc 246xyz' );
245 s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
246 ok( $_ eq 'aabbcc 224466xxyyzz' );
257 $_ = "Now is the %#*! time for all good men...";
258 ok( ($x=(y/a-zA-Z //cd)) == 7 );
261 $_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
264 ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
266 # same as tr/A-Z/a-z/;
267 if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
269 y[\301-\351][\201-\251];
270 } else { # Ye Olde ASCII. Or something like it.
271 y[\101-\132][\141-\172];
274 ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
277 skip("not ASCII",1) unless (ord("+") == ord(",") - 1
278 && ord(",") == ord("-") - 1
279 && ord("a") == ord("b") - 1
280 && ord("b") == ord("c") - 1);
295 # test recursive substitutions
296 # code based on the recursive expansion of makefile variables
299 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
300 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
301 DIR => '$(UNDEFINEDNAME)/xxx',
304 my($var,$level) = @_;
305 return "\$($var)" unless exists $MK{$var};
306 return exp_vars($MK{$var}, $level+1); # can recurse
309 my($str,$level) = @_;
310 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
311 #warn "exp_vars $level = '$str'\n";
315 ok( exp_vars('$(AAAAA)',0) eq 'D' );
316 ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
317 ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
318 ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
321 s/(..)/$x = $1, m#.#/eg;
322 ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
324 # Subst and lookbehind
327 $snum = s/(?<!x)c/x/g;
328 ok( $_ eq "xxxxx" && $snum == 5 );
331 $snum = s/(?<!x)(c)/x/g;
332 ok( $_ eq "xxxxx" && $snum == 5 );
335 $snum = s/(?<!r)foobbar/foobar/g;
336 ok( $_ eq "foobarfoobbar" && $snum == 1 );
339 $snum = s/(?<!ar)(foobbar)/foobar/g;
340 ok( $_ eq "foobarfoobbar" && $snum == 1 );
343 $snum = s/(?<!ar)foobbar/foobar/g;
344 ok( $_ eq "foobarfoobbar" && $snum == 1 );
346 eval 's{foo} # this is a comment, not a delimiter
348 ok( ! @?, 'parsing of split subst with comment' );
350 $snum = eval '$_="exactly"; s sxsys;m 3(yactl)3;$1';
351 is( $snum, 'yactl', 'alpha delimiters are allowed' );
355 ok( $_ eq "bbcbb" && $snum == 4,
356 'check if squashing works at the end of string' );
362 $url = new URI::URL "http://www/"; die if $url eq "xXx";
366 $foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
367 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
368 ' lowercase $@%#MiXeD$@%# ';
371 s{ \d+ \b [,.;]? (?{ 'digits' })
373 [a-z]+ \b [,.;]? (?{ 'lowercase' })
375 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
377 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
379 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
381 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
385 [^A-Za-z0-9\s]+ (?{ '$@%#' })
392 ok( $_ eq '' && $snum == 6 );
395 $snum = s/(\d*|x)/<$1>/g;
396 $foo = '<>' . ('<x><>' x 20) ;
397 ok( $_ eq $foo && $snum == 41 );
404 ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
409 ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
414 ok( $_ eq 'aaaaaaxxaa' );
419 ok( $_ eq 'aaaaaaxaa' );
423 ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
427 ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
431 ok( $_ eq 'xxaaaaaaaa' );
435 ok( $_ eq 'xaaaaaaaa' );
439 ok( $_ eq '.aaa' && $snum == 1 );
441 eval q% s/a/"b"}/e %;
442 ok( $@ =~ /Bad evalled substitution/ );
443 eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
444 ok( $_ eq "x " and !length $@ );
446 eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
447 ok( $_ eq '' and !length $@ );
450 ok( !s/^([a-z]:)/\u$1/ );
452 $_ = "Charles Bronson";
454 ok( $_ eq "C B" && $snum == 12 );
458 my $s = "H\303\266he";
462 is($l, $r, "use utf8 \\w");
465 my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
466 $pv1 =~ s/A/\x{100}/;
467 substr($pv2,0,1) = "\x{100}";
471 skip("EBCDIC", 3) if ord("A") == 193;
474 # Gregor Chrupala <gregor.chrupala@star-group.net>
476 $a = 'España';
478 like($a, qr/ñ/, "use utf8 RHS");
483 $a = 'España España';
485 like($a, qr/ñ/, "use utf8 LHS");
492 like($a, qr/ñ/, "use utf8 LHS and RHS");
497 # SADAHIRO Tomoyuki <bqw10602@nifty.com>
499 $a = "\x{100}\x{101}";
500 $a =~ s/\x{101}/\xFF/;
502 is(length($a), 2, "SADAHIRO utf8 s///");
504 $a = "\x{100}\x{101}";
505 $a =~ s/\x{101}/"\xFF"/e;
509 $a = "\x{100}\x{101}";
510 $a =~ s/\x{101}/\xFF\xFF\xFF/;
511 like($a, qr/\xFF\xFF\xFF/);
514 $a = "\x{100}\x{101}";
515 $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
516 like($a, qr/\xFF\xFF\xFF/);
520 $a =~ s/\xFF/\x{100}/;
521 like($a, qr/\x{100}/);
525 $a =~ s/\xFF/"\x{100}"/e;
526 like($a, qr/\x{100}/);
530 $a =~ s/\xFF/\x{100}/;
531 like($a, qr/\x{100}/);
535 $a =~ s/\xFF/"\x{100}"/e;
536 like($a, qr/\x{100}/);
541 # subst with mixed utf8/non-utf8 type
542 my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
543 my($na, $nb) = ("\x{ff}", "\x{fe}");
546 ($b = $a) =~ s/--/$na/;
547 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
548 ($b = $a) =~ s/--/--$na--/;
549 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
550 ($b = $a) =~ s/--/$uc/;
551 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
552 ($b = $a) =~ s/--/--$uc--/;
553 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
555 ($b = $a) =~ s/--/$ua/;
556 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
557 ($b = $a) =~ s/--/--$ua--/;
558 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
560 # now with utf8 pattern
562 ($b = $a) =~ s/-($ud)?-/$na/;
563 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
564 ($b = $a) =~ s/-($ud)?-/--$na--/;
565 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
566 ($b = $a) =~ s/-($ud)?-/$uc/;
567 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
568 ($b = $a) =~ s/-($ud)?-/--$uc--/;
569 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
571 ($b = $a) =~ s/-($ud)?-/$ua/;
572 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
573 ($b = $a) =~ s/-($ud)?-/--$ua--/;
574 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
575 ($b = $a) =~ s/-($ud)?-/$na/;
576 is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
577 ($b = $a) =~ s/-($ud)?-/--$na--/;
578 is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
584 is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
588 is("<$_> <$s>", "<> <4>", "[perl #7806]");
590 # [perl #19048] Coredump in silly replacement
595 is($_, "\n", "[perl #19048]");
598 # [perl #17757] interaction between saw_ampersand and study
600 my $f = eval q{ $& };
604 is($f, "yy", "[perl #17757]");
607 # [perl #20684] returned a zero count
609 is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
611 # [perl #20682] @- not visible in replacement
613 /(2)/; # seed @- with something else
614 s/(1)(2)(3)/$#- (@-)/;
615 is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
617 # [perl #20682] $^N not visible in replacement
619 /(a)/; s/(b)|(c)/-$^N/g;
620 is($_,'a-b-c','#20682 $^N not visible in replacement');
622 # [perl #22351] perl bug with 'e' substitution modifier
625 no warnings 'uninitialized';
628 is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
631 # [perl #34171] $1 didn't honour 'use bytes' in s//e
639 is(length($x), 2, '[perl #34171]');
643 { # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
646 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
647 is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
649 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
650 is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
654 no warnings 'uninitialized';
655 /(((((((((x)))))))))(z)/; # clear $10
656 s/(((((((((x)))))))))(y)/${10}/;
657 is($_,"y","RT#6006: \$_ eq '$_'");
659 s/(((((((((x)))))))))(r)/fooba${10}/;
660 is($_,"foobar","RT#6006: \$_ eq '$_'");
663 my $want=("\n" x 11).("B\n" x 11)."B";
670 is($want,$_,"RT#17542");
674 my @tests = ('ABC', "\xA3\xA4\xA5", "\x{410}\x{411}\x{412}");
678 is($_, "012", "RT#52104: $id");
682 fresh_perl_is( '$_=q(foo);s/(.)\G//g;print' => 'foo', '[perl #69056] positive GPOS regex segfault' );
683 fresh_perl_is( '$_="abcef"; s/bc|(.)\G(.)/$1 ? "[$1-$2]" : "XX"/ge; print' => 'aXX[c-e][e-f]f', 'positive GPOS regex substitution failure' );
685 # [perl #71470] $var =~ s/$qr//e calling get-magic on $_ as well as $var
689 sub qrBug::TIESCALAR { bless[pop], 'qrBug' }
690 sub qrBug::FETCH { $scratch .= "[fetching $_[0][0]]"; 'prew' }
692 tie my $kror, qrBug => '$kror';
693 tie $_, qrBug => '$_';
697 $scratch, '[fetching $kror]',
698 'bug: $var =~ s/$qr//e calling get-magic on $_ as well as $var',
702 { # Bug #41530; replacing non-utf8 with a utf8 causes problems
703 my $string = "a\x{a0}a";
704 my $sub_string = $string;
705 ok(! utf8::is_utf8($sub_string), "Verify that string isn't initially utf8");
706 $sub_string =~ s/a/\x{100}/g;
707 ok(utf8::is_utf8($sub_string),
708 'Verify replace of non-utf8 with utf8 upgrades to utf8');
709 is($sub_string, "\x{100}\x{A0}\x{100}",
710 'Verify #41530 fixed: replace of non-utf8 with utf8');
712 my $non_sub_string = $string;
713 ok(! utf8::is_utf8($non_sub_string),
714 "Verify that string isn't initially utf8");
715 $non_sub_string =~ s/b/\x{100}/g;
716 ok(! utf8::is_utf8($non_sub_string),
717 "Verify that failed substitute doesn't change string's utf8ness");
718 is($non_sub_string, $string,
719 "Verify that failed substitute doesn't change string");
722 { # Verify largish octal in replacement pattern
725 $string =~ s/a/\400/;
726 is($string, chr 0x100, "Verify that handles s/foo/\\400/");
727 $string =~ s/./\600/;
728 is($string, chr 0x180, "Verify that handles s/foo/\\600/");
729 $string =~ s/./\777/;
730 is($string, chr 0x1FF, "Verify that handles s/foo/\\777/");
733 # Scoping of s//the RHS/ when there is no /e
734 # Tests based on [perl #19078]
737 my $output = ''; my %a;
738 no warnings 'uninitialized';
741 s!.!<@a{$output .= ("$&"),/[$&]/g}>!g;
744 $output, "CCCGGG< >< >< >< >< >< >",
745 's/// sets PL_curpm for each iteration even when the RHS has set it'
751 'Match vars reflect the last match after s/pat/$a{m|pat|}/ without /e'
756 # a tied scalar that returned a plain string, got messed up
757 # when substituted with a UTF8 replacement string, due to
758 # magic getting called multiple times, and pointers now pointing
759 # to stale/freed strings
762 sub TIESCALAR { bless [ "abcdefgh" ] }
763 sub FETCH { $fc++; $_[0][0] }
764 sub STORE { $_[0][0] = $_[1] }
768 $s =~ s/..../\x{101}/;
769 ::is($fc, 1, "tied UTF8 stuff FETCH count");
770 ::is("$s", "\x{101}efgh", "tied UTF8 stuff");