Skip to content

Commit

Permalink
feat: replace decode_utf8 with mbstowcs
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Mar 3, 2024
1 parent eead3a5 commit a1e9de8
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/src/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ size_t i18n_detect_language(wchar_t *lang, size_t max_len)
const char *str = getenv("LANG");

if (str) {
len = decode_utf8(lang, str, max_len);
len = mbstowcs(lang, str, max_len);
// zh_CN -> zh-CN
if (len > 3 && max_len > 3) {
lang[2] = '-';
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/src/yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static wchar_t *yaml_token_getwcs(yaml_token_t *token)
char *str = (char *)token->data.scalar.value;
size_t len = token->data.scalar.length;
wchar_t *wcs = malloc(sizeof(wchar_t) * (len + 1));
len = decode_utf8(wcs, str, len);
len = mbstowcs(wcs, str, len);
if (len < 1) {
abort();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/platform/src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int clipboard_request_text(clipboard_callback_t callback, void *arg)
wchar_t *wstr = malloc(sizeof(wchar_t) * len);
clipboard_t clipboard_data = { 0 };

len = decode_utf8(wstr, clipboard.text, len);
len = mbstowcs(wstr, clipboard.text, len);
wstr[len] = 0;
// Assign the data
clipboard_data.text = wstr;
Expand Down
2 changes: 1 addition & 1 deletion lib/platform/src/linux/linux_x11clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void x11_clipboard_execute_action(bool timed_out)
} else {
size_t len = x11_clipboard.text_len + 1;
wstr = malloc(sizeof(wchar_t) * len);
len = decode_utf8(wstr, x11_clipboard.text, len);
len = mbstowcs(wstr, x11_clipboard.text, len);
wstr[len] = 0;
// Assign the data
clipboard_data.text = wstr;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui-widgets/src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ int ui_text_set_content(ui_widget_t *w, const char *utf8_text)
size_t len = strlen(utf8_text) + 1;

wstr = malloc(sizeof(wchar_t) * len);
decode_utf8(wstr, utf8_text, len);
mbstowcs(wstr, utf8_text, len);
ret = ui_text_set_content_w(w, wstr);
if (wstr) {
free(wstr);
Expand Down
4 changes: 2 additions & 2 deletions lib/ui-widgets/src/textinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ int ui_textinput_set_text(ui_widget_t *widget, const char *utf8_str)
if (!wstr) {
return -ENOMEM;
}
len = decode_utf8(wstr, utf8_str, len);
len = mbstowcs(wstr, utf8_str, len);
wstr[len] = 0;
ret = ui_textinput_set_text_w(widget, wstr);
free(wstr);
Expand Down Expand Up @@ -587,7 +587,7 @@ int ui_textinput_set_placeholder(ui_widget_t *w, const char *str)
if (!wstr) {
return -ENOMEM;
}
len = decode_utf8(wstr, str, len);
len = mbstowcs(wstr, str, len);
wstr[len] = 0;
ret = ui_textinput_set_placeholder_w(w, wstr);
free(wstr);
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/src/ui_text_style.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ static void ui_compute_content(ui_text_style_t *fs, const char *str)
if (!str) {
return;
}
len = decode_utf8(NULL, str, 0);
len = mbstowcs(NULL, str, 0);
content = malloc((len + 1) * sizeof(wchar_t));
len = decode_utf8(content, str, len);
len = mbstowcs(content, str, len);
content[len] = 0;
if (content[0] == '"') {
for (i = 0; content[i + 1]; ++i) {
Expand Down
2 changes: 2 additions & 0 deletions src/lcui.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <time.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
Expand Down Expand Up @@ -149,6 +150,7 @@ void lcui_destroy_app(void)
void lcui_init(void)
{
lcui_init_app();
setlocale(LC_CTYPE, "");
if (app_init(L"LCUI Application") != 0) {
abort();
}
Expand Down

0 comments on commit a1e9de8

Please sign in to comment.