On Thu, Sep 18, 2008 at 05:17:07PM +0800, Chris Davaz wrote:
> The attached split.diff file is just for demonstration, not a patch submittal.
> 
> I made a method on Str called "match" that returns a List of all matches:
> 
> [...]
>     loop:
>       $S0 = match.'text'()
>       if $S0 == '' goto done
>       retv.'push'($S0)
>       match.'next'()
>       goto loop
> 
> However, running the following code:
> say "ab1cd12ef123gh".match(/\d+/).perl;
> 
> We get:
> ["1", "12", "1", "2", "123", "12", "1", "23", "2", "3"]
> 
> As you can see match is returning what seems to be both greedy 
> *and* non-greedy matches (and everything in between). Is 
> this correct?

Yes.  The 'next' method on a Match object currently means that
we want to obtain the next possible match after any backtracking,
not the one that comes after the position of the previous one.  
We'll want this behavior to be able to support the :exhaustive modifier
(and PGE needs it in order to backtrack into subrules).

Try something like the following instead:

    .sub 'match_global' :method :multi(_, 'Sub')
        .param pmc regex
        .local pmc retv, match
    
        retv = 'list'()
        $S0 = self
        match = regex($S0)
    
      loop:
        unless match goto done
        push retv, match
        match = regex(match)
        goto loop
    
      done:
        .return (retv)
    .end


The line "match = regex(match)"  says to invoke the regex starting
at the .to position of the (previous) match object.

Pm

Reply via email to