diff options
| author | ruki <[email protected]> | 2026-01-20 23:38:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-20 23:38:02 +0800 |
| commit | a9d5fe27bb69cc35022e9c8b80ac9d5f89f570c0 (patch) | |
| tree | 7d48629cd895877d91fdab0e9648d03419ca51f1 | |
| parent | 92423451b623e79ac464d388bfb1bfed2780b792 (diff) | |
add utf8.sub and lastof
| -rw-r--r-- | core/src/xmake/engine.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/utf8/lastof.c | 65 | ||||
| -rw-r--r-- | core/src/xmake/utf8/sub.c | 88 | ||||
| -rw-r--r-- | tests/modules/utf8/test.lua | 44 | ||||
| -rw-r--r-- | xmake/core/base/utf8.lua | 2 |
5 files changed, 203 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; +} diff --git a/tests/modules/utf8/test.lua b/tests/modules/utf8/test.lua index f342c8402..51da441d9 100644 --- a/tests/modules/utf8/test.lua +++ b/tests/modules/utf8/test.lua @@ -62,3 +62,47 @@ end function test_charpattern(t) t:require(utf8.charpattern) end + +function test_sub(t) + t:are_equal(utf8.sub("ABC", 1, 1), "A") + t:are_equal(utf8.sub("ABC", 2, 2), "B") + t:are_equal(utf8.sub("ABC", 1, 2), "AB") + t:are_equal(utf8.sub("你好", 1, 1), "你") + t:are_equal(utf8.sub("你好", 2, 2), "好") + t:are_equal(utf8.sub("你好", 1, 2), "你好") + + -- mixed + t:are_equal(utf8.sub("A你好B", 2, 3), "你好") + t:are_equal(utf8.sub("A你好B", 1, 3), "A你好") + t:are_equal(utf8.sub("A你好B", 2, 4), "你好B") + + -- negative + t:are_equal(utf8.sub("ABC", -1), "C") + t:are_equal(utf8.sub("ABC", -2), "BC") + t:are_equal(utf8.sub("你好", -1), "好") + t:are_equal(utf8.sub("你好", -2), "你好") + t:are_equal(utf8.sub("你好", 1, -1), "你好") + t:are_equal(utf8.sub("你好", 1, -2), "你") + + -- out of bounds + t:are_equal(utf8.sub("ABC", 4), "") + t:are_equal(utf8.sub("ABC", 1, 5), "ABC") + t:are_equal(utf8.sub("ABC", 0), "ABC") + t:are_equal(utf8.sub("ABC", -10), "ABC") +end + +function test_lastof(t) + t:are_equal(utf8.lastof("ABC", "A"), 1) + t:are_equal(utf8.lastof("ABC", "B"), 2) + t:are_equal(utf8.lastof("ABC", "C"), 3) + t:are_equal(utf8.lastof("ABCA", "A"), 4) + + t:are_equal(utf8.lastof("你好", "你"), 1) + t:are_equal(utf8.lastof("你好", "好"), 2) + t:are_equal(utf8.lastof("你好你", "你"), 3) + + t:are_equal(utf8.lastof("A你好A", "A"), 4) + t:are_equal(utf8.lastof("A你好A", "好"), 3) + + t:are_equal(utf8.lastof("ABC", "D"), nil) +end diff --git a/xmake/core/base/utf8.lua b/xmake/core/base/utf8.lua index 1a1edc0ea..7351475f8 100644 --- a/xmake/core/base/utf8.lua +++ b/xmake/core/base/utf8.lua @@ -29,6 +29,8 @@ local utf8 = utf8 or {} -- @interface utf8.codepoint(s [, i [, j]]) -- @interface utf8.offset(s, n [, i]) -- @interface utf8.codes(s [, lax]) +-- @interface utf8.sub(s, i [, j]) +-- @interface utf8.lastof(s, substr) -- -- the char pattern |
