diff options
| author | ruki <[email protected]> | 2026-01-18 14:51:59 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-01-18 14:51:59 +0800 |
| commit | d5705110a292f193fb2d202de8115f5933fc9fcc (patch) | |
| tree | 5ce6616765b63197caafe5f377f9c6ea922c5ea2 | |
| parent | e8a618fab3758a3ca1b0b630d7c5b0dc4e08a7c5 (diff) | |
| parent | a1c0eaed71793cd3761dbcf6b2a4cab6c6474434 (diff) | |
Merge pull request #7235 from luadebug/case
add string case conversion functions: lower and upper
| -rw-r--r-- | core/src/xmake/engine.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/string/lower.c | 74 | ||||
| -rw-r--r-- | core/src/xmake/string/upper.c | 74 | ||||
| -rw-r--r-- | tests/modules/string/test.lua | 9 |
4 files changed, 161 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 532be1adc..d81828f1f 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -286,6 +286,8 @@ tb_int_t xm_string_lastof(lua_State *lua); tb_int_t xm_string_convert(lua_State *lua); tb_int_t xm_string_endswith(lua_State *lua); tb_int_t xm_string_startswith(lua_State *lua); +tb_int_t xm_string_lower(lua_State *lua); +tb_int_t xm_string_upper(lua_State *lua); // the process functions tb_int_t xm_process_open(lua_State *lua); @@ -589,6 +591,8 @@ static luaL_Reg const g_string_functions[] = { { "convert", xm_string_convert }, { "endswith", xm_string_endswith }, { "startswith", xm_string_startswith }, + { "lower", xm_string_lower }, + { "upper", xm_string_upper }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/string/lower.c b/core/src/xmake/string/lower.c new file mode 100644 index 000000000..ef43f2385 --- /dev/null +++ b/core/src/xmake/string/lower.c @@ -0,0 +1,74 @@ +/*!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 luadebug, ruki + * @file lower.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* string.lower + * + * @param str the string + * + * @code + * local result = string.lower(str) + * @endcode + */ +tb_int_t xm_string_lower(lua_State *lua) { + // check + tb_assert_and_check_return_val(lua, 0); + + // get string + size_t size = 0; + tb_char_t const* cstr = luaL_checklstring(lua, 1, &size); + tb_check_return_val(cstr, 0); + + // empty? + if (!size) { + lua_pushstring(lua, ""); + return 1; + } + + // copy string to buffer + tb_char_t* buffer = (tb_char_t*)tb_malloc_bytes(size + 1); + if (buffer) { + tb_memcpy(buffer, cstr, size); + buffer[size] = '\0'; + + // to lower + tb_long_t real_size = tb_charset_utf8_tolower(buffer, size); + + // push result + if (real_size >= 0) { + lua_pushlstring(lua, buffer, real_size); + } else { + lua_pushlstring(lua, cstr, size); + } + tb_free(buffer); + } else { + lua_pushnil(lua); + } + return 1; +} diff --git a/core/src/xmake/string/upper.c b/core/src/xmake/string/upper.c new file mode 100644 index 000000000..b9b71b349 --- /dev/null +++ b/core/src/xmake/string/upper.c @@ -0,0 +1,74 @@ +/*!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 luadebug, ruki + * @file upper.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* string.upper + * + * @param str the string + * + * @code + * local result = (str) + * @endcode + */ +tb_int_t xm_string_upper(lua_State *lua) { + // check + tb_assert_and_check_return_val(lua, 0); + + // get string + size_t size = 0; + tb_char_t const* cstr = luaL_checklstring(lua, 1, &size); + tb_check_return_val(cstr, 0); + + // empty? + if (!size) { + lua_pushstring(lua, ""); + return 1; + } + + // copy string to buffer + tb_char_t* buffer = (tb_char_t*)tb_malloc_bytes(size + 1); + if (buffer) { + tb_memcpy(buffer, cstr, size); + buffer[size] = '\0'; + + // to upper + tb_long_t real_size = tb_charset_utf8_toupper(buffer, size); + + // push result + if (real_size >= 0) { + lua_pushlstring(lua, buffer, real_size); + } else { + lua_pushlstring(lua, cstr, size); + } + tb_free(buffer); + } else { + lua_pushnil(lua); + } + return 1; +} diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 3f52dc593..94b3c1a04 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -106,3 +106,12 @@ function test_replace(t) t:are_equal(("123$xyz456xyz789xyz"):replace("123$", "000", {plain = true}), "000xyz456xyz789xyz") t:are_equal(("123xyz$456xyz$789xyz$"):replace("xyz$", "000", {plain = true}), "123000456000789000") end + +function test_case(t) + t:are_equal(("Hello"):lower(), "hello") + t:are_equal(("Hello"):upper(), "HELLO") + t:are_equal(("Звезда Хэнсин"):lower(), "звезда хэнсин") + t:are_equal(("Звезда Хэнсин"):upper(), "ЗВЕЗДА ХЭНСИН") + t:are_equal(("Test 源文件🎆 Message"):lower(), "test 源文件🎆 message") + t:are_equal(("Test 源文件🎆 Message"):upper(), "TEST 源文件🎆 MESSAGE") +end |
