by Aardwolf · 3y ago · 0 comments
But that's not compatible with other OSes, so you can't write native multiplatform C++ code just because windows keeps insisting on their worst-of-both-worlds UTF-16
(worst of both worlds because it's not single value per unicode character like UTF-32, and not ASCII compatible like UTF-8)
Characters can consist of multiple code points.
I agree that utf-8 is preferable to utf-16 though in most cases where the root script is English/Latin.
I enjoy being "that guy", and so, to be unfathomably pedantic, I will point out that the Unicode standard actually uses the word "character" to refer to _encoded characters_, i.e. the mapping from a code point to an abstract character [0]. So 1 code point = 1 character for Unicode. Of course, in real life, character is a silly made-up word that means whatever you think it means and using it to refer to an extended grapheme cluster or glyph is probably closer to how people really think of them anyway.
[0]: See section 3.4 of https://www.unicode.org/versions/Unicode15.0.0/UnicodeStanda...
Unicode does not define "character" because that term is far too imprecise for a deterministic standard. And the Unicode standard is extremely clear that an abstract character may be represented by multiple code points.
"A single abstract character may also be represented by a sequence of code points—for example, latin capital letter g with acute may be represented by the sequence <U+0047 latin capital letter g, U+0301 combining acute accent>, rather than being mapped to a single code point."
> Of course, in real life, character is a silly made-up word that means whatever you think it means and using it to refer to an extended grapheme cluster or glyph is probably closer to how people really think of them anyway.
Human language and writing are the defining inventions of the known universe. Unicode just happens to be one way of representing them. Referring to them as "silly and made up" in comparison to Unicode is nothing short of extreme mental illness.
> Unless specified otherwise for clarity, in the text of the Unicode Standard the term character alone designates an encoded character.
Reading is free. I even gave you the section number.
> A single abstract character may also be represented by a sequence of code points
An "abstract character" is not the same thing as a character. The whole point of my comment was that the general word "character" is a general class of definitions but means nothing by itself. Much like the word "number" - you will notice that mathematicians define many kinds of numbers, but never the word itself.
> Human language and writing are the defining inventions of the known universe. Unicode just happens to be one way of representing them. Referring to them as "silly and made up" in comparison to Unicode is nothing short of extreme mental illness.
All words are made up. I'm sorry you had to find out this way.
The first thing to realize to make it work is that paths are not strings, so you actually don't want to treat them like they are.
Platform differences unfortunately go way deeper than just using UTF-16. Lots of gnarly details. No platform is worse than another - they just have different kinds of good and bad in different places.
C++ also supports wstring, but its implementation is dependent on the platform for god knows what reason.
utf-16 is also supported in C11:
https://en.wikipedia.org/wiki/C11_(C_standard_revision)#Chan...
> Improved Unicode support based on the C Unicode Technical Report ISO/IEC TR 19769:2004 (char16_t and char32_t types for storing UTF-16/UTF-32 encoded data, including conversion functions in <uchar.h> and the corresponding u and U string literal prefixes, as well as the u8 prefix for UTF-8 encoded literals).
You can use uchar.h for converting instead of the Win32 functions.
Here's a helper I wrote to wrap these string types in std::string and std::wstring:
bool mbStrToWideChar(string const& in, wstring& out)
{
bool status = true;
i32 bufferSize = MultiByteToWideChar(
CP_ACP, 0, in.c_str(), -1, nullptr, 0
);
WCHAR* buffer = new WCHAR[bufferSize];
i32 conversionResult = MultiByteToWideChar(
CP_ACP, 0,
in.c_str(), -1,
buffer, bufferSize
);
if (!conversionResult)
{
cerr << "Failed to convert ANSI string [" << in << "] to Wide string!" << endl;
status = false;
goto Delete;
}
out = wstring(buffer);
Delete:
if (buffer)
{
delete[] buffer;
}
return status;
}
and bool wideStrToMbStr(wstring const& in, string& out)
{
// Get buffer size
i32 bufferSize = WideCharToMultiByte(
CP_UTF8, 0,
in.c_str(), (i32)in.size(),
nullptr, 0,
nullptr, nullptr
);
// Set outstring to new buffer size
out = string(bufferSize, '\0');
//Convert
i32 res = WideCharToMultiByte(
CP_UTF8, 0,
in.c_str(), (i32)in.size(),
&out[0], (i32)out.size(),
nullptr, nullptr
);
if (!res)
{
wcerr << L"Failed to convert Wide string [" << in << L"] to ANSI string!" << endl;
return false;
}
return true;
}I forgot what Japanese emulator I tried to run when I found all of this out, ut sufficed to say I didn't enjoy the experience.
However, the common denominator nowadays is UTF-8, which has been a blessing overall getting rid of most of the aforementioned mess for international multi-user systems. And there is the C.UTF-8 locale which is slowly gaining traction.