diff options
author | Hans Hagen <pragma@wxs.nl> | 2020-07-05 23:34:31 +0200 |
---|---|---|
committer | Context Git Mirror Bot <phg@phi-gamma.net> | 2020-07-05 23:34:31 +0200 |
commit | f6e05e8759bcf6db94d40b9d9ee0beb268f0347c (patch) | |
tree | 960cfdb3fffa4a80816f91b396f1e00b5782d757 /tex/context/base/mkiv/util-prs.lua | |
parent | 6034a3118b282600abea776e41df31ed2b91c686 (diff) | |
download | context-f6e05e8759bcf6db94d40b9d9ee0beb268f0347c.tar.gz |
2020-07-05 23:03:00
Diffstat (limited to 'tex/context/base/mkiv/util-prs.lua')
-rw-r--r-- | tex/context/base/mkiv/util-prs.lua | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/tex/context/base/mkiv/util-prs.lua b/tex/context/base/mkiv/util-prs.lua index 3154bf6c2..8915c3e9e 100644 --- a/tex/context/base/mkiv/util-prs.lua +++ b/tex/context/base/mkiv/util-prs.lua @@ -10,7 +10,7 @@ local lpeg, table, string = lpeg, table, string local P, R, V, S, C, Ct, Cs, Carg, Cc, Cg, Cf, Cp = lpeg.P, lpeg.R, lpeg.V, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cs, lpeg.Carg, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.Cp local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns local concat, gmatch, find = table.concat, string.gmatch, string.find -local tostring, type, next, rawset = tostring, type, next, rawset +local tonumber, tostring, type, next, rawset = tonumber, tostring, type, next, rawset local mod, div = math.mod, math.div utilities = utilities or {} @@ -538,13 +538,15 @@ function parsers.csvsplitter(specification) specification = specification and setmetatableindex(specification,defaultspecification) or defaultspecification local separator = specification.separator local quotechar = specification.quote + local numbers = specification.numbers local separator = S(separator ~= "" and separator or ",") local whatever = C((1 - separator - newline)^0) if quotechar and quotechar ~= "" then local quotedata = nil for chr in gmatch(quotechar,".") do local quotechar = P(chr) - local quoteword = quotechar * C((1 - quotechar)^0) * quotechar + local quoteitem = (1 - quotechar)^0 + local quoteword = quotechar * (numbers and (quoteitem/tonumber) or C(quoteitem)) * quotechar if quotedata then quotedata = quotedata + quoteword else @@ -559,29 +561,29 @@ function parsers.csvsplitter(specification) end end --- and this is a slightly patched version of a version posted by Philipp Gesang - --- local mycsvsplitter = parsers.rfc4180splitter() - -- local crap = [[ -- first,second,third,fourth -- "1","2","3","4" --- "a","b","c","d" --- "foo","bar""baz","boogie","xyzzy" +-- "5","6","7","8" -- ]] +-- +-- local mycsvsplitter = parsers.csvsplitter { numbers = true } +-- +-- local list = mycsvsplitter(crap) inspect(list) --- local list, names = mycsvsplitter(crap,true) inspect(list) inspect(names) --- local list, names = mycsvsplitter(crap) inspect(list) inspect(names) +-- and this is a slightly patched version of a version posted by Philipp Gesang function parsers.rfc4180splitter(specification) specification = specification and setmetatableindex(specification,defaultspecification) or defaultspecification + local numbers = specification.numbers local separator = specification.separator --> rfc: COMMA local quotechar = P(specification.quote) --> DQUOTE local dquotechar = quotechar * quotechar --> 2DQUOTE / specification.quote local separator = S(separator ~= "" and separator or ",") + local whatever = (dquotechar + (1 - quotechar))^0 local escaped = quotechar - * Cs((dquotechar + (1 - quotechar))^0) + * (numbers and (whatever/tonumber) or Cs(whatever)) * quotechar local non_escaped = C((1 - quotechar - newline - separator)^1) local field = escaped + non_escaped + Cc("") @@ -601,6 +603,18 @@ function parsers.rfc4180splitter(specification) end end +-- local mycsvsplitter = parsers.rfc4180splitter { numbers = true } +-- +-- local crap = [[ +-- first,second,third,fourth +-- "1","2","3","4" +-- "a","b","c","d" +-- "foo","bar""baz","boogie","xyzzy" +-- ]] +-- +-- local list, names = mycsvsplitter(crap,true) inspect(list) inspect(names) +-- local list, names = mycsvsplitter(crap) inspect(list) inspect(names) + -- parsers.stepper("1,7-",9,function(i) print(">>>",i) end) -- parsers.stepper("1-3,7,8,9") -- parsers.stepper("1-3,6,7",function(i) print(">>>",i) end) |