Add table.rope with :write and :to_text

This commit is contained in:
Lars Mueller 2021-03-04 08:30:19 +01:00
parent 8c97425fbc
commit d728157204
2 changed files with 18 additions and 0 deletions

@ -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 = {}

@ -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