lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The relevant line in the manual says "a pattern item can be [among
other things] a single character class followed by '?', which matches
0 or 1 occurrence of a character in the class".

The greediness thus is indeed undefined.

If you were to implement a non-greedy version, you would not be wrong,
and you could smugly laugh at the poor saps who would get bitten by
their lack of attention to detail.

Alternatively, you could be nice, write it to behave like the
original, and make the life of your users slightly better...

Please be nice :-).

FWIW, I implemented Lua patterns in Lua +FFI for LuaJIT. I compile
patterns to Lua code, by stitching strings together. "x?" was tricky
to get right without resorting to duplicated code or splitting the
matcher into several functions.

    templates["?"] = {[[ -- c?
      do
        local i1, matched = i, false
        if ]], P.TEST, [[ then matched = true; i = i + 1 end
        goto firsttime
        ::secondtime::
        i = i1
        ::firsttime::
        do]],
          P.NEXT, [[ --
        ::done:: end
        if i == 0 and matched then matched = false; goto secondtime end
      end]]
    }

`i` is the current index of the subject, `P.TEST` would be
`subject:sub(i,i) == "x"` (for "x?..."), and `P.NEXT` is the code for
the rest of the pattern. `i` is set to 0 if the match fails, hence the
test on the last line. I use 0 to avoid adding another stack slot, and
to keep `i` type stable. (Now that I reread the code, I realize that I
could also use the same trick with `i1` and drop the `matched`
variable).

Have a nice day,

—Pierre-Yves
—Pierre-Yves


On Wed, Dec 24, 2014 at 10:57 PM, Soni L. <fakedme@gmail.com> wrote:
>
> On 24/12/14 07:55 PM, Paul K wrote:
>>>
>>> if I have the pattern "(.)(.?)(.?)" and I try to match "ab" where's the b
>>> gonna go?
>>
>> "b" should go with the second capture as it's still greedy (the
>> question mark makes it optional, but if there is something to capture,
>> it will be captured).
>>
>> Paul.
>>
>> On Wed, Dec 24, 2014 at 1:49 PM, Soni L. <fakedme@gmail.com> wrote:
>>>
>>> <SoniEx2> if I have the pattern "(.)(.?)(.?)" and I try to match "ab"
>>> <SoniEx2> where's the b gonna go?
>>> <SoniEx2> (or is that undefined?)
>>>
>>> --
>>> Disclaimer: these emails are public and can be accessed from <TODO: get a
>>> non-DHCP IP and put it here>. If you do not agree with this, DO NOT
>>> REPLY.
>
> What if the implementation does it backwards for some reason?
>
>
> --
> Disclaimer: these emails are public and can be accessed from <TODO: get a
> non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.
>
>