mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Rework iconv encoding detection
WCHAR_T doesn't seem as portable as we thought, so it's just easier to detect the right encoding using macros at this point.
This commit is contained in:
parent
d1a1aed23e
commit
008d6be900
2 changed files with 35 additions and 32 deletions
|
@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
*/
|
||||
|
||||
#include "string.h"
|
||||
#include "pointer.h"
|
||||
#include "serialize.h" // BYTE_ORDER
|
||||
#include "numeric.h"
|
||||
#include "log.h"
|
||||
|
||||
|
@ -67,20 +67,16 @@ static bool convert(const char *to, const char *from, char *outbuf,
|
|||
return true;
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
// On Android iconv disagrees how big a wchar_t is for whatever reason
|
||||
const char *DEFAULT_ENCODING = "UTF-32LE";
|
||||
#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
|
||||
// NetBSD does not allow "WCHAR_T" as a charset input to iconv.
|
||||
#include <sys/endian.h>
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
const char *DEFAULT_ENCODING = "UTF-32BE";
|
||||
#else
|
||||
const char *DEFAULT_ENCODING = "UTF-32LE";
|
||||
#endif
|
||||
#else
|
||||
const char *DEFAULT_ENCODING = "WCHAR_T";
|
||||
#endif
|
||||
// select right encoding for wchar_t size
|
||||
constexpr auto DEFAULT_ENCODING = ([] () -> const char* {
|
||||
constexpr auto sz = sizeof(wchar_t);
|
||||
static_assert(sz == 2 || sz == 4, "Unexpected wide char size");
|
||||
if constexpr (sz == 2) {
|
||||
return (BYTE_ORDER == BIG_ENDIAN) ? "UTF-16BE" : "UTF-16LE";
|
||||
} else {
|
||||
return (BYTE_ORDER == BIG_ENDIAN) ? "UTF-32BE" : "UTF-32LE";
|
||||
}
|
||||
})();
|
||||
|
||||
std::wstring utf8_to_wide(std::string_view input)
|
||||
{
|
||||
|
@ -93,10 +89,6 @@ std::wstring utf8_to_wide(std::string_view input)
|
|||
std::wstring out;
|
||||
out.resize(outbuf_size / sizeof(wchar_t));
|
||||
|
||||
#if defined(__ANDROID__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
|
||||
static_assert(sizeof(wchar_t) == 4, "Unexpected wide char size");
|
||||
#endif
|
||||
|
||||
char *outbuf = reinterpret_cast<char*>(&out[0]);
|
||||
if (!convert(DEFAULT_ENCODING, "UTF-8", outbuf, &outbuf_size, inbuf, inbuf_size)) {
|
||||
infostream << "Couldn't convert UTF-8 string 0x" << hex_encode(input)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue