I’m working on bug 11789: http://llvm.org/bugs/show_bug.cgi?id=11789
As part of it, I need to take a C string for a PredefinedExpr, and emit the equivalent wchar_t string literal. Right now my code just casts chars to wchar_t, but I realize this isn’t right, since it will break whenever clang is targeting a platform other than that which it was compiled on. My question is, is there an example of existing code for taking a string, encoded as char, and turning it into an array of bytes appropriately encoded as wchar_t for the target platform? If not, could anybody point me to a safe way to do this? Thanks!
[...] question is, is there an example of existing code for taking a string,
encoded as char, and turning it into an array of bytes appropriately encoded
as wchar_t for the target platform?
Take a look here:
If not, could anybody point me to a safe
way to do this? Thanks!
The general idea is to set the locale (i.e. call `setlocale()`) and
then perform a multibyte to widechar translation. Since `wchar_t` does
not specify an encoding (i.e. the encoding varies from one platform to
another) you will need to make sure that your translation correctly
converts the input string to the desired *native* encoding. Little
endian UTF-16 is the encoding of choice on Windows platforms.
C++11 has `codecvt_facet` which may interest you.
Thank you for your reply. I should have been more clear -- I am writing code in clang to do this. I can't use the local of the machine clang is running on, because I need to respect the target architecture, not just in its encoding of wchar_t, but its endianness.