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' );
94 ok( $_ eq '$x', ":$_: eq :\$x:" );
98 ok( $_ eq 'foo', ":$_: eq :foo:" );
102 ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
105 ($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
106 ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
109 ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
111 ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
113 ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
116 ok( /a/i && s///gi && $_ eq 'BCD' );
119 ok( length($_) == 4 );
121 ok( $_ eq '\\' x 8 && $snum == 4 );
124 ok( length($_) == 8 );
126 ok( $_ eq '\\//' x 4 && $snum == 4 );
127 ok( length($_) == 12 );
131 ok( $_ eq 'aaXXXXbbb' );
135 ok( $_ eq 'aaXXXXbbb' );
139 ok( $_ eq 'baaXXXXbbb' );
143 ok( $_ eq 'baaXXXXbbb' );
147 ok( $_ eq 'aXXXXbbb' );
151 ok( $_ eq 'baXXXXbbb' );
155 ok( $_ eq 'aaaXXXXbb' );
159 ok( $_ eq 'aaaXXXXbb' );
163 ok( $_ eq 'aaaXXXXb' );
167 ok( $_ eq 'aayXXXbbb' );
171 ok( $_ eq 'aaaXXXzbb' );
179 ok( $_ eq 'aaaXXXXxb' );
181 # now for some unoptimized versions of the same.
185 ok( $_ eq 'aaXXXXbbb' );
189 ok( $_ eq 'aaXXXXbbb' );
193 ok( $_ eq 'baaXXXXbbb' );
197 ok( $_ eq 'baaXXXXbbb' );
201 ok( $_ eq 'aXXXXbbb' );
205 ok( $_ eq 'baXXXXbbb' );
209 ok( $_ eq 'aaaXXXXbb' );
213 ok( $_ eq 'aaaXXXXbb' );
217 ok( $_ eq 'aaaXXXXb' );
221 ok( $_ eq 'aayXXXbbb' );
225 ok( $_ eq 'aaaXXXzbb' );
228 $x ne $x || s/aaX.*Xbb//;
233 ok( $_ eq 'aaaXXXXxb' );
236 s/(\d+)/$1*2/e; # yields 'abc246xyz'
237 ok( $_ eq 'abc246xyz' );
238 s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
239 ok( $_ eq 'abc 246xyz' );
240 s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
241 ok( $_ eq 'aabbcc 224466xxyyzz' );
252 $_ = "Now is the %#*! time for all good men...";
253 ok( ($x=(y/a-zA-Z //cd)) == 7 );
256 $_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
259 ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
261 # same as tr/A-Z/a-z/;
262 if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
264 y[\301-\351][\201-\251];
265 } else { # Ye Olde ASCII. Or something like it.
266 y[\101-\132][\141-\172];
269 ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
272 skip("not ASCII",1) unless (ord("+") == ord(",") - 1
273 && ord(",") == ord("-") - 1
274 && ord("a") == ord("b") - 1
275 && ord("b") == ord("c") - 1);
290 # test recursive substitutions
291 # code based on the recursive expansion of makefile variables
294 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
295 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
296 DIR => '$(UNDEFINEDNAME)/xxx',
299 my($var,$level) = @_;
300 return "\$($var)" unless exists $MK{$var};
301 return exp_vars($MK{$var}, $level+1); # can recurse
304 my($str,$level) = @_;
305 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
306 #warn "exp_vars $level = '$str'\n";
310 ok( exp_vars('$(AAAAA)',0) eq 'D' );
311 ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
312 ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
313 ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
316 s/(..)/$x = $1, m#.#/eg;
317 ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
319 # Subst and lookbehind
322 $snum = s/(?<!x)c/x/g;
323 ok( $_ eq "xxxxx" && $snum == 5 );
326 $snum = s/(?<!x)(c)/x/g;
327 ok( $_ eq "xxxxx" && $snum == 5 );
330 $snum = s/(?<!r)foobbar/foobar/g;
331 ok( $_ eq "foobarfoobbar" && $snum == 1 );
334 $snum = s/(?<!ar)(foobbar)/foobar/g;
335 ok( $_ eq "foobarfoobbar" && $snum == 1 );
338 $snum = s/(?<!ar)foobbar/foobar/g;
339 ok( $_ eq "foobarfoobbar" && $snum == 1 );
341 eval 's{foo} # this is a comment, not a delimiter
343 ok( ! @?, 'parsing of split subst with comment' );
345 $snum = eval '$_="exactly"; s sxsys;m 3(yactl)3;$1';
346 is( $snum, 'yactl', 'alpha delimiters are allowed' );
350 ok( $_ eq "bbcbb" && $snum == 4,
351 'check if squashing works at the end of string' );
357 $url = new URI::URL "http://www/"; die if $url eq "xXx";
361 $foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
362 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
363 ' lowercase $@%#MiXeD$@%# ';
366 s{ \d+ \b [,.;]? (?{ 'digits' })
368 [a-z]+ \b [,.;]? (?{ 'lowercase' })
370 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
372 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
374 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
376 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
380 [^A-Za-z0-9\s]+ (?{ '$@%#' })
387 ok( $_ eq '' && $snum == 6 );
390 $snum = s/(\d*|x)/<$1>/g;
391 $foo = '<>' . ('<x><>' x 20) ;
392 ok( $_ eq $foo && $snum == 41 );
399 ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
404 ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
409 ok( $_ eq 'aaaaaaxxaa' );
414 ok( $_ eq 'aaaaaaxaa' );
418 ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
422 ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
426 ok( $_ eq 'xxaaaaaaaa' );
430 ok( $_ eq 'xaaaaaaaa' );
434 ok( $_ eq '.aaa' && $snum == 1 );
436 eval q% s/a/"b"}/e %;
437 ok( $@ =~ /Bad evalled substitution/ );
438 eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
439 ok( $_ eq "x " and !length $@ );
441 eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
442 ok( $_ eq '' and !length $@ );
445 ok( !s/^([a-z]:)/\u$1/ );
447 $_ = "Charles Bronson";
449 ok( $_ eq "C B" && $snum == 12 );
453 my $s = "H\303\266he";
457 is($l, $r, "use utf8 \\w");
460 my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
461 $pv1 =~ s/A/\x{100}/;
462 substr($pv2,0,1) = "\x{100}";
466 skip("EBCDIC", 3) if ord("A") == 193;
469 # Gregor Chrupala <gregor.chrupala@star-group.net>
471 $a = 'España';
473 like($a, qr/ñ/, "use utf8 RHS");
478 $a = 'España España';
480 like($a, qr/ñ/, "use utf8 LHS");
487 like($a, qr/ñ/, "use utf8 LHS and RHS");
492 # SADAHIRO Tomoyuki <bqw10602@nifty.com>
494 $a = "\x{100}\x{101}";
495 $a =~ s/\x{101}/\xFF/;
497 is(length($a), 2, "SADAHIRO utf8 s///");
499 $a = "\x{100}\x{101}";
500 $a =~ s/\x{101}/"\xFF"/e;
504 $a = "\x{100}\x{101}";
505 $a =~ s/\x{101}/\xFF\xFF\xFF/;
506 like($a, qr/\xFF\xFF\xFF/);
509 $a = "\x{100}\x{101}";
510 $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
511 like($a, qr/\xFF\xFF\xFF/);
515 $a =~ s/\xFF/\x{100}/;
516 like($a, qr/\x{100}/);
520 $a =~ s/\xFF/"\x{100}"/e;
521 like($a, qr/\x{100}/);
525 $a =~ s/\xFF/\x{100}/;
526 like($a, qr/\x{100}/);
530 $a =~ s/\xFF/"\x{100}"/e;
531 like($a, qr/\x{100}/);
536 # subst with mixed utf8/non-utf8 type
537 my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
538 my($na, $nb) = ("\x{ff}", "\x{fe}");
541 ($b = $a) =~ s/--/$na/;
542 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
543 ($b = $a) =~ s/--/--$na--/;
544 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
545 ($b = $a) =~ s/--/$uc/;
546 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
547 ($b = $a) =~ s/--/--$uc--/;
548 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
550 ($b = $a) =~ s/--/$ua/;
551 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
552 ($b = $a) =~ s/--/--$ua--/;
553 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
555 # now with utf8 pattern
557 ($b = $a) =~ s/-($ud)?-/$na/;
558 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
559 ($b = $a) =~ s/-($ud)?-/--$na--/;
560 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
561 ($b = $a) =~ s/-($ud)?-/$uc/;
562 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
563 ($b = $a) =~ s/-($ud)?-/--$uc--/;
564 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
566 ($b = $a) =~ s/-($ud)?-/$ua/;
567 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
568 ($b = $a) =~ s/-($ud)?-/--$ua--/;
569 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
570 ($b = $a) =~ s/-($ud)?-/$na/;
571 is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
572 ($b = $a) =~ s/-($ud)?-/--$na--/;
573 is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
579 is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
583 is("<$_> <$s>", "<> <4>", "[perl #7806]");
585 # [perl #19048] Coredump in silly replacement
590 is($_, "\n", "[perl #19048]");
593 # [perl #17757] interaction between saw_ampersand and study
595 my $f = eval q{ $& };
599 is($f, "yy", "[perl #17757]");
602 # [perl #20684] returned a zero count
604 is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
606 # [perl #20682] @- not visible in replacement
608 /(2)/; # seed @- with something else
609 s/(1)(2)(3)/$#- (@-)/;
610 is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
612 # [perl #20682] $^N not visible in replacement
614 /(a)/; s/(b)|(c)/-$^N/g;
615 is($_,'a-b-c','#20682 $^N not visible in replacement');
617 # [perl #22351] perl bug with 'e' substitution modifier
620 no warnings 'uninitialized';
623 is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
626 # [perl #34171] $1 didn't honour 'use bytes' in s//e
634 is(length($x), 2, '[perl #34171]');
638 { # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
641 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
642 is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
644 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
645 is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
649 no warnings 'uninitialized';
650 /(((((((((x)))))))))(z)/; # clear $10
651 s/(((((((((x)))))))))(y)/${10}/;
652 is($_,"y","RT#6006: \$_ eq '$_'");
654 s/(((((((((x)))))))))(r)/fooba${10}/;
655 is($_,"foobar","RT#6006: \$_ eq '$_'");
658 my $want=("\n" x 11).("B\n" x 11)."B";
665 is($want,$_,"RT#17542");
669 my @tests = ('ABC', "\xA3\xA4\xA5", "\x{410}\x{411}\x{412}");
673 is($_, "012", "RT#52104: $id");
677 fresh_perl_is( '$_=q(foo);s/(.)\G//g;print' => 'foo', '[perl #69056] positive GPOS regex segfault' );
678 fresh_perl_is( '$_="abcef"; s/bc|(.)\G(.)/$1 ? "[$1-$2]" : "XX"/ge; print' => 'aXX[c-e][e-f]f', 'positive GPOS regex substitution failure' );
680 # [perl #~~~~~] $var =~ s/$qr//e calling get-magic on $_ as well as $var
684 sub qrBug::TIESCALAR { bless[pop], 'qrBug' }
685 sub qrBug::FETCH { $scratch .= "[fetching $_[0][0]]"; 'prew' }
687 tie my $kror, qrBug => '$kror';
688 tie $_, qrBug => '$_';
692 $scratch, '[fetching $kror]',
693 'bug: $var =~ s/$qr//e calling get-magic on $_ as well as $var',
697 { # Bug #41530; replacing non-utf8 with a utf8 causes problems
698 my $string = "a\x{a0}a";
699 my $sub_string = $string;
700 ok(! utf8::is_utf8($sub_string), "Verify that string isn't initially utf8");
701 $sub_string =~ s/a/\x{100}/g;
702 ok(utf8::is_utf8($sub_string),
703 'Verify replace of non-utf8 with utf8 upgrades to utf8');
704 is($sub_string, "\x{100}\x{A0}\x{100}",
705 'Verify #41530 fixed: replace of non-utf8 with utf8');
707 my $non_sub_string = $string;
708 ok(! utf8::is_utf8($non_sub_string),
709 "Verify that string isn't initially utf8");
710 $non_sub_string =~ s/b/\x{100}/g;
711 ok(! utf8::is_utf8($non_sub_string),
712 "Verify that failed substitute doesn't change string's utf8ness");
713 is($non_sub_string, $string,
714 "Verify that failed substitute doesn't change string");