summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-21 00:35:04 +0800
committerruki <[email protected]>2021-09-21 00:35:04 +0800
commitde47ad0121ca4155777005976b05debeda67aca6 (patch)
tree0cb535481ec2f4522ed73f73b663d30e40a054b5
parent6bd955fbacc03792d8b36dffa087967e9e056e31 (diff)
add byteof
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/libc/byteof.c58
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--tests/modules/bytes/test.lua3
-rw-r--r--xmake/core/base/bytes.lua4
-rw-r--r--xmake/core/base/libc.lua9
6 files changed, 73 insertions, 4 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index f7d5a1c20..e4ac9f0f5 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -230,6 +230,7 @@ tb_int_t xm_libc_strndup(lua_State* lua);
tb_int_t xm_libc_dataptr(lua_State* lua);
tb_int_t xm_libc_ptraddr(lua_State* lua);
tb_int_t xm_libc_diffptr(lua_State* lua);
+tb_int_t xm_libc_byteof(lua_State* lua);
#ifdef XM_CONFIG_API_HAVE_CURSES
// register curses
@@ -433,6 +434,7 @@ static luaL_Reg const g_libc_functions[] =
, { "dataptr", xm_libc_dataptr }
, { "ptraddr", xm_libc_ptraddr }
, { "diffptr", xm_libc_diffptr }
+, { "byteof", xm_libc_byteof }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/libc/byteof.c b/core/src/xmake/libc/byteof.c
new file mode 100644
index 000000000..9d0031559
--- /dev/null
+++ b/core/src/xmake/libc/byteof.c
@@ -0,0 +1,58 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file byteof.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "byteof"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_libc_byteof(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get data
+ tb_pointer_t data = tb_null;
+ if (xm_lua_ispointer(lua, 1))
+ data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ else if (lua_isstring(lua, 1))
+ data = (tb_pointer_t)luaL_checkstring(lua, 1);
+ else xm_libc_return_error(lua, "libc.byteof(invalid data)!");
+
+ // get offset
+ tb_int_t offset = 0;
+ if (lua_isnumber(lua, 2))
+ offset = (tb_int_t)lua_tonumber(lua, 2);
+ else xm_libc_return_error(lua, "libc.byteof(invalid offset)!");
+ lua_pushinteger(lua, ((tb_byte_t const*)data)[offset]);
+ return 1;
+}
+
+
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index b766b272d..a37003b37 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -123,6 +123,7 @@ xmake_C_FILES += \
libc/dataptr \
libc/ptraddr \
libc/diffptr \
+ libc/byteof \
libc/strndup
iswin =
diff --git a/tests/modules/bytes/test.lua b/tests/modules/bytes/test.lua
index ac12db29c..4cb272c94 100644
--- a/tests/modules/bytes/test.lua
+++ b/tests/modules/bytes/test.lua
@@ -10,7 +10,6 @@ function test_ctor(t)
t:are_equal(bytes({bytes("123"), bytes("456"), bytes("789")}):str(), "123456789")
end
---[[
function test_clone(t)
t:are_equal(bytes(10):clone():size(), 10)
t:are_equal(bytes("123456789"):clone():str(), "123456789")
@@ -36,4 +35,4 @@ end
function test_concat(t)
t:are_equal((bytes("123") .. bytes("456")):str(), "123456")
t:are_equal(bytes(bytes("123"), bytes("456")):str(), "123456")
-end]]
+end
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index f35f20790..0486f0178 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -138,7 +138,7 @@ function _instance.new(...)
os.raise("incorrect bounds(%d-%d)!", start, last)
end
instance._SIZE = last - start + 1
- instance._CDATA = b:cdata() - 1 + start
+ instance._CDATA = libc.diffptr(b:cdata(), -1 + start)
instance._REF = b -- keep lua ref for GC
instance._MANAGED = false
instance._READONLY = b:readonly()
@@ -390,7 +390,7 @@ function _instance:__index(key)
if key < 1 or key > self:size() then
os.raise("%s: index(%d/%d) out of bounds!", self, key, self:size())
end
- return self._CDATA[key - 1]
+ return libc.byteof(self._CDATA, key - 1)
elseif type(key) == "table" then
local start, last = key[1], key[2]
return self:slice(start, last)
diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua
index 18c14a669..d3f02d7f5 100644
--- a/xmake/core/base/libc.lua
+++ b/xmake/core/base/libc.lua
@@ -30,6 +30,7 @@ libc._strndup = libc._strndup or libc.strndup
libc._dataptr = libc._dataptr or libc.dataptr
libc._ptraddr = libc._ptraddr or libc.ptraddr
libc._diffptr = libc._diffptr or libc.diffptr
+libc._byteof = libc._byteof or libc.byteof
-- load modules
local ffi = xmake._LUAJIT and require("ffi")
@@ -94,6 +95,14 @@ function libc.strndup(s, n)
end
end
+function libc.byteof(data, offset)
+ if ffi then
+ return data[offset]
+ else
+ return libc._byteof(data, offset)
+ end
+end
+
function libc.diffptr(data, offset)
if ffi then
return data + offset