Skip to content

Commit d3d36fa

Browse files
committed
Revert "feat: replace decode_utf8 with mbstowcs"
This reverts commit 9e225e9.
1 parent bc3a910 commit d3d36fa

File tree

10 files changed

+37
-40
lines changed

10 files changed

+37
-40
lines changed

lib/i18n/src/i18n.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ size_t i18n_detect_language(wchar_t *lang, size_t max_len)
148148
const char *str = getenv("LANG");
149149

150150
if (str) {
151-
len = mbstowcs(lang, str, max_len);
151+
len = decode_utf8(lang, str, max_len);
152152
// zh_CN -> zh-CN
153153
if (len > 3 && max_len > 3) {
154154
lang[2] = '-';

lib/i18n/src/i18n.h.in

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/*
2-
* lib/i18n/include/i18n.h
3-
*
4-
* Copyright (c) 2023-2024, Liu Chao <i@lc-soft.io> All rights reserved.
5-
*
6-
* SPDX-License-Identifier: MIT
7-
*
8-
* This file is part of LCUI, distributed under the MIT License found in the
9-
* LICENSE.TXT file in the root directory of this source tree.
10-
*/
111

122
#ifndef LIBI18N_INCLUDE_I18N_H
133
#define LIBI18N_INCLUDE_I18N_H

lib/i18n/src/yaml.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
static wchar_t *yaml_token_getwcs(yaml_token_t *token)
2020
{
2121
char *str = (char *)token->data.scalar.value;
22-
size_t len = token->data.scalar.length + 1;
23-
wchar_t *wcs = calloc(len, sizeof(wchar_t));
24-
mbstowcs(wcs, str, len + 1);
22+
size_t len = token->data.scalar.length;
23+
wchar_t *wcs = malloc(sizeof(wchar_t) * (len + 1));
24+
len = decode_utf8(wcs, str, len);
25+
if (len < 1) {
26+
abort();
27+
}
28+
wcs[len] = 0;
2529
return wcs;
2630
}
2731

@@ -69,7 +73,7 @@ dict_t *i18n_load_yaml_file(const char *path)
6973
return NULL;
7074
}
7175
if (!yaml_parser_initialize(&parser)) {
72-
free(buffer);
76+
free(buffer);
7377
logger_error("[i18n] failed to initialize parser!\n");
7478
return NULL;
7579
}

lib/i18n/tests/test.c

+21-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <i18n.h>
1313
#include <ctest.h>
14-
#include <locale.h>
1514

1615
void test_i18n(void)
1716
{
@@ -25,39 +24,45 @@ void test_i18n(void)
2524
i18n_load_language(L"zh-CN", "locales/zh-CN.yml"), true);
2625

2726
ctest_equal_bool("i18n_load_language(L\"en\", \"locales/en.yml\")",
28-
i18n_load_language(L"en", "locales/en.yml"), true);
27+
i18n_load_language(L"en", "locales/en.yml"),
28+
true);
2929

3030
ctest_equal_bool(
3131
"i18n_load_language(L\"notfound\", \"locales/notfound.yml\")",
3232
i18n_load_language(L"notfound", "locales/notfound.yml"), false);
3333

34-
i18n_change_language(L"en");
34+
i18n_change_language(L"en");
3535

36-
ctest_equal_wcs("en: i18n_translate(L\"name\") == L\"English\")",
37-
i18n_translate(L"name"), L"English");
36+
ctest_equal_bool("en: i18n_translate(L\"name\") == L\"English\")",
37+
wcscmp(i18n_translate(L"name"), L"English") == 0,
38+
true);
3839

39-
ctest_equal_wcs("en: i18n_translate(L\"button.ok\") == L\"Ok\")",
40-
i18n_translate(L"button.ok"), L"Ok");
40+
ctest_equal_bool("en: i18n_translate(L\"button.ok\") == L\"Ok\")",
41+
wcscmp(i18n_translate(L"button.ok"), L"Ok") == 0,
42+
true);
4143

4244
ctest_equal_bool("en: i18n_translate(L\"button.notfound\") == NULL)",
43-
i18n_translate(L"button.notfound") == NULL, true);
45+
i18n_translate(L"button.notfound")== NULL,
46+
true);
4447

45-
i18n_change_language(L"zh-CN");
48+
i18n_change_language(L"zh-CN");
4649

47-
ctest_equal_wcs("zh-CN: i18n_translate(L\"name\")",
48-
i18n_translate(L"name"), L"\u4E2D\u6587");
50+
ctest_equal_bool("zh-CN: i18n_translate(L\"name\")",
51+
wcscmp(i18n_translate(L"name"), L"\u4E2D\u6587") == 0,
52+
true);
4953

50-
ctest_equal_wcs("zh-CN: i18n_translate(L\"button.ok\")",
51-
i18n_translate(L"button.ok"), L"\u786E\u5B9A");
54+
ctest_equal_bool("zh-CN: i18n_translate(L\"button.ok\")",
55+
wcscmp(i18n_translate(L"button.ok"), L"\u786E\u5B9A") == 0,
56+
true);
5257

5358
ctest_equal_bool("zh-CN: i18n_translate(L\"button.notfound\") == NULL",
54-
i18n_translate(L"button.notfound") == NULL, true);
55-
i18n_clear();
59+
i18n_translate(L"button.notfound")== NULL,
60+
true);
61+
i18n_clear();
5662
}
5763

5864
int main(void)
5965
{
60-
setlocale(LC_CTYPE, "");
6166
logger_set_level(LOGGER_LEVEL_OFF);
6267
ctest_describe("i18n", test_i18n);
6368
return ctest_finish();

lib/platform/src/clipboard.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int clipboard_request_text(clipboard_callback_t callback, void *arg)
2929
wchar_t *wstr = malloc(sizeof(wchar_t) * len);
3030
clipboard_t clipboard_data = { 0 };
3131

32-
len = mbstowcs(wstr, clipboard.text, len);
32+
len = decode_utf8(wstr, clipboard.text, len);
3333
wstr[len] = 0;
3434
// Assign the data
3535
clipboard_data.text = wstr;

lib/platform/src/linux/linux_x11clipboard.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void x11_clipboard_execute_action(bool timed_out)
7777
} else {
7878
size_t len = x11_clipboard.text_len + 1;
7979
wstr = malloc(sizeof(wchar_t) * len);
80-
len = mbstowcs(wstr, x11_clipboard.text, len);
80+
len = decode_utf8(wstr, x11_clipboard.text, len);
8181
wstr[len] = 0;
8282
// Assign the data
8383
clipboard_data.text = wstr;

lib/ui-widgets/src/text.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ int ui_text_set_content(ui_widget_t *w, const char *utf8_text)
312312
size_t len = strlen(utf8_text) + 1;
313313

314314
wstr = malloc(sizeof(wchar_t) * len);
315-
mbstowcs(wstr, utf8_text, len);
315+
decode_utf8(wstr, utf8_text, len);
316316
ret = ui_text_set_content_w(w, wstr);
317317
if (wstr) {
318318
free(wstr);

lib/ui-widgets/src/textinput.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ int ui_textinput_set_text(ui_widget_t *widget, const char *utf8_str)
523523
if (!wstr) {
524524
return -ENOMEM;
525525
}
526-
len = mbstowcs(wstr, utf8_str, len);
526+
len = decode_utf8(wstr, utf8_str, len);
527527
wstr[len] = 0;
528528
ret = ui_textinput_set_text_w(widget, wstr);
529529
free(wstr);
@@ -587,7 +587,7 @@ int ui_textinput_set_placeholder(ui_widget_t *w, const char *str)
587587
if (!wstr) {
588588
return -ENOMEM;
589589
}
590-
len = mbstowcs(wstr, str, len);
590+
len = decode_utf8(wstr, str, len);
591591
wstr[len] = 0;
592592
ret = ui_textinput_set_placeholder_w(w, wstr);
593593
free(wstr);

lib/ui/src/ui_text_style.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ static void ui_compute_content(ui_text_style_t *fs, const char *str)
6161
if (!str) {
6262
return;
6363
}
64-
len = mbstowcs(NULL, str, 0);
64+
len = decode_utf8(NULL, str, 0);
6565
content = malloc((len + 1) * sizeof(wchar_t));
66-
len = mbstowcs(content, str, len);
66+
len = decode_utf8(content, str, len);
6767
content[len] = 0;
6868
if (content[0] == '"') {
6969
for (i = 0; content[i + 1]; ++i) {

src/lcui.c

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
#include <time.h>
13-
#include <locale.h>
1413
#include <stdio.h>
1514
#include <stdlib.h>
1615
#include <errno.h>
@@ -150,7 +149,6 @@ void lcui_destroy_app(void)
150149
void lcui_init(void)
151150
{
152151
lcui_init_app();
153-
setlocale(LC_CTYPE, "");
154152
if (app_init(L"LCUI Application") != 0) {
155153
abort();
156154
}

0 commit comments

Comments
 (0)