summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-20 23:38:02 +0800
committerruki <[email protected]>2026-01-20 23:38:02 +0800
commita9d5fe27bb69cc35022e9c8b80ac9d5f89f570c0 (patch)
tree7d48629cd895877d91fdab0e9648d03419ca51f1 /core/src
parent92423451b623e79ac464d388bfb1bfed2780b792 (diff)
add utf8.sub and lastof
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c4
-rw-r--r--core/src/xmake/utf8/lastof.c65
-rw-r--r--core/src/xmake/utf8/sub.c88
3 files changed, 157 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index ff7c79061..098a808c8 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -285,6 +285,8 @@ tb_int_t xm_utf8_char(lua_State *lua);
tb_int_t xm_utf8_codepoint(lua_State *lua);
tb_int_t xm_utf8_offset(lua_State *lua);
tb_int_t xm_utf8_codes(lua_State *lua);
+tb_int_t xm_utf8_sub(lua_State *lua);
+tb_int_t xm_utf8_lastof(lua_State *lua);
// the string functions
tb_int_t xm_string_trim(lua_State *lua);
@@ -597,6 +599,8 @@ static luaL_Reg const g_utf8_functions[] = {
{"codepoint", xm_utf8_codepoint},
{"len", xm_utf8_len},
{"offset", xm_utf8_offset},
+ {"sub", xm_utf8_sub},
+ {"lastof", xm_utf8_lastof},
{tb_null, tb_null}
};
diff --git a/core/src/xmake/utf8/lastof.c b/core/src/xmake/utf8/lastof.c
new file mode 100644
index 000000000..8c5ec7dd3
--- /dev/null
+++ b/core/src/xmake/utf8/lastof.c
@@ -0,0 +1,65 @@
+/*!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, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file lastof.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "utf8.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* utf8.lastof(s, substr)
+ */
+tb_int_t xm_utf8_lastof(lua_State *lua) {
+ size_t len;
+ tb_char_t const* s = luaL_checklstring(lua, 1, &len);
+ size_t sublen;
+ tb_char_t const* sub = luaL_checklstring(lua, 2, &sublen);
+
+ if (sublen == 0) {
+ lua_pushnil(lua);
+ return 1;
+ }
+
+ tb_char_t const* p = s;
+ tb_char_t const* last = tb_null;
+
+ while (1) {
+ p = tb_strstr(p, sub);
+ if (!p) break;
+ last = p;
+ p += 1;
+ }
+
+ if (last) {
+ tb_long_t count = xm_utf8_len_impl(s, len, 1, last - s, tb_true, tb_null);
+ if (count < 0) {
+ lua_pushnil(lua);
+ return 1;
+ }
+ lua_pushinteger(lua, count + 1);
+ } else {
+ lua_pushnil(lua);
+ }
+ return 1;
+}
diff --git a/core/src/xmake/utf8/sub.c b/core/src/xmake/utf8/sub.c
new file mode 100644
index 000000000..c86c60f22
--- /dev/null
+++ b/core/src/xmake/utf8/sub.c
@@ -0,0 +1,88 @@
+/*!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, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file sub.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "utf8.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* utf8.sub(s, i [, j])
+ */
+tb_int_t xm_utf8_sub(lua_State *lua) {
+ size_t len;
+ tb_char_t const* s = luaL_checklstring(lua, 1, &len);
+ lua_Integer i = luaL_checkinteger(lua, 2);
+ lua_Integer j = luaL_optinteger(lua, 3, -1);
+
+ // map i (char index) to byte offset
+ tb_long_t start_byte = 0;
+ if (i > 0) {
+ start_byte = xm_utf8_offset_impl(s, len, i, 1);
+ } else if (i < 0) {
+ start_byte = xm_utf8_offset_impl(s, len, i, len + 1);
+ } else {
+ start_byte = 1;
+ }
+
+ if (start_byte == -1) {
+ if (i > 0) {
+ lua_pushliteral(lua, "");
+ return 1;
+ } else {
+ start_byte = 1;
+ }
+ } else if (start_byte == 0) {
+ if (i < 0) {
+ start_byte = 1;
+ } else {
+ lua_pushliteral(lua, "");
+ return 1;
+ }
+ }
+
+ // map j (char index) to byte offset (end)
+ tb_long_t end_byte = 0;
+ if (j >= 0) {
+ end_byte = xm_utf8_offset_impl(s, len, j + 1, 1);
+ } else {
+ end_byte = xm_utf8_offset_impl(s, len, j + 1, len + 1);
+ }
+
+ if (end_byte == -1) {
+ if (j >= 0) end_byte = len + 1;
+ else end_byte = 1;
+ } else if (end_byte == 0) {
+ if (j >= 0) end_byte = len + 1;
+ else end_byte = 1;
+ }
+
+ if (end_byte <= start_byte) {
+ lua_pushliteral(lua, "");
+ return 1;
+ }
+
+ lua_pushlstring(lua, s + start_byte - 1, end_byte - start_byte);
+ return 1;
+}