New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
split should return captured delimiters #817
Comments
From @clintongormley'abc-def--ghi'.split(/(\-+)/) added tests to t/spec/S32-str/split-simple.t |
From @clintongormley
or i would have done if i could remember my pugs password! Index: split-simple.t--- split-simple.t (revision 25961) # L<S29/Str/"=item split"> =begin description @@ -65,4 +65,14 @@ +# split should return capture |
@clintongormley - Status changed from 'new' to 'open' |
From @moritzOn Sat Mar 21 13:01:25 2009, DrTech wrote:
I've added the tests, and re-sent your pugs password. Thanks for the report and the tests, |
From @ronaldxsPlease find attached a patch to implement requested functionality. |
From @ronaldxssplit-simple.patchIndex: t/spec/S32-str/split-simple.t
===================================================================
--- t/spec/S32-str/split-simple.t (revision 26002)
+++ t/spec/S32-str/split-simple.t (working copy)
@@ -2,7 +2,7 @@
use Test;
# L<S29/Str/"=item split">
-plan 45;
+plan 46;
=begin description
@@ -83,12 +83,13 @@
# split should return capture
my @split = 'abc def ghi'.split(/(\s+)/);
-#?rakudo todo "split should return captures"
-#?DOES 3
-{
- ok @split.elems == 5, q{split returns captured delimiter} ;
- ok @split[1] eq ' ', q{split captured single space};
- ok @split[3] eq ' ', q{split captured multiple spaces};
-}
+ok @split.elems == 5, q{split returns captured delimiter} ;
+ok @split[1] eq ' ', q{split captured single space};
+ok @split[3] eq ' ', q{split captured multiple spaces};
+@split = 'abc::def::ghi'.split(/(\:)/, 5);
+ok @split.elems == 5 and
+ @split[3] eq 'def' and
+ @split[4] eq ':',
+ q{split with capture obeyed limit};
# vim: ft=perl6
|
From @ronaldxsAny-str.patchdiff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index 27c2080..85e970a 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -54,9 +54,25 @@ class Any is also {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
+
return gather {
while $l > 1 && $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
+ $l--;
+
+ # match objects too tied to underlying strings so copy ...
+ my @mat_cap = @().map: { substr($_, 0) };
+ my $mat_cap_n = $l min @mat_cap.elems;
+ if ($mat_cap_n) {
+ if $mat_cap_n == @mat_cap {
+ take @mat_cap
+ }
+ else {
+ take @mat_cap[ 0 .. ($mat_cap_n -1) ]
+ }
+ $l -= $mat_cap_n
+ }
+
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
@@ -64,7 +80,6 @@ class Any is also {
$keep = '';
$s.=substr($/.to)
}
- $l--;
}
take $keep ~ $s if $l > 0;
}
|
From duff@lighthouse.tamucc.eduOn Thu, Mar 26, 2009 at 06:00:29PM -0700, Ron Schmidt via RT wrote:
That's not right, is it? Or I don't understand what perl 6 does with 0 "abc" I assume the error is because of the switch in the string from using a Also, setting the limit parameter to 5 seems fairly useless in the For instance, if the string were "abc::def::ghi::jkl", @split would -Scott |
From @ronaldxs
... Your concern seems to be with the added test and your issues with |
From @ronaldxssplit-simple.patchIndex: t/spec/S32-str/split-simple.t
===================================================================
--- t/spec/S32-str/split-simple.t (revision 26002)
+++ t/spec/S32-str/split-simple.t (working copy)
@@ -2,7 +2,7 @@
use Test;
# L<S29/Str/"=item split">
-plan 45;
+plan 46;
=begin description
@@ -83,12 +83,13 @@
# split should return capture
my @split = 'abc def ghi'.split(/(\s+)/);
-#?rakudo todo "split should return captures"
-#?DOES 3
-{
- ok @split.elems == 5, q{split returns captured delimiter} ;
- ok @split[1] eq ' ', q{split captured single space};
- ok @split[3] eq ' ', q{split captured multiple spaces};
-}
+ok @split.elems == 5, q{split returns captured delimiter} ;
+ok @split[1] eq ' ', q{split captured single space};
+ok @split[3] eq ' ', q{split captured multiple spaces};
+@split = 'abc::def::ghi'.split(/(\:)/, 3);
+ok @split.elems == 5 and
+ @split[3] eq ':' and
+ @split[4] eq 'def::ghi',
+ q{split with capture obeyed limit};
# vim: ft=perl6
|
From @ronaldxsAny-str.patchdiff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index 27c2080..0346d4e 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -54,9 +54,23 @@ class Any is also {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
+
return gather {
while $l > 1 && $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
+
+ # match objects too tied to underlying strings so copy ...
+ my @mat_cap = @().map: { substr($_, 0) };
+ my $mat_cap_n = $l min @mat_cap.elems;
+ if ($mat_cap_n) {
+ if $mat_cap_n == @mat_cap {
+ take @mat_cap
+ }
+ else {
+ take @mat_cap[ 0 .. ($mat_cap_n -1) ]
+ }
+ }
+
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
|
From @ronaldxsAn irc discussion (http://irclog.perlgeek.de/perl6/2009-04- Please ignore any of my earlier proposed patches for now… |
From @ronaldxsPlease find attached new, revised, hopefully more spec conformant, One of the two patches includes new tests with some appropriate split_test( |
From @ronaldxssplit-simple.patchIndex: t/spec/S32-str/split-simple.t
===================================================================
--- t/spec/S32-str/split-simple.t (revision 26126)
+++ t/spec/S32-str/split-simple.t (working copy)
@@ -2,7 +2,7 @@
use Test;
# L<S29/Str/"=item split">
-plan 45;
+plan 52;
=begin description
@@ -81,14 +81,37 @@
ok (''.split('')).elems == 0, q{''.split('') returns empty list};
ok (split('', '')).elems == 0, q{''.split('') returns empty list};
-# split should return capture
-my @split = 'abc def ghi'.split(/(\s+)/);
-#?rakudo todo "split should return captures"
-#?DOES 3
-{
- ok @split.elems == 5, q{split returns captured delimiter} ;
- ok @split[1] eq ' ', q{split captured single space};
- ok @split[3] eq ' ', q{split captured multiple spaces};
-}
+# split should return capture with all flag set
+split_test(
+ 'abc def ghi'.split(/(\s+)/, all => True),
+ ['abc', ' ', 'def', ' ', 'ghi'],
+ 'split returns captured spaces'
+);
+# split should NOT return capture without all flag
+split_test(
+ 'abc def ghi'.split(/(\s+)/),
+ ['abc', 'def', 'ghi'],
+ 'split ignores captures without all flag'
+);
+
+ok 'abc def ghi'.split(/(\s+)/, :all(True)).[1] ~~ Match,
+ 'capture returns match object not string';
+
+split_test(
+ 'abc::def::ghi::'.split(/(\:)/, all => True, 3),
+ ['abc', ':', '', ':', 'def::ghi::'],
+ 'split with capture obeyed limit'
+);
+
+split_test(
+ 'AZZAZ'.split(/(Z)/, :all(True), 9),
+ ['A', 'Z', '', 'Z', 'A', 'Z', ''],
+ 'end cases with trailing capture and too big limit'
+);
+
+my Match $mat = 'AZYAZ'.split(/(Z)(Y)/, :all(True))[1];
+ok $mat eq 'ZY' && $mat[0] eq 'Z' && $mat[1] eq 'Y',
+ 'basic test of match with multiple captures';
+
# vim: ft=perl6
|
From @ronaldxsAny-str.patchdiff --git a/src/setting/Any-str.pm b/src/setting/Any-str.pm
index 27c2080..a898064 100644
--- a/src/setting/Any-str.pm
+++ b/src/setting/Any-str.pm
@@ -50,13 +50,23 @@ class Any is also {
$char
}
- our List multi method split(Code $delimiter, $limit = *) {
+ our List multi method split(Code $delimiter, $limit = *, :$all = False) {
my $s = ~self;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $keep = '';
+
return gather {
- while $l > 1 && $s ~~ $delimiter {
+ # <ws> still off (rt 60366) and maybe not easy fix just now 3/31/09
+ # second test is hack to prevent <?wb> or <ws> from hanging
+ while $l > 1 and $s ne '' and $s ~~ $delimiter {
take $keep ~ $s.substr(0, $/.from);
+ if $all and (@($/) or %($/)) {
+ my Match $mat = $/.clone;
+ # work around too close binding of match to underly string
+ $mat[ $_ ] = $mat[ $_ ].clone for 0 .. (@().elems -1);
+ $mat{ $_ } = $mat{ $_ }.clone for %().keys;
+ take $mat;
+ }
if $/.from == $/.to {
$keep = $s.substr($/.to, 1);
$s.=substr($/.to + 1);
@@ -66,12 +76,15 @@ class Any is also {
}
$l--;
}
- take $keep ~ $s if $l > 0;
+ unless $l <= 0 or ($keep eq '' and $s eq '' and $l == 0) {
+ take $keep ~ $s
+ }
}
}
# TODO: substitute with '$delimiter as Str' once coercion is implemented
- our List multi method split($delimiter, $limit = *) {
+ # :$all = False just keeps MMD happy for now ...
+ our List multi method split($delimiter, $limit = *, :$all = False) {
my Int $prev = 0;
my $l = $limit ~~ Whatever ?? Inf !! $limit;
my $s = ~self;
|
From @moritzAs was already mentioned, match objects are returned by split when the This is implemented and tested in Rakudo. |
@moritz - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#64062 (status was 'resolved')
Searchable as RT64062$
The text was updated successfully, but these errors were encountered: