lua-users home
lua-l archive

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


A update to my lpeg.M proposal to match anywhere with head chars optimization.
This is what I have now, a lpeg.H() that recursively build a set of head chars.

Example:
words = re.compile "{ 'hello' / 'Hello'+ / [wW] 'orld' }"
--> lpeg.H(words) = S'HWhw'

Doing lpeg.M in C is beyond me ... instead, i made this lua function:

function re.anywhere(pat)
    pat = re.compile(pat)
    local skip = (1 - lpeg.H(pat)) ^ 0
    return lpeg.P{ pat + 1 * skip * lpeg.V(1) }
end

benchmark showed that optimization work:
-- my timer function unit in msec (my PC is 20 years old)

text = ('this is a test '):rep(10) .. 'hello'
pat0 = lpeg.P{ words + 1 * lpeg.V(1) }
pat1 = re.anywhere(words)

=timer(1e6, lpeg.match, pat0, text)
15863  hello
=timer(1e6, lpeg.match, pat1, text)
6700    hello