diff options
| author | ruki <[email protected]> | 2020-01-19 21:43:08 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-01-19 21:43:08 +0800 |
| commit | 382488036184236e17be24ede9cca18ae8af32c5 (patch) | |
| tree | c9ed704cb7ddc3cc95d5f5833086124a4cbf3759 /xmake/core/base/bytes.lua | |
| parent | 7fa954bf793944bdf42175a372277fcd386d62af (diff) | |
| parent | d0b39e229426a336f71a0cc24f6319a47e2ce975 (diff) | |
Merge pull request #669 from OpportunityLiu/tab-complete
Improve tab complete
Diffstat (limited to 'xmake/core/base/bytes.lua')
| -rw-r--r-- | xmake/core/base/bytes.lua | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index 54343a962..76579ab0f 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -37,7 +37,7 @@ ffi.cdef[[ -- new a bytes instance -- --- bytes(size): allocates a buffer of given size +-- bytes(size[, init]): allocates a buffer of given size, init with given number or char value -- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not) -- bytes(str): mounts a buffer from the given string -- bytes(bytes, start, last): mounts a buffer from another one, with start/last limits @@ -51,9 +51,9 @@ function _instance.new(...) local instance = table.inherit(_instance) if type(arg1) == "number" then local size = arg1 - local ptr = arg2 - if ptr then + if type(arg2) == "cdata" then -- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not) + local ptr = arg2 local manage = arg3 if manage then instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.free) @@ -63,8 +63,21 @@ function _instance.new(...) instance._MANAGED = false end else - -- bytes(size): allocates a buffer of given size - ptr = ffi.C.malloc(size) + -- bytes(size[, init]): allocates a buffer of given size + local init + if arg2 then + if type(arg2) == "number" then + init = arg2 + elseif type(arg2) == "string" then + init = arg2:byte() + else + os.raise("invalid arguments #2 for bytes(size, ...), cdata, string, number or nil expected!") + end + end + local ptr = ffi.C.malloc(size) + if init then + ffi.fill(ptr, size, init) + end instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.free) instance._MANAGED = true end @@ -429,8 +442,8 @@ end -- register call function setmetatable(bytes, { - __call = function (_, ...) - return bytes.new(...) + __call = function (_, ...) + return bytes.new(...) end, __todisplay = function() return todisplay(bytes.new) |
