[latex3-commits] [git/LaTeX3-latex3-lualibs] new-libs: Import from ConTeXt (83ac65f)

Marcel Fabian Krüger tex at 2krueger.de
Sun Sep 18 06:02:41 CEST 2022


Repository : https://github.com/latex3/lualibs
On branch  : new-libs
Link       : https://github.com/latex3/lualibs/commit/83ac65fcec3aa0655e881c4d540a7a6575e8a22d

>---------------------------------------------------------------

commit 83ac65fcec3aa0655e881c4d540a7a6575e8a22d
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sun Sep 18 06:02:41 2022 +0200

    Import from ConTeXt


>---------------------------------------------------------------

83ac65fcec3aa0655e881c4d540a7a6575e8a22d
 lualibs-math.lua     | 14 ++++++++++++--
 lualibs-os.lua       |  2 +-
 lualibs-util-deb.lua | 22 ++++++++++++++++++++++
 lualibs-util-jsn.lua |  9 +++++++++
 lualibs-util-prs.lua | 40 ++++++++++++++++++++++++++++++----------
 lualibs-util-tab.lua | 40 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 114 insertions(+), 13 deletions(-)

diff --git a/lualibs-math.lua b/lualibs-math.lua
index 69f0a19..6105bc3 100644
--- a/lualibs-math.lua
+++ b/lualibs-math.lua
@@ -14,9 +14,19 @@ end
 
 if not math.round then
 
-    local floor = math.floor
+    if xmath then
+
+        math.round = xmath.round
+
+    else
 
-    function math.round(x) return floor(x + 0.5) end
+        local floor = math.floor
+
+        function math.round(x)
+            return x < 0 and -floor(-x + 0.5) or floor(x + 0.5)
+        end
+
+    end
 
 end
 
diff --git a/lualibs-os.lua b/lualibs-os.lua
index 7f4f387..faae768 100644
--- a/lualibs-os.lua
+++ b/lualibs-os.lua
@@ -168,7 +168,7 @@ if not os.__getenv__ then
         end
 
         function os.getenv(k)
-            local K = upper(k)
+            local K = upper(k) -- hm utf
             local v = osenv[K] or osgetenv(K) or osgetenv(k)
             if v == "" then
                 return nil
diff --git a/lualibs-util-deb.lua b/lualibs-util-deb.lua
index bd94b6d..10e5731 100644
--- a/lualibs-util-deb.lua
+++ b/lualibs-util-deb.lua
@@ -346,3 +346,25 @@ debugger.showtraceback = showtraceback
 -- debug.showtraceback = showtraceback
 
 -- showtraceback()
+
+-- For now also here because we want it in mtxrun (taken from lmt file):
+
+if luac then
+
+    local show, dump = luac.print, string.dump
+
+    function luac.inspect(v)
+        if type(v) == "function" then
+            local ok, str = xpcall(dump,function() end,v)
+            if ok then
+                v = str
+            end
+        end
+        if type(v) == "string" then
+            show(v,true)
+        else
+            print(v)
+        end
+    end
+
+end
diff --git a/lualibs-util-jsn.lua b/lualibs-util-jsn.lua
index 7467f84..2d78561 100644
--- a/lualibs-util-jsn.lua
+++ b/lualibs-util-jsn.lua
@@ -269,6 +269,8 @@ do
                             k = lpegmatch(escaper,k) or k
                             v = lpegmatch(escaper,v) or v
                             n = n + 1 t[n] = f_key_val_str(depth,k,v)
+                        elseif i > 1 then
+                            n = n - 1
                         end
                     elseif tv == "table" then
                         local l = #v
@@ -284,6 +286,9 @@ do
                             end
                         elseif next(v) then
                             tojsonpp(v,k,depth,level+1,0)
+                        elseif i > 1 then
+                            n = n - 1
+                            -- we don't know if we have a hash or string
                         end
                     elseif tv == "boolean" then
                         if tk == "number" then
@@ -301,6 +306,8 @@ do
                             else
                                 t[n] = f_key_val_nop(depth,k)
                             end
+                        elseif i > 1 then
+                            n = n - 1
                         end
                     else
                         if tk == "number" then
@@ -310,6 +317,8 @@ do
                             k = lpegmatch(escaper,k) or k
                             n = n + 1
                             t[n] = f_key_val_null(depth,k)
+                        elseif i > 1 then
+                            n = n - 1
                         end
                     end
                 end
diff --git a/lualibs-util-prs.lua b/lualibs-util-prs.lua
index 204611d..635b610 100644
--- a/lualibs-util-prs.lua
+++ b/lualibs-util-prs.lua
@@ -622,12 +622,6 @@ end
 -- 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)
--- parsers.stepper(" 1 : 3, ,7 ")
--- parsers.stepper("1:4,9:13,24:*",30)
-
 local function ranger(first,last,n,action)
     if not first then
         -- forget about it
@@ -655,14 +649,29 @@ local stepper  = spacers * ( cardinal * ( spacers * S(":-") * spacers * ( cardin
                * Carg(1) * Carg(2) / ranger * S(", ")^0 )^1 * endofstring -- we're sort of strict (could do without endofstring)
 
 function parsers.stepper(str,n,action)
+    local ts = type(str)
     if type(n) == "function" then
-        lpegmatch(stepper,str,1,false,n or print)
-    else
+        if ts == "number" then
+            n(str)
+        elseif ts == "table" then
+            for i=1,#str do
+                n(str[i])
+            end
+        else
+            lpegmatch(stepper,str,1,false,n or print)
+        end
+    elseif ts == "string" then
         lpegmatch(stepper,str,1,n,action or print)
     end
 end
 
---
+-- 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)
+-- parsers.stepper(" 1 : 3, ,7 ")
+-- parsers.stepper("1:4,9:13,24:*",30)
+-- parsers.stepper(1,print)
+-- parsers.stepper({1,3,4},print)
 
 local pattern_math = Cs((P("%")/"\\percent " +  P("^")           * Cc("{") * lpegpatterns.integer * Cc("}") + anything)^0)
 local pattern_text = Cs((P("%")/"\\percent " + (P("^")/"\\high") * Cc("{") * lpegpatterns.integer * Cc("}") + anything)^0)
@@ -806,11 +815,20 @@ local pattern = Cf( Ct("") *
         (              Cg(Cc("day")   * cardinal)
           *  S("-/") * Cg(Cc("month") * cardinal)
           *  S("-/") * Cg(Cc("year")  * p_year)
+        ) +
+        (              Cg(Cc("year")  * p_year)
+          *  S("-/") * Cg(Cc("month") * cardinal)
+        ) +
+        (              Cg(Cc("month")  * cardinal)
+          *  S("-/") * Cg(Cc("year")   * p_year)
         )
     )
-      *  P(" ")  * Cg(Cc("hour")   * cardinal)
+      *  (
+         P(" ")  * Cg(Cc("hour")   * cardinal)
       *  P(":")  * Cg(Cc("min")    * cardinal)
       * (P(":")  * Cg(Cc("sec")    * cardinal))^-1
+      + P(-1) )
+
 , rawset)
 
 lpegpatterns.splittime = pattern
@@ -819,6 +837,8 @@ function parsers.totime(str)
     return lpegmatch(pattern,str)
 end
 
+-- inspect(parsers.totime("2019-03-05"))
+-- inspect(parsers.totime("2019-03-05 12:12:12"))
 -- print(os.time(parsers.totime("2019-03-05 12:12:12")))
 -- print(os.time(parsers.totime("2019/03/05 12:12:12")))
 -- print(os.time(parsers.totime("05-03-2019 12:12:12")))
diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua
index a92b47b..64fa1af 100644
--- a/lualibs-util-tab.lua
+++ b/lualibs-util-tab.lua
@@ -978,3 +978,43 @@ end
 --         return remove(t,random(1,n))
 --     end
 -- end
+
+function combine(target,source)
+    -- no copy so if that is needed one needs to deepcopy source first
+    if target then
+        for k, v in next, source do
+            if type(v) == "table" then
+               target[k] = combine(target[k],source[k])
+            else
+               target[k] = v
+            end
+        end
+        return target
+    else
+        return source
+    end
+end
+
+table.combine = combine
+
+-- If needed we can add something (some discussion on the list but I'm not sure if
+-- it makes sense because merging such mixed tables is quite unusual.
+--
+-- function table.himerged(...)
+--     local result = { }
+--     local r      = 0
+--     for i=1,select("#",...) do
+--         local s = select(i,...)
+--         if s then
+--             for k, v in next, s do
+--                 if type(k) == "number"  then
+--                     r = r + 1
+--                     result[r] = v
+--                 else
+--                     result[k] = v
+--                 end
+--             end
+--         end
+--     end
+--     return result
+-- end





More information about the latex3-commits mailing list.