summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-11-02 00:38:59 +0800
committerruki <[email protected]>2019-11-01 21:53:55 +0800
commit6d4ad7fe1c5f8ac44b420a8cb5de0bc9cd0d37b6 (patch)
tree059cf0d49ec8d7ef2f13c0c415f79619663c034c
parent2359364d3b4d8ff6c282be9ff721fc2d820d15a9 (diff)
improve bytes ctor
-rw-r--r--tests/modules/bytes/test.lua1
-rw-r--r--xmake/core/base/bytes.lua15
2 files changed, 16 insertions, 0 deletions
diff --git a/tests/modules/bytes/test.lua b/tests/modules/bytes/test.lua
index 262f9ee65..52b754c0a 100644
--- a/tests/modules/bytes/test.lua
+++ b/tests/modules/bytes/test.lua
@@ -6,6 +6,7 @@ function test_ctor(t)
t:are_equal(bytes("123456789"):size(), 9)
t:are_equal(bytes(10):size(), 10)
t:are_equal(bytes(bytes("123"), bytes("456"), bytes("789")):str(), "123456789")
+ t:are_equal(bytes({bytes("123"), bytes("456"), bytes("789")}):str(), "123456789")
end
function test_clone(t)
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index 8b6fbee3c..377341480 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -104,6 +104,21 @@ function _instance.new(...)
end
instance._MANAGED = true
instance._READONLY = false
+ elseif not arg2 and arg1[1] and type(arg1[1]) == 'table' then
+ -- bytes({bytes1, bytes2, ...}) : allocates and concat buffer from a list of byte buffers (table)
+ args = arg1
+ instance._SIZE = 0
+ for _, b in ipairs(args) do
+ instance._SIZE = instance._SIZE + b:size()
+ end
+ instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ffi.C.xm_ffi_malloc(instance._SIZE)), ffi.C.xm_ffi_free)
+ local offset = 0
+ for _, b in ipairs(args) do
+ ffi.copy(instance._CDATA + offset, b._CDATA, b:size())
+ offset = offset + b:size()
+ end
+ instance._MANAGED = true
+ instance._READONLY = false
elseif not arg2 and arg1:size() then
-- bytes(bytes): allocates a buffer from another one (strict replica, sharing memory)
local b = arg1