summaryrefslogtreecommitdiff
path: root/xmake/core/base/bytes.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-07 00:53:39 +0800
committerruki <[email protected]>2022-04-06 23:22:21 +0800
commitc48a7d959049ad14b6ea09baca682dcbaf88639c (patch)
treeb023420a92e8c74aeea35b3ce02ba55f98395729 /xmake/core/base/bytes.lua
parent79e14625b3977bf7953c2150e584332a0ea8dd16 (diff)
improve bytes
Diffstat (limited to 'xmake/core/base/bytes.lua')
-rw-r--r--xmake/core/base/bytes.lua28
1 files changed, 23 insertions, 5 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index b95260237..f90adfb84 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -205,13 +205,32 @@ function _instance:copy(src)
if self:readonly() then
os.raise("%s: cannot be modified!", self)
end
- if type(src) == 'string' then
+ if type(src) == "string" then
src = bytes(src)
end
- if src:size() ~= self:size() then
- os.raise("%s: cannot copy bytes, src and dst must have same size(%d->%d)!", self, src:size(), self:size())
+ if src:size() > self:size() then
+ os.raise("%s: cannot copy bytes, src:size(%d) must be smaller than %d!", self, src:size(), self:size())
end
- libc.memcpy(self:cdata(), src:cdata(), self:size())
+ libc.memcpy(self:cdata(), src:cdata(), src:size())
+ return self
+end
+
+-- copy bytes to the given position
+function _instance:copy2(pos, src)
+ if self:readonly() then
+ os.raise("%s: cannot be modified!", self)
+ end
+ if type(src) == "string" then
+ src = bytes(src)
+ end
+ if pos < 1 or pos > self:size() then
+ os.raise("%s: invalid pos(%d)!", self, pos)
+ end
+ local leftsize = self:size() + 1 - pos
+ if src:size() > leftsize then
+ os.raise("%s: cannot copy bytes, src:size(%d) must be smaller than %d!", self, src:size(), leftsize)
+ end
+ libc.memcpy(self:cdata() + pos - 1, src:cdata(), src:size())
return self
end
@@ -224,7 +243,6 @@ end
-- dump whole bytes data
function _instance:dump()
-
local i = 0
local n = 147
local p = 0