lua-users home
lua-l archive

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

On Fri, Sep 24, 2010 at 8:50 AM, Scott Vokes <> wrote:
>   I'm not set on testing strings with :match rather than just ==,
> either. Perhaps it could be activated by an optional optional
> match=true flag on the pattern row, like { "foo %d+", handle_num,
> match=true } ? For now, it checks strings for magic characters and
> only uses :match on string patterns when they are present (which makes
> a big difference timewise). (Steve Donovan and I debated the pros and
> cons of this, too.)

my 2 cents (with 30% discount for not having read the source!):  don't
scan the string either, use closures:

instead of:  "xxx" is a constant, and "xx%d" is a match pattern, do:
"xxx" is a constant and tamale.M("xx%d") is a match predicate (a
closure that takes the target element and returns true or false).

something like this:

function tamale.M(p)
    return function(s)
        return p:match(s)

and in the matching core:

local t = type(pat_element)
if t== 'string' then
    m = (targ_element==pat_element)
elseif t == 'function' then
    m = pat_element(targ_element)
elseif t == 'table' then

(again, i haven't read the source, but i guess somewhere it's checking
the type of the pattern elements to switch between matching

a big advantage of this is that you could use different matching predicates