From 3cf298b854443e7bbb2a96d976e8d79ffc4ddf02 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sat, 17 Jan 2026 02:33:18 +0300 Subject: add string case conversion functions: lower and upper --- tests/modules/string/test.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests/modules/string/test.lua') diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 3f52dc593..6bb3b6f78 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -106,3 +106,10 @@ 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(), "ЗВЕЗДА ХЭНСИН") +end -- cgit v1.3.1 From b5f93e02d0e28b694fe452ef868620fffbd7872b Mon Sep 17 00:00:00 2001 From: Saikari Date: Sat, 17 Jan 2026 04:00:16 +0300 Subject: try use setlocale --- core/src/xmake/string/case.c | 22 ++++++++++++++++------ tests/modules/string/test.lua | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'tests/modules/string/test.lua') diff --git a/core/src/xmake/string/case.c b/core/src/xmake/string/case.c index 858d4be08..ee20b4a9b 100644 --- a/core/src/xmake/string/case.c +++ b/core/src/xmake/string/case.c @@ -34,6 +34,9 @@ #if defined(TB_CONFIG_OS_WINDOWS) # include #endif +#if defined(TB_CONFIG_LIBC_HAVE_SETLOCALE) +# include +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * helper @@ -43,6 +46,11 @@ static tb_int_t xm_string_case(lua_State* lua, tb_bool_t lower) { // check tb_assert_and_check_return_val(lua, 0); +#if defined(TB_CONFIG_LIBC_HAVE_SETLOCALE) + // set locale to utf8 + setlocale(LC_ALL, ""); +#endif + // get the string size_t size = 0; tb_char_t const* str = luaL_checklstring(lua, 1, &size); @@ -79,12 +87,14 @@ static tb_int_t xm_string_case(lua_State* lua, tb_bool_t lower) { tb_uint32_t* p = (tb_uint32_t*)dst_data; tb_size_t n = (tb_size_t)dst_size / 4; while (n--) { - if (lower) { - if (iswupper((wint_t)*p)) - *p = towlower((wint_t)*p); - } else { - if (iswlower((wint_t)*p)) - *p = towupper((wint_t)*p); + if (*p < 0x10000) { + if (lower) { + if (iswupper((wint_t)*p)) + *p = towlower((wint_t)*p); + } else { + if (iswlower((wint_t)*p)) + *p = towupper((wint_t)*p); + } } p++; } diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 6bb3b6f78..94b3c1a04 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -112,4 +112,6 @@ function test_case(t) 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 -- cgit v1.3.1