|
----- Original Message ----- From: Luiz Henrique de Figueiredo Date: 8/3/2009 6:49 AM This is based on some help from the list, PiL, and some techniques on the LPEG recipes page. It does build one "massive" table of the contents of the .csv file, a tremendous difference over Steve's implementation, I believe.have you done some benchmarks (informal or otherwise), eg with a pure-Lua solution that works with LPEG or some other pattern-matching algorithms?There's code in PiL: http://www.lua.org/pil/20.4.html -Josh --------------------------------------------------- -- lexers/csv.lua -- -- Given a .csv file, convert it into a Lua table. module(..., package.seeall) local lpeg = require 'lpeg' local C, Cs, Ct, P, S = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.S local eol = P'\r\n' + P'\n' local quoted_field = '"' * Cs(((P(1) - '"') + P'""' / '"')^0) * '"' local unquoted_field = C((1 - S',\r\n"')^0) local field = quoted_field + unquoted_field local record = Ct(field * (',' * field)^0) local nonemptyrecord = #P(1 - eol) * record local records = Ct((record * eol)^0 * nonemptyrecord^-1) * -1 local _one_line_record_ = record * (eol + -1) function parse_line(line) assert(type(line) == 'string', 'bad argument #1 (expected string)') return lpeg.match(one_line_record, line) end -- public interface getmetatable(getfenv(1)).__call = function(self, input) assert(type(input) == 'string', 'bad argument #1 (expected string)') return lpeg.match(records, input) end |