diff options
| author | ruki <[email protected]> | 2026-01-21 00:46:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-21 00:46:38 +0800 |
| commit | f10baca5c10b1d21c6d9900ad3d6b382c37258c6 (patch) | |
| tree | 01799807de63410f747c1132dc1fefd7df699959 /core/src | |
| parent | b5a506803e4f3c8b11858be2a57c509d479683e1 (diff) | |
add utf8.byte
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/utf8/byte.c | 52 | ||||
| -rw-r--r-- | core/src/xmake/utf8/utf8.c | 25 | ||||
| -rw-r--r-- | core/src/xmake/utf8/utf8.h | 1 |
4 files changed, 80 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 146cdd17a..bca1cce65 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -282,6 +282,7 @@ tb_int_t xm_winos_short_path(lua_State *lua); // the utf8 functions tb_int_t xm_utf8_len(lua_State *lua); tb_int_t xm_utf8_char(lua_State *lua); +tb_int_t xm_utf8_byte(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); @@ -597,6 +598,7 @@ static luaL_Reg const g_bloom_filter_functions[] = { // the utf8 functions static luaL_Reg const g_utf8_functions[] = { {"char", xm_utf8_char}, + {"byte", xm_utf8_byte}, {"codes", xm_utf8_codes}, {"codepoint", xm_utf8_codepoint}, {"len", xm_utf8_len}, diff --git a/core/src/xmake/utf8/byte.c b/core/src/xmake/utf8/byte.c new file mode 100644 index 000000000..036f04334 --- /dev/null +++ b/core/src/xmake/utf8/byte.c @@ -0,0 +1,52 @@ +/*!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 byte.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "utf8.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_bool_t xm_utf8_byte_cb(xm_utf8_int_t code, tb_cpointer_t udata) { + lua_State* lua = (lua_State*)udata; + tb_assert_and_check_return_val(lua, tb_false); + + luaL_checkstack(lua, 1, "too many results"); + lua_pushinteger(lua, code); + return tb_true; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* utf8.byte(s, i [, j]) + */ +tb_int_t xm_utf8_byte(lua_State* lua) { + size_t len; + tb_char_t const* s = luaL_checklstring(lua, 1, &len); + lua_Integer i = luaL_optinteger(lua, 2, 1); + lua_Integer j = luaL_optinteger(lua, 3, i); + + return (tb_int_t)xm_utf8_byte_impl(s, len, i, j, xm_utf8_byte_cb, lua); +} diff --git a/core/src/xmake/utf8/utf8.c b/core/src/xmake/utf8/utf8.c index 168ffe60a..ff6c33a1a 100644 --- a/core/src/xmake/utf8/utf8.c +++ b/core/src/xmake/utf8/utf8.c @@ -399,6 +399,31 @@ tb_long_t xm_utf8_lastof_impl(tb_char_t const* s, tb_size_t len, tb_char_t const return 0; } +tb_long_t xm_utf8_byte_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, xm_utf8_codepoint_func_t func, tb_cpointer_t udata) { + tb_size_t sublen = 0; + tb_char_t const* sub = xm_utf8_sub_impl(s, len, i, j, &sublen); + if (sub && sublen > 0) { + + // decode and push codepoints + tb_long_t n = 0; + tb_char_t const* p = sub; + tb_char_t const* e = sub + sublen; + while (p < e) { + xm_utf8_int_t val; + tb_char_t const* next = xm_utf8_decode(p, &val, tb_true); + if (next) { + if (func && !func(val, udata)) break; + n++; + p = next; + } else { + p++; + } + } + return n; + } + return 0; +} + tb_char_t const* xm_utf8_sub_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, tb_size_t* psublen) { tb_assert_and_check_return_val(s && psublen, tb_null); *psublen = 0; diff --git a/core/src/xmake/utf8/utf8.h b/core/src/xmake/utf8/utf8.h index d6d884240..fff81f1e5 100644 --- a/core/src/xmake/utf8/utf8.h +++ b/core/src/xmake/utf8/utf8.h @@ -53,6 +53,7 @@ tb_long_t xm_utf8_offset_impl(tb_char_t const* s, tb_size_t len, tb_lo tb_bool_t xm_utf8_codepoint_impl(tb_char_t const* s, tb_size_t len, tb_long_t posi, tb_long_t posj, tb_bool_t strict, xm_utf8_codepoint_func_t func, tb_cpointer_t udata); tb_long_t xm_utf8_find_impl(tb_char_t const* s, tb_size_t len, tb_char_t const* sub, tb_size_t sublen, tb_long_t init, tb_long_t* pchar_end); tb_long_t xm_utf8_lastof_impl(tb_char_t const* s, tb_size_t len, tb_char_t const* sub, tb_size_t sublen); +tb_long_t xm_utf8_byte_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, xm_utf8_codepoint_func_t func, tb_cpointer_t udata); tb_char_t const* xm_utf8_sub_impl(tb_char_t const* s, tb_size_t len, tb_long_t i, tb_long_t j, tb_size_t* psublen); #endif |
