Here is the new patch for split on a regex. Testing it with: say "theXbiggestXbangXforXtheXbuck".split(/X/).perl; say "ab1cd12ef123gh".split(/\d+/).perl; say "<frag>char</frag><tag>soup</tag>".split(/\<\/?.*?\>/).perl;
We get the following output: ["the", "biggest", "bang", "for", "the", "buck"] ["ab", "cd", "ef", "gh"] ["", "char", "", "soup", ""] I'll upload a test to pugs later. On Thu, Sep 18, 2008 at 3:33 PM, Moritz Lenz <[EMAIL PROTECTED]> wrote: > Chris Davaz wrote: >> Ok, here it is without the change to "split on a string", and the test >> passes. > > Yes, but on my machine now t/spec/S04-statements/gather.t produces a > segmentation fault. I'll have to investigate if that is related to your > patch or not. > > Also split behaves a bit strangely here: > > > say 'ab23d4f5'.split(/\d+/).perl > ["ab", "", "", "d", "f", ""] > > say 'ab23d4f5'.split(/\d/).perl > ["ab", "", "d", "f", ""] > > (There are a few other oddities like the behaviour with a zero-width > match, but that's only a minor issue). > >> Please apply this one and in the meantime I will see how we can >> get the method signature right for split on a strong + not break reverse. > > -- > Moritz Lenz > http://moritz.faui2k3.org/ | http://perl-6.de/ >
Index: src/classes/Str.pir =================================================================== --- src/classes/Str.pir (revision 31220) +++ src/classes/Str.pir (working copy) @@ -76,6 +76,35 @@ .return(retv) .end +# split a string on a regex +.sub 'split' :method :multi(_, 'Sub') + .param pmc regex + .local pmc match + .local pmc tmpstr + .local pmc retv + .local int start_pos + .local int end_pos + + $S0 = self + retv = new 'List' + start_pos = 0 + + loop: + match = regex($S0, 'continue' => start_pos) + end_pos = match.'from'() + end_pos -= start_pos + tmpstr = new 'Perl6Str' + $S1 = substr $S0, start_pos, end_pos + tmpstr = $S1 + retv.'push'(tmpstr) + unless match goto done + start_pos = match.'to'() + goto loop + + done: + .return(retv) +.end + .sub lc :method .local string tmps .local pmc retv