mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 17:23:45 +01:00
Add table.rope with :write and :to_text
This commit is contained in:
parent
8c97425fbc
commit
d728157204
13
table.lua
13
table.lua
@ -33,6 +33,19 @@ function shuffle(table)
|
|||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local rope_metatable = {__index = {
|
||||||
|
write = function(self, text)
|
||||||
|
table.insert(self, text)
|
||||||
|
end,
|
||||||
|
to_text = function(self)
|
||||||
|
return table.concat(self)
|
||||||
|
end
|
||||||
|
}}
|
||||||
|
--> rope with simple metatable (:write(text) and :to_text())
|
||||||
|
function rope(table)
|
||||||
|
return setmetatable(table or {}, rope_metatable)
|
||||||
|
end
|
||||||
|
|
||||||
function is_circular(table)
|
function is_circular(table)
|
||||||
assert(type(table) == "table")
|
assert(type(table) == "table")
|
||||||
local known = {}
|
local known = {}
|
||||||
|
5
test.lua
5
test.lua
@ -27,6 +27,11 @@ do
|
|||||||
assert(nilget({a = {}}, "a", "b", "c") == nil)
|
assert(nilget({a = {}}, "a", "b", "c") == nil)
|
||||||
assert(nilget(nil, "a", "b", "c") == nil)
|
assert(nilget(nil, "a", "b", "c") == nil)
|
||||||
assert(nilget(nil, "a", nil, "c") == nil)
|
assert(nilget(nil, "a", nil, "c") == nil)
|
||||||
|
local rope = modlib.table.rope{}
|
||||||
|
rope:write"hello"
|
||||||
|
rope:write" "
|
||||||
|
rope:write"world"
|
||||||
|
assert(rope:to_text() == "hello world", rope:to_text())
|
||||||
end
|
end
|
||||||
|
|
||||||
-- heap
|
-- heap
|
||||||
|
Loading…
Reference in New Issue
Block a user