Use platform implementation in ucstring for UTF-8 conversion on Windows to get valid UTF-16

develop
kaetemi 4 years ago
parent dd28130617
commit 771248fcf4

@ -300,6 +300,13 @@ public:
virtual void serial(bool &b) ; virtual void serial(bool &b) ;
#ifndef NL_OS_CYGWIN #ifndef NL_OS_CYGWIN
virtual void serial(char &b) ; virtual void serial(char &b) ;
#endif
#ifdef NL_OS_WINDOWS
inline void serial(wchar_t &b)
{
nlctassert(sizeof(wchar_t) == sizeof(uint16));
serial(reinterpret_cast<uint16 &>(b));
}
#endif #endif
virtual void serial(std::string &b) ; virtual void serial(std::string &b) ;
virtual void serial(ucstring &b) ; virtual void serial(ucstring &b) ;

@ -546,7 +546,11 @@ template<> struct hash<uint64>
* \typedef ucchar * \typedef ucchar
* An Unicode character (16 bits) * An Unicode character (16 bits)
*/ */
#if defined(NL_OS_WINDOWS)
typedef wchar_t ucchar;
#else
typedef uint16 ucchar; typedef uint16 ucchar;
#endif
#ifndef NL_OVERRIDE #ifndef NL_OVERRIDE
#define NL_OVERRIDE override #define NL_OVERRIDE override

@ -30,7 +30,11 @@
* An unicode string class (16 bits per character). * An unicode string class (16 bits per character).
* Add features to convert and assign \c ucstring to \c string and \c string to \c ucstring. * Add features to convert and assign \c ucstring to \c string and \c string to \c ucstring.
*/ */
#if defined(NL_OS_WINDOWS)
typedef std::wstring ucstringbase;
#else
typedef std::basic_string<ucchar> ucstringbase; typedef std::basic_string<ucchar> ucstringbase;
#endif
class ucstring : public ucstringbase class ucstring : public ucstringbase
{ {

@ -172,7 +172,7 @@ bool unpack7Zip(const std::string &sevenZipFile, const std::string &destFileName
filename.resize(nameLen); filename.resize(nameLen);
// write filename into ucstring // write filename into ucstring
SzArEx_GetFileNameUtf16(&db, 0, &filename[0]); SzArEx_GetFileNameUtf16(&db, 0, reinterpret_cast<UInt16 *>(&filename[0]));
// write the extracted file // write the extracted file
FILE *outputHandle = nlfopen(destFileName, "wb+"); FILE *outputHandle = nlfopen(destFileName, "wb+");

@ -244,7 +244,7 @@ std::wstring utf8ToWide(const char *str, size_t len)
#if defined(NL_OS_WINDOWS) #if defined(NL_OS_WINDOWS)
return winCpToWide(str, len, CP_UTF8); return winCpToWide(str, len, CP_UTF8);
#else #else
// TODO: UTF-32 to UTF-8 // TODO: UTF-8 to UTF-32
nlassert(false); nlassert(false);
#endif #endif
} }

@ -31,6 +31,12 @@ void ucstring::toString(std::string &str) const
std::string ucstring::toUtf8() const std::string ucstring::toUtf8() const
{ {
#if defined(NL_OS_WINDOWS)
// Use OS implementation
nlctassert(sizeof(wchar_t) == sizeof(ucchar));
nlctassert(sizeof(wchar_t) == sizeof(uint16));
return NLMISC::wideToUtf8(static_cast<const std::wstring &>(*this));
#else
std::string res; std::string res;
ucstring::const_iterator first(begin()), last(end()); ucstring::const_iterator first(begin()), last(end());
for (; first != last; ++first) for (; first != last; ++first)
@ -65,10 +71,20 @@ std::string ucstring::toUtf8() const
} }
} }
return res; return res;
#endif
} }
void ucstring::fromUtf8(const std::string &stringUtf8) void ucstring::fromUtf8(const std::string &stringUtf8)
{ {
#if defined(NL_OS_WINDOWS)
// Use OS implementation
nlctassert(sizeof(wchar_t) == sizeof(ucchar));
nlctassert(sizeof(wchar_t) == sizeof(uint16));
nlctassert(sizeof(std::wstring) == sizeof(ucstring)); // These can be swapped on Windows
static_cast<std::wstring &>(*this) = nlmove(NLMISC::utf8ToWide(stringUtf8));
if (stringUtf8.size() && !size())
rawCopy(stringUtf8);
#else
// clear the string // clear the string
erase(); erase();
@ -146,6 +162,7 @@ void ucstring::fromUtf8(const std::string &stringUtf8)
push_back(code); push_back(code);
} }
} }
#endif
} }
void ucstring::rawCopy(const std::string &str) void ucstring::rawCopy(const std::string &str)

Loading…
Cancel
Save