Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 44ba990

Browse files
author
Philipp Gesang
committed
sync with Context as of 2013-11-04
1 parent 6934dad commit 44ba990

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

lualibs-util-prs.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ local newline = lpegpatterns.newline
4040
local anything = lpegpatterns.anything
4141
local endofstring = lpegpatterns.endofstring
4242

43-
local nobrace = 1 - ( lbrace + rbrace )
44-
local noparent = 1 - ( lparent + rparent)
43+
local nobrace = 1 - (lbrace + rbrace )
44+
local noparent = 1 - (lparent + rparent)
4545

4646
-- we could use a Cf Cg construct
4747

@@ -189,6 +189,38 @@ function parsers.settings_to_array(str,strict)
189189
end
190190
end
191191

192+
-- this one also strips end spaces before separators
193+
--
194+
-- "{123} , 456 " -> "123" "456"
195+
196+
local separator = space^0 * comma * space^0
197+
local value = P(lbrace * C((nobrace + nestedbraces)^0) * rbrace)
198+
+ C((nestedbraces + (1-(space^0*(comma+P(-1)))))^0)
199+
local withvalue = Carg(1) * value / function(f,s) return f(s) end
200+
local pattern_a = spaces * Ct(value*(separator*value)^0)
201+
local pattern_b = spaces * withvalue * (separator*withvalue)^0
202+
203+
function parsers.stripped_settings_to_array(str)
204+
if not str or str == "" then
205+
return { }
206+
else
207+
return lpegmatch(pattern_a,str)
208+
end
209+
end
210+
211+
function parsers.process_stripped_settings(str,action)
212+
if not str or str == "" then
213+
return { }
214+
else
215+
return lpegmatch(pattern_b,str,1,action)
216+
end
217+
end
218+
219+
-- parsers.process_stripped_settings("{123} , 456 ",function(s) print("["..s.."]") end)
220+
-- parsers.process_stripped_settings("123 , 456 ",function(s) print("["..s.."]") end)
221+
222+
--
223+
192224
local function set(t,v)
193225
t[#t+1] = v
194226
end

lualibs-util-str.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,13 @@ end
614614

615615
--
616616

617+
local format_z = function(f)
618+
n = n + (tonumber(f) or 1)
619+
return "''" -- okay, not that efficient to append '' but a special case anyway
620+
end
621+
622+
--
623+
617624
local format_rest = function(s)
618625
return format("%q",s) -- catches " and \n and such
619626
end
@@ -671,6 +678,7 @@ local builder = Cs { "start",
671678
+ V("A") -- new
672679
+ V("j") + V("J") -- stripped e E
673680
+ V("m") + V("M") -- new
681+
+ V("z") -- new
674682
--
675683
+ V("*") -- ignores probably messed up %
676684
)
@@ -720,6 +728,8 @@ local builder = Cs { "start",
720728
["m"] = (prefix_tab * P("m")) / format_m, -- %m => xxx.xxx.xxx,xx (optional prefix instead of .)
721729
["M"] = (prefix_tab * P("M")) / format_M, -- %M => xxx,xxx,xxx.xx (optional prefix instead of ,)
722730
--
731+
["z"] = (prefix_any * P("z")) / format_z, -- %M => xxx,xxx,xxx.xx (optional prefix instead of ,)
732+
--
723733
["a"] = (prefix_any * P("a")) / format_a, -- %a => '...' (forces tostring)
724734
["A"] = (prefix_any * P("A")) / format_A, -- %A => "..." (forces tostring)
725735
--
@@ -750,7 +760,7 @@ local function make(t,str)
750760
p = lpegmatch(builder,str,1,"..",t._extensions_) -- after this we know n
751761
if n > 0 then
752762
p = format(template,preamble,t._preamble_,arguments[n],p)
753-
-- print("builder>",p)
763+
-- print("builder>",p)
754764
f = loadstripped(p)()
755765
else
756766
f = function() return str end

0 commit comments

Comments
 (0)