diff options
| author | ruki <[email protected]> | 2026-01-20 23:11:30 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-20 23:11:30 +0800 |
| commit | e0f8104c1a0e0e62a4c5f02fa7457f308b27e5dd (patch) | |
| tree | 316cc92a602d7d4788fc15e5ed679b5b118ea08b /core/src/xmake/utf8/offset.c | |
| parent | 766a7a86a7af89c58038500985c9d8ec441de25f (diff) | |
add utf8 module
Diffstat (limited to 'core/src/xmake/utf8/offset.c')
| -rw-r--r-- | core/src/xmake/utf8/offset.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/core/src/xmake/utf8/offset.c b/core/src/xmake/utf8/offset.c new file mode 100644 index 000000000..89635eac1 --- /dev/null +++ b/core/src/xmake/utf8/offset.c @@ -0,0 +1,53 @@ +/*!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 offset.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "utf8.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* +** offset(s, n, [i]) -> index where n-th character counting from +** position 'i' starts; 0 means character at 'i'. +*/ +tb_int_t xm_utf8_offset(lua_State *lua) { + size_t len; + tb_char_t const* s = luaL_checklstring(lua, 1, &len); + lua_Integer n = luaL_checkinteger(lua, 2); + lua_Integer posi = (n >= 0) ? 1 : len + 1; + posi = xm_utf8_posrelat(luaL_optinteger(lua, 3, posi), len); + + tb_long_t result = xm_utf8_offset_impl(s, len, n, posi); + if (result == -1) + return luaL_argerror(lua, 3, "position out of bounds"); + if (result == -2) + return luaL_error(lua, "initial position is a continuation byte"); + if (result == 0) { + lua_pushnil(lua); + return 1; + } + lua_pushinteger(lua, result); + return 1; +} |
