summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-21 22:09:09 +0800
committerruki <[email protected]>2026-01-21 22:09:09 +0800
commit197dfb660a35825d0bb46f8ef69884d7ec00c410 (patch)
tree271f4bce14ac31a35f89c7df2e51c27b27025043
parent3a602442ced657fd07d9890176f1bc24ea1b346f (diff)
fix path.filename and lastof
-rw-r--r--core/src/xmake/utf8/utf8.c4
-rw-r--r--tests/modules/path/test.lua17
-rw-r--r--tests/modules/string/test.lua7
-rw-r--r--tests/modules/utf8/test.lua10
4 files changed, 35 insertions, 3 deletions
diff --git a/core/src/xmake/utf8/utf8.c b/core/src/xmake/utf8/utf8.c
index 1e76c2a7e..f4da25935 100644
--- a/core/src/xmake/utf8/utf8.c
+++ b/core/src/xmake/utf8/utf8.c
@@ -395,9 +395,7 @@ tb_long_t xm_utf8_lastof_impl(tb_char_t const* s, tb_size_t len, tb_char_t const
}
if (last) {
- tb_long_t count = xm_utf8_len_impl(s, len, 1, last - s, tb_true, tb_null);
- if (count < 0) return 0;
- return count + 1;
+ return (tb_long_t)(last - s + 1);
}
return 0;
}
diff --git a/tests/modules/path/test.lua b/tests/modules/path/test.lua
index f68a57e5f..57f7d06bc 100644
--- a/tests/modules/path/test.lua
+++ b/tests/modules/path/test.lua
@@ -34,6 +34,23 @@ function test_extension(t)
t:are_equal(path.extension("\\home\\foo.so"), ".so")
end
+function test_filename(t)
+ t:are_equal(path.filename("foo"), "foo")
+ t:are_equal(path.filename("foo.so"), "foo.so")
+ t:are_equal(path.filename("/tmp/foo.so"), "foo.so")
+ t:are_equal(path.filename("c:\\tmp\\foo.so"), "foo.so")
+ t:are_equal(path.filename("/tmp/.."), "..")
+ t:are_equal(path.filename("/tmp/."), ".")
+ t:are_equal(path.filename("/"), "")
+ t:are_equal(path.filename(""), "")
+
+ -- unicode
+ t:are_equal(path.filename("Unicode 测试/test.lua"), "test.lua")
+ t:are_equal(path.filename("Unicode 测试/foo/test.lua"), "test.lua")
+ t:are_equal(path.filename("测试/test.lua"), "test.lua")
+ t:are_equal(path.filename("测试\\test.lua"), "test.lua")
+end
+
function test_directory(t)
t:are_equal(path.directory(""), nil)
t:are_equal(path.directory("."), nil)
diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua
index 94b3c1a04..4b093de45 100644
--- a/tests/modules/string/test.lua
+++ b/tests/modules/string/test.lua
@@ -94,6 +94,13 @@ function test_lastof(t)
t:are_equal(("/home/file.txt"):lastof('/', true), 6)
t:are_equal(("/home/file.txt"):lastof('/home', true), 1)
t:are_equal(("/home/file.txt"):lastof('[/\\]home'), 1)
+
+ -- long string
+ local longstr = ("a"):rep(1000) .. "b"
+ t:are_equal(longstr:lastof("b"), 1001)
+ t:are_equal(longstr:lastof("a"), 1000)
+ t:are_equal(longstr:lastof("b", true), 1001)
+ t:are_equal(longstr:lastof("a", true), 1000)
end
function test_replace(t)
diff --git a/tests/modules/utf8/test.lua b/tests/modules/utf8/test.lua
index f7c8dbd2e..ddbd6b639 100644
--- a/tests/modules/utf8/test.lua
+++ b/tests/modules/utf8/test.lua
@@ -110,6 +110,16 @@ function test_lastof(t)
t:are_equal(utf8.lastof("ABC", "A", true), 1)
t:are_equal(utf8.lastof("ABC", "B", true), 2)
t:are_equal(utf8.lastof("ABC", ".", true), nil)
+ t:are_equal(utf8.lastof("你好", "好", true), 2)
+ t:are_equal(utf8.lastof("C你好D", "好", true), 3)
+ t:are_equal(utf8.lastof("C你好D", "D", true), 4)
+
+ -- long string
+ local longstr = ("你"):rep(1000) .. "好"
+ t:are_equal(utf8.lastof(longstr, "好"), 1001)
+ t:are_equal(utf8.lastof(longstr, "你"), 1000)
+ t:are_equal(utf8.lastof(longstr, "好", true), 1001)
+ t:are_equal(utf8.lastof(longstr, "你", true), 1000)
-- pattern
t:are_equal(utf8.lastof("ABC", "."), 3)