Please find attached a patch to implement requested functionality. 
Passes split-simple.t tests and includes a patch that updates those
tests and adds one more that verifies correct performance with a return
limit count.
Index: 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
diff --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;
         }

Reply via email to