summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-20 23:43:50 +0800
committerruki <[email protected]>2026-01-20 23:43:50 +0800
commit23710584abfd0bb9c12c207066c145e6feee81e5 (patch)
treeb72b9db4d290534618687622fe281c2112de2b9d
parenta9d5fe27bb69cc35022e9c8b80ac9d5f89f570c0 (diff)
fix utf8.find
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/utf8/find.c98
-rw-r--r--tests/modules/utf8/test.lua22
-rw-r--r--xmake/core/base/utf8.lua1
4 files changed, 123 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 098a808c8..9ef4ec68f 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -287,6 +287,7 @@ 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);
+tb_int_t xm_utf8_find(lua_State *lua);
// the string functions
tb_int_t xm_string_trim(lua_State *lua);
@@ -601,6 +602,7 @@ static luaL_Reg const g_utf8_functions[] = {
{"offset", xm_utf8_offset},
{"sub", xm_utf8_sub},
{"lastof", xm_utf8_lastof},
+ {"find", xm_utf8_find},
{tb_null, tb_null}
};
diff --git a/core/src/xmake/utf8/find.c b/core/src/xmake/utf8/find.c
new file mode 100644
index 000000000..2fbe7bf0c
--- /dev/null
+++ b/core/src/xmake/utf8/find.c
@@ -0,0 +1,98 @@
+/*!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 find.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "utf8.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* utf8.find(s, target [, init])
+ */
+tb_int_t xm_utf8_find(lua_State *lua) {
+ tb_assert_and_check_return_val(lua, 0);
+
+ 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);
+ lua_Integer init = luaL_optinteger(lua, 3, 1);
+
+ if (init > (lua_Integer)len) {
+ lua_pushnil(lua);
+ return 1;
+ }
+
+ if (sublen == 0) {
+ if (init <= 1) {
+ lua_pushinteger(lua, 1);
+ lua_pushinteger(lua, 0);
+ return 2;
+ } else {
+ lua_pushinteger(lua, init);
+ lua_pushinteger(lua, init - 1);
+ return 2;
+ }
+ }
+
+ tb_long_t start_byte = 0;
+ if (init > 0) {
+ start_byte = xm_utf8_offset_impl(s, len, init, 1);
+ } else if (init < 0) {
+ start_byte = xm_utf8_offset_impl(s, len, init, len + 1);
+ } else {
+ start_byte = 1;
+ }
+
+ if (start_byte <= 0) {
+ lua_pushnil(lua);
+ return 1;
+ }
+
+ tb_char_t const* p = tb_strstr(s + start_byte - 1, sub);
+ if (!p) {
+ lua_pushnil(lua);
+ return 1;
+ }
+
+ tb_long_t found_byte_start = p - s + 1;
+
+ tb_long_t char_start = 0;
+ if (found_byte_start > 1) {
+ char_start = xm_utf8_len_impl(s, len, 1, found_byte_start - 1, tb_true, tb_null);
+ if (char_start < 0) {
+ lua_pushnil(lua); return 1;
+ }
+ }
+ char_start += 1;
+
+ tb_long_t match_char_len = xm_utf8_len_impl(s, len, found_byte_start, found_byte_start + sublen - 1, tb_true, tb_null);
+ if (match_char_len < 0) {
+ lua_pushnil(lua); return 1;
+ }
+
+ lua_pushinteger(lua, char_start);
+ lua_pushinteger(lua, char_start + match_char_len - 1);
+ return 2;
+}
diff --git a/tests/modules/utf8/test.lua b/tests/modules/utf8/test.lua
index 51da441d9..8ec9495fb 100644
--- a/tests/modules/utf8/test.lua
+++ b/tests/modules/utf8/test.lua
@@ -106,3 +106,25 @@ function test_lastof(t)
t:are_equal(utf8.lastof("ABC", "D"), nil)
end
+
+function test_find(t)
+ t:are_equal({utf8.find("A", "A")}, {1, 1})
+ t:are_equal({utf8.find("ABC", "A")}, {1, 1})
+ t:are_equal({utf8.find("ABC", "B")}, {2, 2})
+ t:are_equal({utf8.find("ABC", "C")}, {3, 3})
+ t:are_equal({utf8.find("ABCA", "A")}, {1, 1})
+ t:are_equal({utf8.find("ABCA", "A", 2)}, {4, 4})
+ t:are_equal({utf8.find("ABCA", "A", 1)}, {1, 1})
+
+ t:are_equal({utf8.find("你好", "你")}, {1, 1})
+ t:are_equal({utf8.find("你好", "好")}, {2, 2})
+ t:are_equal({utf8.find("你好你", "你", 2)}, {3, 3})
+
+ t:are_equal({utf8.find("A你好A", "A")}, {1, 1})
+ t:are_equal({utf8.find("A你好A", "A", 2)}, {4, 4})
+ t:are_equal({utf8.find("A你好A", "好")}, {3, 3})
+
+ t:are_equal(utf8.find("ABC", "D"), nil)
+ t:are_equal({utf8.find("ABC", "")}, {1, 0})
+ t:are_equal({utf8.find("ABC", "", 2)}, {2, 1})
+end
diff --git a/xmake/core/base/utf8.lua b/xmake/core/base/utf8.lua
index 7351475f8..2878404fb 100644
--- a/xmake/core/base/utf8.lua
+++ b/xmake/core/base/utf8.lua
@@ -31,6 +31,7 @@ local utf8 = utf8 or {}
-- @interface utf8.codes(s [, lax])
-- @interface utf8.sub(s, i [, j])
-- @interface utf8.lastof(s, substr)
+-- @interface utf8.find(s, target [, init])
--
-- the char pattern