diff options
author | Philipp Gesang <phg42.2a@gmail.com> | 2014-02-14 18:28:31 +0100 |
---|---|---|
committer | Philipp Gesang <phg42.2a@gmail.com> | 2014-02-14 18:28:31 +0100 |
commit | 5ed9c2a6d45121bdeacdf1c8f4dead50374f0899 (patch) | |
tree | 0e2bbc4ecd17b275133bb89340bbc4218a604b67 | |
parent | 43ffeea64e686e25807959575d07162bc8bb02b6 (diff) | |
download | lualibs-5ed9c2a6d45121bdeacdf1c8f4dead50374f0899.tar.gz |
sync with Context as of 2014-02-14
-rw-r--r-- | lualibs-dir.lua | 61 | ||||
-rw-r--r-- | lualibs-lpeg.lua | 4 | ||||
-rw-r--r-- | lualibs-lua.lua | 6 | ||||
-rw-r--r-- | lualibs-string.lua | 5 | ||||
-rw-r--r-- | lualibs-trac-inf.lua | 5 | ||||
-rw-r--r-- | lualibs-util-deb.lua | 46 | ||||
-rw-r--r-- | lualibs-util-str.lua | 170 | ||||
-rw-r--r-- | lualibs-util-tab.lua | 3 |
8 files changed, 184 insertions, 116 deletions
diff --git a/lualibs-dir.lua b/lualibs-dir.lua index 40081cc..b658b7c 100644 --- a/lualibs-dir.lua +++ b/lualibs-dir.lua @@ -25,6 +25,7 @@ local isdir = lfs.isdir local isfile = lfs.isfile local currentdir = lfs.currentdir local chdir = lfs.chdir +local mkdir = lfs.mkdir local onwindows = os.type == "windows" or find(os.getenv("PATH"),";") @@ -284,17 +285,28 @@ local make_indeed = true -- false if onwindows then function dir.mkdirs(...) - local str, pth = "", "" - for i=1,select("#",...) do - local s = select(i,...) - if s == "" then - -- skip - elseif str == "" then - str = s - else - str = str .. "/" .. s + local n = select("#",...) + local str + if n == 1 then + str = select(1,...) + if isdir(str) then + return str, true + end + else + str = "" + for i=1,n do + local s = select(i,...) + local s = select(i,...) + if s == "" then + -- skip + elseif str == "" then + str = s + else + str = str .. "/" .. s + end end end + local pth = "" local drive = false local first, middle, last = match(str,"^(//)(//*)(.*)$") if first then @@ -330,7 +342,7 @@ if onwindows then pth = pth .. "/" .. s end if make_indeed and not isdir(pth) then - lfs.mkdir(pth) + mkdir(pth) end end return pth, (isdir(pth) == true) @@ -351,14 +363,23 @@ if onwindows then else function dir.mkdirs(...) - local str, pth = "", "" - for i=1,select("#",...) do - local s = select(i,...) - if s and s ~= "" then -- we catch nil and false - if str ~= "" then - str = str .. "/" .. s - else - str = s + local n = select("#",...) + local str, pth + if n == 1 then + str = select(1,...) + if isdir(str) then + return str, true + end + else + str = "" + for i=1,n do + local s = select(i,...) + if s and s ~= "" then -- we catch nil and false + if str ~= "" then + str = str .. "/" .. s + else + str = s + end end end end @@ -373,7 +394,7 @@ else pth = pth .. "/" .. s end if make_indeed and not first and not isdir(pth) then - lfs.mkdir(pth) + mkdir(pth) end end else @@ -381,7 +402,7 @@ else for s in gmatch(str,"[^/]+") do pth = pth .. "/" .. s if make_indeed and not isdir(pth) then - lfs.mkdir(pth) + mkdir(pth) end end end diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua index 6d3acd7..6feb708 100644 --- a/lualibs-lpeg.lua +++ b/lualibs-lpeg.lua @@ -176,12 +176,14 @@ patterns.whitespace = whitespace patterns.nonspacer = nonspacer patterns.nonwhitespace = nonwhitespace -local stripper = spacer^0 * C((spacer^0 * nonspacer^1)^0) -- from example by roberto +local stripper = spacer ^0 * C((spacer ^0 * nonspacer ^1)^0) -- from example by roberto +local fullstripper = whitespace^0 * C((whitespace^0 * nonwhitespace^1)^0) ----- collapser = Cs(spacer^0/"" * ((spacer^1 * endofstring / "") + (spacer^1/" ") + P(1))^0) local collapser = Cs(spacer^0/"" * nonspacer^0 * ((spacer^0/" " * nonspacer^1)^0)) patterns.stripper = stripper +patterns.fullstripper = fullstripper patterns.collapser = collapser patterns.lowercase = lowercase diff --git a/lualibs-lua.lua b/lualibs-lua.lua index fc05afa..4a96b0b 100644 --- a/lualibs-lua.lua +++ b/lualibs-lua.lua @@ -148,3 +148,9 @@ function optionalrequire(...) return result end end + +-- nice for non ascii scripts (this might move): + +if lua then + lua.mask = load([[τεχ = 1]]) and "utf" or "ascii" +end diff --git a/lualibs-string.lua b/lualibs-string.lua index 9b079b0..3b1a000 100644 --- a/lualibs-string.lua +++ b/lualibs-string.lua @@ -70,6 +70,7 @@ function string.limit(str,n,sentinel) -- not utf proof end local stripper = patterns.stripper +local fullstripper = patterns.fullstripper local collapser = patterns.collapser local longtostring = patterns.longtostring @@ -77,6 +78,10 @@ function string.strip(str) return lpegmatch(stripper,str) or "" end +function string.fullstrip(str) + return lpegmatch(fullstripper,str) or "" +end + function string.collapsespaces(str) return lpegmatch(collapser,str) or "" end diff --git a/lualibs-trac-inf.lua b/lualibs-trac-inf.lua index 802f2e6..067cff2 100644 --- a/lualibs-trac-inf.lua +++ b/lualibs-trac-inf.lua @@ -123,7 +123,10 @@ function statistics.show() -- this code will move local register = statistics.register register("used platform", function() - return format("%s, type: %s, binary subtree: %s",os.platform or "unknown",os.type or "unknown", environment.texos or "unknown") + local mask = lua.mask or "ascii" + return format("%s, type: %s, binary subtree: %s, symbol mask: %s (%s)", + os.platform or "unknown",os.type or "unknown", environment.texos or "unknown", + mask,mask == "utf" and "τεχ" or "tex") end) register("luatex banner", function() return lower(status.banner) diff --git a/lualibs-util-deb.lua b/lualibs-util-deb.lua index 785373f..ee732b3 100644 --- a/lualibs-util-deb.lua +++ b/lualibs-util-deb.lua @@ -92,37 +92,41 @@ end function debugger.disable() debug.sethook() ---~ counters[debug.getinfo(2,"f").func] = nil + -- counters[debug.getinfo(2,"f").func] = nil end ---~ debugger.enable() - ---~ print(math.sin(1*.5)) ---~ print(math.sin(1*.5)) ---~ print(math.sin(1*.5)) ---~ print(math.sin(1*.5)) ---~ print(math.sin(1*.5)) - ---~ debugger.disable() - ---~ print("") ---~ debugger.showstats() ---~ print("") ---~ debugger.showstats(print,3) - +-- debugger.enable() +-- +-- print(math.sin(1*.5)) +-- print(math.sin(1*.5)) +-- print(math.sin(1*.5)) +-- print(math.sin(1*.5)) +-- print(math.sin(1*.5)) +-- +-- debugger.disable() +-- +-- print("") +-- debugger.showstats() +-- print("") +-- debugger.showstats(print,3) +-- -- from the lua book: -function traceback() - local level = 1 +local function showtraceback(rep) -- from lua site / adapted + local level = 2 -- we don't want this function to be reported + local reporter = rep or report while true do - local info = debug.getinfo(level, "Sl") + local info = getinfo(level, "Sl") if not info then break elseif info.what == "C" then - print(format("%3i : C function",level)) + reporter("%2i : %s",level-1,"C function") else - print(format("%3i : [%s]:%d",level,info.short_src,info.currentline)) + reporter("%2i : %s : %s",level-1,info.short_src,info.currentline) end level = level + 1 end end + +debugger.showtraceback = showtraceback +-- debug.showtraceback = showtraceback diff --git a/lualibs-util-str.lua b/lualibs-util-str.lua index f04f0e5..4ecaed7 100644 --- a/lualibs-util-str.lua +++ b/lualibs-util-str.lua @@ -20,16 +20,24 @@ local utfchar, utfbyte = utf.char, utf.byte ----- loadstripped = utilities.lua.loadstripped ----- setmetatableindex = table.setmetatableindex --- local loadstripped = _LUAVERSION < 5.2 and load or function(str) --- return load(dump(load(str),true)) -- it only makes sense in luajit and luatex where we have a stipped load --- end +local loadstripped = nil -local loadstripped = function(str,shortcuts) - if shortcuts then - return load(dump(load(str),true),nil,nil,shortcuts) - else - return load(dump(load(str),true)) +if _LUAVERSION < 5.2 then + + loadstripped = function(str,shortcuts) + return load(str) + end + +else + + loadstripped = function(str,shortcuts) + if shortcuts then + return load(dump(load(str),true),nil,nil,shortcuts) + else + return load(dump(load(str),true)) + end end + end -- todo: make a special namespace for the formatter @@ -305,60 +313,58 @@ local template = [[ return function(%s) return %s end ]] --- local environment = { --- lpeg = lpeg, --- type = type, --- string = string, --- number = number, --- table = table, --- utf = utf, --- } --- --- local preamble = [[ --- local type = type --- local tostring = tostring --- local tonumber = tonumber --- local format = string.format --- local concat = table.concat --- local signed = number.signed --- local points = number.points --- local basepoints = number.basepoints --- local utfchar = utf.char --- local utfbyte = utf.byte --- local lpegmatch = lpeg.match --- local nspaces = string.nspaces --- local tracedchar = string.tracedchar --- local autosingle = string.autosingle --- local autodouble = string.autodouble --- local sequenced = table.sequenced --- local formattednumber = number.formatted --- local sparseexponent = number.sparseexponent --- ]] - -local environment = { - global = global or _G, - lpeg = lpeg, - type = type, - tostring = tostring, - tonumber = tonumber, - format = string.format, - concat = table.concat, - signed = number.signed, - points = number.points, - basepoints = number.basepoints, - utfchar = utf.char, - utfbyte = utf.byte, - lpegmatch = lpeg.match, - nspaces = string.nspaces, - tracedchar = string.tracedchar, - autosingle = string.autosingle, - autodouble = string.autodouble, - sequenced = table.sequenced, - formattednumber = number.formatted, - sparseexponent = number.sparseexponent, -} +local preamble, environment = "", { } + +if _LUAVERSION < 5.2 then + + preamble = [[ +local lpeg=lpeg +local type=type +local tostring=tostring +local tonumber=tonumber +local format=string.format +local concat=table.concat +local signed=number.signed +local points=number.points +local basepoints= number.basepoints +local utfchar=utf.char +local utfbyte=utf.byte +local lpegmatch=lpeg.match +local nspaces=string.nspaces +local tracedchar=string.tracedchar +local autosingle=string.autosingle +local autodouble=string.autodouble +local sequenced=table.sequenced +local formattednumber=number.formatted +local sparseexponent=number.sparseexponent + ]] + +else + + environment = { + global = global or _G, + lpeg = lpeg, + type = type, + tostring = tostring, + tonumber = tonumber, + format = string.format, + concat = table.concat, + signed = number.signed, + points = number.points, + basepoints = number.basepoints, + utfchar = utf.char, + utfbyte = utf.byte, + lpegmatch = lpeg.match, + nspaces = string.nspaces, + tracedchar = string.tracedchar, + autosingle = string.autosingle, + autodouble = string.autodouble, + sequenced = table.sequenced, + formattednumber = number.formatted, + sparseexponent = number.sparseexponent, + } -local preamble = "" +end -- -- -- @@ -869,14 +875,26 @@ strings.formatters = { } -- table (metatable) in which case we could better keep a count and -- clear that table when a threshold is reached -function strings.formatters.new() - local e = { } -- better make a copy as we can overload - for k, v in next, environment do - e[k] = v +if _LUAVERSION < 5.2 then + + function strings.formatters.new() + local t = { _extensions_ = { }, _preamble_ = preamble, _environment_ = { }, _type_ = "formatter" } + setmetatable(t, { __index = make, __call = use }) + return t end - local t = { _extensions_ = { }, _preamble_ = "", _environment_ = e, _type_ = "formatter" } - setmetatable(t, { __index = make, __call = use }) - return t + +else + + function strings.formatters.new() + local e = { } -- better make a copy as we can overload + for k, v in next, environment do + e[k] = v + end + local t = { _extensions_ = { }, _preamble_ = "", _environment_ = e, _type_ = "formatter" } + setmetatable(t, { __index = make, __call = use }) + return t + end + end -- function strings.formatters.new() @@ -921,9 +939,19 @@ patterns.luaquoted = Cs(Cc('"') * ((1-S('"\n'))^1 + P('"')/'\\"' + P('\n')/'\\n" -- add(formatters,"tex", [[lpegmatch(texescape,%s)]],[[local texescape = lpeg.patterns.texescape]]) -- add(formatters,"lua", [[lpegmatch(luaescape,%s)]],[[local luaescape = lpeg.patterns.luaescape]]) -add(formatters,"xml", [[lpegmatch(xmlescape,%s)]],{ xmlescape = lpeg.patterns.xmlescape }) -add(formatters,"tex", [[lpegmatch(texescape,%s)]],{ texescape = lpeg.patterns.texescape }) -add(formatters,"lua", [[lpegmatch(luaescape,%s)]],{ luaescape = lpeg.patterns.luaescape }) +if _LUAVERSION < 5.2 then + + add(formatters,"xml",[[lpegmatch(xmlescape,%s)]],"local xmlescape = lpeg.patterns.xmlescape") + add(formatters,"tex",[[lpegmatch(texescape,%s)]],"local texescape = lpeg.patterns.texescape") + add(formatters,"lua",[[lpegmatch(luaescape,%s)]],"local luaescape = lpeg.patterns.luaescape") + +else + + add(formatters,"xml",[[lpegmatch(xmlescape,%s)]],{ xmlescape = lpeg.patterns.xmlescape }) + add(formatters,"tex",[[lpegmatch(texescape,%s)]],{ texescape = lpeg.patterns.texescape }) + add(formatters,"lua",[[lpegmatch(luaescape,%s)]],{ luaescape = lpeg.patterns.luaescape }) + +end -- -- yes or no: -- diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua index ae44269..d235520 100644 --- a/lualibs-util-tab.lua +++ b/lualibs-util-tab.lua @@ -316,7 +316,7 @@ function table.fastserialize(t,prefix) -- not sorted -- only number and string indices (currently) - local r = { prefix or "return" } + local r = { type(prefix) == "string" and prefix or "return" } local m = 1 local function fastserialize(t,outer) -- no mixes @@ -376,7 +376,6 @@ function table.fastserialize(t,prefix) end return r end - return concat(fastserialize(t,true)) end |