Re: [Tuxpaint-devel] conversion from wchar to utfstream
An award-winning drawing program for children of all ages
Brought to you by:
wkendrick
From: Pere P. i C. <per...@gm...> - 2022-03-09 00:13:37
|
El dg. 06 de 03 de 2022 a les 22:40 +0900, en/na Shin-ichi TOYAMA va escriure: > Hi! > > I have been thinking it is strange that there are some amount of specific code to convert wide charcter strings to utf-8 strings in render_text_w() from line 1665 to 1740. > > ----------------------------------------------------------------- > /* Convert from 16-bit UNICODE to UTF-8 encoded for SDL_Pango: */ > > utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1)); > utfstr = (char *)malloc(utfstr_max); > memset(utfstr, 0, utfstr_max); > > < ... many lines ... > > > utfstr[j] = '\0'; > ----------------------------------------------------------------- > > Here, we need to use '#ifdef WIN32' to cope with the difference in length of wchar_t between windows and other platforms. > > I looked here again, then I have become confident that we can replace all this block with wcstombs() which became safe also on windows by defining it as a native conversion function WideCharToMultiByte(). > > ----------------------------------------------------------------- > /* Convert from 16-bit UNICODE to UTF-8 encoded for SDL_Pango: */ > > utfstr_max = (sizeof(char) * 4 * (wcslen(str) + 1)); > utfstr = (char *)malloc(utfstr_max); > > wcstombs(utfstr, str, utfstr_max); > ----------------------------------------------------------------- > > How do you think of it? > Looks much more clear, I don't know why that code was needed but seems wcstombs() currently does the same thing Note that I've just tested your approach in Linux. My 2 cents Pere |