mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
27278fb887 | ||
|
8627638db0 | ||
|
1559803c70 | ||
|
258e523219 | ||
|
16848d83a5 | ||
|
ca338b8833 | ||
|
ca36473904 | ||
|
717745fe79 | ||
|
688de3368e | ||
|
7412706277 | ||
|
78144dbdb8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
||||
__*
|
||||
*.tmp
|
||||
*.swp
|
@@ -329,7 +329,7 @@ f(10, 5) -- Returns 25
|
||||
### lume.serialize(x)
|
||||
Serializes the argument `x` into a string which can be loaded again using
|
||||
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
||||
serialized. Circular references are not handled; all nested tables are
|
||||
serialized. Circular references will result in an error; all nested tables are
|
||||
serialized as unique tables.
|
||||
```lua
|
||||
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
||||
@@ -383,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
|
||||
by a space.
|
||||
```lua
|
||||
-- Assuming the file is called "example.lua" and the next line is 12:
|
||||
lume.trace("hello", 1234) -- Prints "[example.lua:12] hello 1234"
|
||||
lume.trace("hello", 1234) -- Prints "example.lua:12: hello 1234"
|
||||
```
|
||||
|
||||
### lume.dostring(str)
|
||||
|
44
lume.lua
44
lume.lua
@@ -7,7 +7,7 @@
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "2.2.0" }
|
||||
local lume = { _version = "2.2.2" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -536,19 +536,41 @@ function lume.lambda(str)
|
||||
end
|
||||
|
||||
|
||||
function lume.serialize(x)
|
||||
local f = { string = function(v) return string.format("%q", v) end,
|
||||
number = tostring, boolean = tostring }
|
||||
f.table = function(t)
|
||||
local serialize
|
||||
|
||||
local serialize_map = {
|
||||
[ "boolean" ] = tostring,
|
||||
[ "nil" ] = tostring,
|
||||
[ "string" ] = function(v) return string.format("%q", v) end,
|
||||
[ "number" ] = function(v)
|
||||
if v ~= v then return "0/0" -- nan
|
||||
elseif v == 1 / 0 then return "1/0" -- inf
|
||||
elseif v == -1 / 0 then return "-1/0" end -- -inf
|
||||
return tostring(v)
|
||||
end,
|
||||
[ "table" ] = function(t, stk)
|
||||
stk = stk or {}
|
||||
if stk[t] then error("circular reference") end
|
||||
local rtn = {}
|
||||
stk[t] = true
|
||||
for k, v in pairs(t) do
|
||||
rtn[#rtn + 1] = "[" .. f[type(k)](k) .. "]=" .. f[type(v)](v) .. ","
|
||||
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
|
||||
end
|
||||
return "{" .. table.concat(rtn) .. "}"
|
||||
stk[t] = nil
|
||||
return "{" .. table.concat(rtn, ",") .. "}"
|
||||
end
|
||||
local err = function(t,k) error("unsupported serialize type: " .. k) end
|
||||
setmetatable(f, { __index = err })
|
||||
return f[type(x)](x)
|
||||
}
|
||||
|
||||
setmetatable(serialize_map, {
|
||||
__index = function(t, k) error("unsupported serialize type: " .. k) end
|
||||
})
|
||||
|
||||
serialize = function(x, stk)
|
||||
return serialize_map[type(x)](x, stk)
|
||||
end
|
||||
|
||||
function lume.serialize(x)
|
||||
return serialize(x)
|
||||
end
|
||||
|
||||
|
||||
@@ -618,7 +640,7 @@ end
|
||||
|
||||
function lume.trace(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
|
||||
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
||||
for i = 1, select("#", ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local tester = require "tester"
|
||||
local tester = require "util.tester"
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
|
||||
@@ -484,6 +484,10 @@ tests["lume.serialize, lume.deserialize"] = function()
|
||||
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
||||
local s = lume.serialize(t)
|
||||
testeq( lume.deserialize(s), t )
|
||||
testeq( lume.deserialize(lume.serialize(math.huge)), math.huge )
|
||||
testeq( lume.deserialize(lume.serialize(-math.huge)), -math.huge )
|
||||
local x = lume.deserialize(lume.serialize(0 / 0)) -- nan
|
||||
testeq( x ~= x, true )
|
||||
end
|
||||
|
||||
-- lume.split
|
||||
@@ -535,7 +539,7 @@ tests["lume.trace"] = function()
|
||||
local oldprint = print
|
||||
local file, line, msg
|
||||
print = function(x)
|
||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
||||
file, line, msg = x:match("(.-):(.-): (.*)")
|
||||
end
|
||||
lume.trace("Hi world", 123.456, 1, nil)
|
||||
print = oldprint
|
Reference in New Issue
Block a user