summaryrefslogtreecommitdiff
path: root/core/src/xmake/string
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-21 00:32:48 +0800
committerruki <[email protected]>2026-01-21 00:32:48 +0800
commit443fc30ae713b14623ac20bd5f1e8fdb12740783 (patch)
tree3ec726af7f2c4452726bd825fa240341ad01fb0a /core/src/xmake/string
parent91843ffb82564d29ce9a9e386f2c9292620a5ba9 (diff)
improve lastof
Diffstat (limited to 'core/src/xmake/string')
-rw-r--r--core/src/xmake/string/lastof.c42
1 files changed, 5 insertions, 37 deletions
diff --git a/core/src/xmake/string/lastof.c b/core/src/xmake/string/lastof.c
index 9ac6c6784..912647a9c 100644
--- a/core/src/xmake/string/lastof.c
+++ b/core/src/xmake/string/lastof.c
@@ -29,39 +29,7 @@
* includes
*/
#include "prefix.h"
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * private implementation
- */
-static tb_void_t xm_string_lastof_str(
- lua_State *lua, tb_char_t const *cstr, tb_size_t nstr, tb_char_t const *csubstr, tb_size_t nsubstr) {
- // find it
- tb_char_t const *curr = tb_null;
- tb_char_t const *next = cstr;
- do {
- next = tb_strstr(next, csubstr); // faster than tb_strnstr()
- if (next) {
- curr = next;
- next += nsubstr;
- }
-
- } while (!next);
-
- // found?
- if (curr) {
- lua_pushinteger(lua, curr - cstr + 1);
- } else {
- lua_pushnil(lua);
- }
-}
-static tb_void_t xm_string_lastof_chr(lua_State *lua, tb_char_t const *cstr, tb_size_t nstr, tb_char_t ch) {
- tb_char_t const *pos = tb_strrchr(cstr, ch); // faster than tb_strnrchr()
- if (pos) {
- lua_pushinteger(lua, pos - cstr + 1);
- } else {
- lua_pushnil(lua);
- }
-}
+#include "../utf8/utf8.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
@@ -84,11 +52,11 @@ tb_int_t xm_string_lastof(lua_State *lua) {
tb_char_t const *csubstr = luaL_checklstring(lua, 2, &nsubstr);
// lastof it
- lua_newtable(lua);
- if (nsubstr == 1) {
- xm_string_lastof_chr(lua, cstr, (tb_size_t)nstr, csubstr[0]);
+ tb_long_t char_pos = xm_utf8_lastof_impl(cstr, nstr, csubstr, nsubstr);
+ if (char_pos > 0) {
+ lua_pushinteger(lua, char_pos);
} else {
- xm_string_lastof_str(lua, cstr, (tb_size_t)nstr, csubstr, nsubstr);
+ lua_pushnil(lua);
}
return 1;
}