summaryrefslogtreecommitdiff
path: root/core/src
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 /core/src
parenta9d5fe27bb69cc35022e9c8b80ac9d5f89f570c0 (diff)
fix utf8.find
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/utf8/find.c98
2 files changed, 100 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;
+}