[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg use questions
- From: Tony Finch <dot@...>
- Date: Tue, 30 Nov 2010 11:37:14 +0000
On Tue, 30 Nov 2010, Juris Kalnins wrote:
> - matching (length, value) encoded strings:
> for example some binary stream, like network packet, that
> at some point stores length, followed by that many bytes.
Use a match-time capture, e.g. for a one-octet count,
local m = require "lpeg"
local p = m.Cmt(m.P(1), function (str, pos)
local len = str:byte(pos)
return pos+1+len, str:sub(pos+1, pos+len)
end
The capture value is a string comprising the len octets following the
count octet.
> - matching output of another lpeg capture.
> for example, some keyword that is case-insensitive.
> can it be matched using P "string" over something?
This might be another job for match-time captures, e.g.
function kw_pattern(kw)
return m.Cmt(m.P(#kw), function (str,pos)
local fin = pos+#kw
if kw == str:sub(pos,fin):lower()
then return fin end
end
end
Tony.
--
f.anthony.n.finch <dot@dotat.at> http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.