External strings

Strings can use external buffers to store their contents.
This commit is contained in:
Roberto Ierusalimschy
2023-11-09 17:05:42 -03:00
parent 7f4906f565
commit 024f9064f1
9 changed files with 195 additions and 14 deletions

View File

@@ -157,6 +157,12 @@ else -- compatible coercion
assert(tostring(-1203 + 0.0) == "-1203")
end
local function topointer (s)
return string.format("%p", s)
end
do -- tests for '%p' format
-- not much to test, as C does not specify what '%p' does.
-- ("The value of the pointer is converted to a sequence of printing
@@ -180,18 +186,18 @@ do -- tests for '%p' format
do
local t1 = {}; local t2 = {}
assert(string.format("%p", t1) ~= string.format("%p", t2))
assert(topointer(t1) ~= topointer(t2))
end
do -- short strings are internalized
local s1 = string.rep("a", 10)
local s2 = string.rep("aa", 5)
assert(string.format("%p", s1) == string.format("%p", s2))
assert(topointer(s1) == topointer(s2))
end
do -- long strings aren't internalized
local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
assert(string.format("%p", s1) ~= string.format("%p", s2))
assert(topointer(s1) ~= topointer(s2))
end
end
@@ -521,6 +527,20 @@ else
testpfs("P", str, {})
end
if T == nil then
(Message or print)('\n >>> testC not active: skipping external strings tests <<<\n')
else
print("testing external strings")
local x = T.externKstr("hello") -- external fixed short string
assert(x == "hello")
local x = T.externstr("hello") -- external allocated short string
assert(x == "hello")
x = string.rep("a", 100) -- long string
local y = T.externKstr(x) -- external fixed long string
assert(y == x)
local z = T.externstr(x) -- external allocated long string
assert(z == y)
end
print('OK')