diff options
| author | Saikari <[email protected]> | 2026-01-17 04:00:16 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-01-17 04:00:16 +0300 |
| commit | b5f93e02d0e28b694fe452ef868620fffbd7872b (patch) | |
| tree | dbbfd75399ac3f97ffe05835e29bed2cb2527d23 | |
| parent | d83c306af108ef93fc08b9db1c42469c7f14372f (diff) | |
try use setlocale
| -rw-r--r-- | core/src/xmake/string/case.c | 22 | ||||
| -rw-r--r-- | tests/modules/string/test.lua | 2 |
2 files changed, 18 insertions, 6 deletions
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 <windows.h> #endif +#if defined(TB_CONFIG_LIBC_HAVE_SETLOCALE) +# include <locale.h> +#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 |
