6 require './charset_tools.pl';
16 ($a,$b,$c) = split($FS,$_);
18 is(join(';',$a,$b,$c), 'a;b;c', 'Split a simple string into scalars.');
22 is(join("$_",@ary), 'aa:b:cc');
23 is($cnt, scalar(@ary));
26 my @xyz = (@ary = split(//));
28 is(join(".",@ary), "a.b.c.\n");
29 is($cnt, scalar(@ary));
34 is(join(".",@ary), "a.b.c");
35 is($cnt, scalar(@ary));
37 $_ = join(':',split(' '," a b\tc \t d "));
39 @ary = split(' '," a b\tc \t d ");
40 $cnt = split(' '," a b\tc \t d ");
41 is($cnt, scalar(@ary));
43 $_ = join(':',split(/ */,"foo bar bie\tdoll"));
44 is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
45 @ary = split(/ */,"foo bar bie\tdoll");
46 $cnt = split(/ */,"foo bar bie\tdoll");
47 is($cnt, scalar(@ary));
49 $_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
50 is($_, "foo:a:b::c:bar");
51 @ary = split(/ /,'a b c');
52 $cnt = split(/ /,'a b c');
53 is($cnt, scalar(@ary));
55 # Can we say how many fields to split to?
56 $_ = join(':', split(' ','1 2 3 4 5 6', 3));
57 is($_, '1:2:3 4 5 6', "Split into a specified number of fields, defined by a literal");
58 @ary = split(' ','1 2 3 4 5 6', 3);
59 $cnt = split(' ','1 2 3 4 5 6', 3);
60 is($cnt, scalar(@ary), "Check element count from previous test");
62 # Can we do it as a variable?
64 $_ = join(':', split(' ','1 2 3 4 5 6', $x));
65 is($_, '1:2:3:4 5 6', "Split into a specified number of fields, defined by a scalar variable");
66 @ary = split(' ','1 2 3 4 5 6', $x);
67 $cnt = split(' ','1 2 3 4 5 6', $x);
68 is($cnt, scalar(@ary), "Check element count from previous test");
70 # Does the 999 suppress null field chopping?
71 $_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
72 is($_ , '1:2:3:4:5:6:::');
73 @ary = split(/:/,'1:2:3:4:5:6:::', 999);
74 $cnt = split(/:/,'1:2:3:4:5:6:::', 999);
75 is($cnt, scalar(@ary));
77 # Splitting without pattern
79 $_ = join(':', split);
80 is($_ , '1:2:3:4', "Split and join without specifying a split pattern");
82 # Does assignment to a list imply split to one more field than that?
83 $foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
84 ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
86 # Can we say how many fields to split to when assigning to a list?
87 ($a,$b) = split(' ','1 2 3 4 5 6', 2);
89 is($_, '1:2 3 4 5 6', "Storing split output into list of scalars");
91 # do subpatterns generate additional fields (without trailing nulls)?
92 $_ = join '|', split(/,|(-)/, "1-10,20,,,");
94 @ary = split(/,|(-)/, "1-10,20,,,");
95 $cnt = split(/,|(-)/, "1-10,20,,,");
96 is($cnt, scalar(@ary));
98 # do subpatterns generate additional fields (with a limit)?
99 $_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
100 is($_, "1|-|10||20||||||");
101 @ary = split(/,|(-)/, "1-10,20,,,", 10);
102 $cnt = split(/,|(-)/, "1-10,20,,,", 10);
103 is($cnt, scalar(@ary));
105 # is the 'two undefs' bug fixed?
106 (undef, $a, undef, $b) = qw(1 2 3 4);
109 # .. even for locals?
111 local(undef, $a, undef, $b) = qw(1 2 3 4);
115 # check splitting of null string
116 $_ = join('|', split(/x/, '',-1), 'Z');
118 @ary = split(/x/, '',-1);
119 $cnt = split(/x/, '',-1);
120 is($cnt, scalar(@ary));
122 $_ = join('|', split(/x/, '', 1), 'Z');
124 @ary = split(/x/, '', 1);
125 $cnt = split(/x/, '', 1);
126 is($cnt, scalar(@ary));
128 $_ = join('|', split(/(p+)/,'',-1), 'Z');
130 @ary = split(/(p+)/,'',-1);
131 $cnt = split(/(p+)/,'',-1);
132 is($cnt, scalar(@ary));
134 $_ = join('|', split(/.?/, '',-1), 'Z');
136 @ary = split(/.?/, '',-1);
137 $cnt = split(/.?/, '',-1);
138 is($cnt, scalar(@ary));
141 # Are /^/m patterns scanned?
142 $_ = join '|', split(/^a/m, "a b a\na d a", 20);
143 is($_, "| b a\n| d a");
144 @ary = split(/^a/m, "a b a\na d a", 20);
145 $cnt = split(/^a/m, "a b a\na d a", 20);
146 is($cnt, scalar(@ary));
148 # Are /$/m patterns scanned?
149 $_ = join '|', split(/a$/m, "a b a\na d a", 20);
150 is($_, "a b |\na d |");
151 @ary = split(/a$/m, "a b a\na d a", 20);
152 $cnt = split(/a$/m, "a b a\na d a", 20);
153 is($cnt, scalar(@ary));
155 # Are /^/m patterns scanned?
156 $_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
157 is($_, "| b aa\n| d aa");
158 @ary = split(/^aa/m, "aa b aa\naa d aa", 20);
159 $cnt = split(/^aa/m, "aa b aa\naa d aa", 20);
160 is($cnt, scalar(@ary));
162 # Are /$/m patterns scanned?
163 $_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
164 is($_, "aa b |\naa d |");
165 @ary = split(/aa$/m, "aa b aa\naa d aa", 20);
166 $cnt = split(/aa$/m, "aa b aa\naa d aa", 20);
167 is($cnt, scalar(@ary));
171 @ary = split(/\s*:\s*/);
172 $cnt = split(/\s*:\s*/);
173 is(($res = join(".",@ary)), "a.b.c.d", $res);
174 is($cnt, scalar(@ary));
176 # use of match result as pattern (!)
177 is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
178 @ary = split('abc' =~ /b/, 'p1q1r1s');
179 $cnt = split('abc' =~ /b/, 'p1q1r1s');
180 is($cnt, scalar(@ary));
182 # /^/ treated as /^/m
183 $_ = join ':', split /^/, "ab\ncd\nef\n";
184 is($_, "ab\n:cd\n:ef\n","check that split /^/ is treated as split /^/m");
186 $_ = join ':', split /\A/, "ab\ncd\nef\n";
187 is($_, "ab\ncd\nef\n","check that split /\A/ is NOT treated as split /^/m");
189 # see if @a = @b = split(...) optimization works
190 @list1 = @list2 = split ('p',"a p b c p");
191 ok(@list1 == @list2 &&
192 "@list1" eq "@list2" &&
194 "@list1" eq "a b c ");
196 # zero-width assertion
197 $_ = join ':', split /(?=\w)/, "rm b";
199 @ary = split /(?=\w)/, "rm b";
200 $cnt = split /(?=\w)/, "rm b";
201 is($cnt, scalar(@ary));
205 @ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
206 $cnt = split //, v1.20.300.4000.50000.4000.300.20.1;
207 is("@ary", "1 20 300 4000 50000 4000 300 20 1");
208 is($cnt, scalar(@ary));
210 @ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
211 $cnt = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
213 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
214 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
215 is($cnt, scalar(@ary));
217 @ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
218 $cnt = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
220 $ary[0] eq "\xFF\xFF" &&
221 $ary[0] eq "\x{FF}\xFF" &&
222 $ary[0] eq "\x{FF}\x{FF}" &&
223 $ary[1] eq "\xFE\xFE" &&
224 $ary[1] eq "\x{FE}\xFE" &&
225 $ary[1] eq "\x{FE}\x{FE}" &&
226 $ary[2] eq "\xFD\xFD" &&
227 $ary[2] eq "\x{FD}\xFD" &&
228 $ary[2] eq "\x{FD}\x{FD}");
229 is($cnt, scalar(@ary));
232 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
233 my $c = split(//, join("", map chr, (1234, 123, 2345)));
234 is("@a", "1234 123 2345");
240 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
241 my $c = split(/$x/, join("", map chr, (1234, ord($x), 2345)));
242 is("@a", "1234 2345");
247 # bug id 20000427.003
252 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
254 my @charlist = split //, $sushi;
255 my $charnum = split //, $sushi;
256 is($charnum, scalar(@charlist));
258 foreach my $ch (@charlist) {
259 $r = $r . " " . sprintf "U+%04X", ord($ch);
262 is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
266 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
269 # bug id 20000426.003
271 my ($a, $b, $c) = split(/\x40/, $s);
272 ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
275 my ($a, $b) = split(/\x{100}/, $s);
276 ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
278 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
279 ok($a eq "\x20\x40" && $b eq "\x40\x20");
282 my ($a, $b) = split(/\x40\x{80}/, $s);
283 ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
286 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
287 ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
293 my $a = "ABC\x{263A}";
295 my @b = split( //, $a );
296 my $c = split( //, $a );
301 ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
304 ok(length($a) == 4 && $a eq "ZBC\x{263A}");
308 my @a = split(/\xFE/, "\xFF\xFE\xFD");
309 my $b = split(/\xFE/, "\xFF\xFE\xFD");
311 ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
316 # check that PMf_WHITE is cleared after \s+ is used
317 # reported in <20010627113312.RWGY6087.viemta06@localhost>
319 foreach my $pat ( qr/\s+/, qr/ll/ ) {
320 $r = join ':' => split($pat, "hello cruel world");
322 is($r, "he:o cruel world");
327 # split /(A)|B/, "1B2" should return (1, undef, 2)
328 my @x = split /(A)|B/, "1B2";
329 my $y = split /(A)|B/, "1B2";
331 ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
337 local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
338 my $char = "\x{10f1ff}";
339 my @a = split /\r?\n/, "$char\n";
340 my $b = split /\r?\n/, "$char\n";
342 ok(@a == 1 && $a[0] eq $char && !defined($warn));
349 $_ = 'readin,database,readout';
350 utf8::upgrade $_ if $u;
352 my @d = split /[,]/,$1;
353 my $e = split /[,]/,$1;
355 is(join (':',@d), 'readin:database:readout', "[perl #18195]");
363 eval { @a=split(/[, ]+/,$p) };
364 eval { $b=split(/[, ]+/,$p) };
366 is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
370 # LATIN SMALL LETTER A WITH DIAERESIS, CYRILLIC SMALL LETTER I
371 for my $pattern ("\N{U+E4}", "\x{0437}") {
372 utf8::upgrade $pattern;
374 for my $str ("a${pattern}b", "axb", "a${pattern}b") {
375 @split = split /$pattern/, $str;
376 push @res, scalar(@split);
380 is($res[2], 2, '#123469 - split with utf8 pattern after handling non-utf8 EXPR');
385 is (\@a, \@{"a"}, '@a must be global for following test');
387 $n = @a = split /,/,$p;
388 is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
393 # assigning off the end of the array after a split could leave garbage
394 # in the inner elements
397 @a = split /,/, ',,,,,';
400 is (ref $x, 'SCALAR', '#28938 - garbage after extend');
404 my $src = "ABC \0 FOO \0 XYZ";
405 my @s = split(" \0 ", $src);
406 my @r = split(/ \0 /, $src);
407 my $cs = split(" \0 ", $src);
408 my $cr = split(/ \0 /, $src);
415 is(join(':',@s), join(':',@r));
419 use constant BANG => {};
420 () = split m/,/, "", BANG;
426 # 'Hybrid' scalar-and-array context
427 scalar(our @PATH = split /::/, "Font::GlyphNames");
428 # 'my' doesn't trigger the bug
429 is "@PATH", "Font GlyphNames", "hybrid scalar-and-array context";
437 @results= split(0||" ", $expr);
438 is @results, 2, 'split(0||" ") is treated like split(" ")'; #'
441 @results= split $cond ? " " : qr/ /, $expr;
442 is @results, 3, 'split($cond ? " " : qr/ /, $expr) works as expected (like qr/ /)';
444 @results= split $cond ? " " : qr/ /, $expr;
445 is @results, 2, 'split($cond ? " " : qr/ /, $expr) works as expected (like " ")';
448 @results = split /\s/, $expr;
450 "split on regex of single space metacharacter: captured 4 elements";
452 "split on regex of single space metacharacter: first element is empty string";
454 @results = split / /, $expr;
456 "split on regex of single whitespace: captured 4 elements";
458 "split on regex of single whitespace: first element is empty string";
460 @results = split " ", $expr;
462 "split on string of single whitespace: captured 3 elements";
464 "split on string of single whitespace: first element is non-empty";
467 @results = split " ", $expr;
469 "split on string of single whitespace: captured 3 elements";
471 "split on string of single whitespace: first element is non-empty; multiple contiguous space characters";
474 for my $cond (0,1,0,1,0) {
476 @results = split $cond ? qr/ / : " ", $expr;
477 push @seq, scalar(@results) . ":" . $results[-1];
479 is join(" ", @seq), "1:foo 3:foo 1:foo 3:foo 1:foo",
480 qq{split(\$cond ? qr/ / : " ", "$exp") behaves as expected over repeated similar patterns};
484 # 'RT #116086: split "\x20" does not work as documented';
488 @results = split uni_to_native("\x20"), $expr;
490 "RT #116086: split on string of single hex-20: captured 3 elements";
492 "RT #116086: split on string of single hex-20: first element is non-empty";
495 @results = split uni_to_native("\x20"), $expr;
497 "RT #116086: split on string of single hex-20: captured 3 elements";
499 "RT #116086: split on string of single hex-20: first element is non-empty; multiple contiguous space characters";
502 # Nasty interaction between split and use constant
503 use constant nought => 0;
504 ($a,$b,$c) = split //, $foo, nought;
505 is nought, 0, 'split does not mangle 0 constants';
508 $aaa[1] = "foobarbaz";
510 @aaa = split //, $bbb[1];
511 is "@aaa", "f o o b a r b a z",
512 'split-to-array does not free its own argument';
514 () = @a = split //, "abc";
515 is "@a", "a b c", '() = split-to-array';
517 (@a = split //, "abc") = 1..10;
518 is "@a", '1 2 3', 'assignment to split-to-array (pmtarget/package array)';
521 (@a = split //, "abc") = 1..10;
522 is "@a", '1 2 3', 'assignment to split-to-array (targ/lexical)';
524 (@{\@a} = split //, "abc") = 1..10;
525 is "@a", '1 2 3', 'assignment to split-to-array (stacked)';