Add utf32 / wchar_t handling in network string
This commit is contained in:
@@ -111,7 +111,56 @@ int BareNetworkString::decodeStringW(irr::core::stringw *out) const
|
||||
int len = decodeString(&s);
|
||||
*out = StringUtils::utf8ToWide(s);
|
||||
return len;
|
||||
} // decodeString
|
||||
} // decodeStringW
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** Encode string with max length of 16bit and utf32, used in motd or
|
||||
* chat. */
|
||||
BareNetworkString& BareNetworkString::encodeString16(
|
||||
const irr::core::stringw& value)
|
||||
{
|
||||
size_t utf32_len = 0;
|
||||
const uint32_t* utf32 = NULL;
|
||||
std::u32string convert;
|
||||
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
convert = StringUtils::wideToUtf32(value);
|
||||
utf32_len = convert.size();
|
||||
utf32 = (const uint32_t*)convert.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
utf32_len = value.size();
|
||||
utf32 = (const uint32_t*)value.c_str();
|
||||
}
|
||||
|
||||
uint16_t str_len = (uint16_t)utf32_len;
|
||||
if (utf32_len > 65535)
|
||||
str_len = 65535;
|
||||
addUInt16(str_len);
|
||||
for (unsigned i = 0; i < str_len; i++)
|
||||
addUInt32(utf32[i]);
|
||||
return *this;
|
||||
} // encodeString16
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int BareNetworkString::decodeString16(irr::core::stringw* out) const
|
||||
{
|
||||
uint16_t str_len = getUInt16();
|
||||
std::u32string convert;
|
||||
for (unsigned i = 0; i < str_len; i++)
|
||||
{
|
||||
uint32_t c = getUInt32();
|
||||
if (sizeof(wchar_t) == 2)
|
||||
convert += (char32_t)c;
|
||||
else
|
||||
out->append((wchar_t)c);
|
||||
}
|
||||
if (str_len > 0 && !convert.empty())
|
||||
*out = StringUtils::utf32ToWide(convert);
|
||||
return str_len * 4 + 2;
|
||||
} // decodeString16
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** Returns a string representing this message suitable to be printed
|
||||
|
||||
@@ -146,32 +146,9 @@ public:
|
||||
/** Allows one to read a buffer from the beginning again. */
|
||||
void reset() { m_current_offset = 0; }
|
||||
// ------------------------------------------------------------------------
|
||||
/** Encode string with max length of 16bit and utf32, used in motd or
|
||||
* chat. */
|
||||
BareNetworkString& encodeString16(const irr::core::stringw& value)
|
||||
{
|
||||
uint16_t str_len = (uint16_t)value.size();
|
||||
if (value.size() > 65535)
|
||||
str_len = 65535;
|
||||
addUInt16(str_len);
|
||||
for (unsigned i = 0; i < str_len; i++)
|
||||
addUInt32(value[i]);
|
||||
return *this;
|
||||
}
|
||||
BareNetworkString& encodeString16(const irr::core::stringw& value);
|
||||
// ------------------------------------------------------------------------
|
||||
int decodeString16(irr::core::stringw* out) const
|
||||
{
|
||||
uint16_t str_len = getUInt16();
|
||||
for (unsigned i = 0; i < str_len; i++)
|
||||
{
|
||||
uint32_t c = getUInt32();
|
||||
// In the future convert for windows
|
||||
if (c > 65535)
|
||||
c = 20; // whitespace
|
||||
out->append((wchar_t)c);
|
||||
}
|
||||
return str_len * 4 + 2;
|
||||
}
|
||||
int decodeString16(irr::core::stringw* out) const;
|
||||
// ------------------------------------------------------------------------
|
||||
BareNetworkString& encodeString(const std::string &value);
|
||||
BareNetworkString& encodeString(const irr::core::stringw &value);
|
||||
|
||||
@@ -1251,7 +1251,7 @@ namespace StringUtils
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
const uint32_t* chars = (const uint32_t*)input.c_str();
|
||||
utf8::utf16to32(chars, chars + input.size(),
|
||||
utf8::utf32to16(chars, chars + input.size(),
|
||||
back_inserter(wchar_line));
|
||||
}
|
||||
else if (sizeof(wchar_t) == sizeof(char32_t))
|
||||
|
||||
@@ -289,7 +289,7 @@ namespace utf8
|
||||
{
|
||||
while (start != end)
|
||||
{
|
||||
uint32_t cp = start++;
|
||||
uint32_t cp = *start++;
|
||||
if (cp > 0xffff)
|
||||
{
|
||||
//make a surrogate pair
|
||||
|
||||
@@ -187,7 +187,7 @@ namespace utf8
|
||||
{
|
||||
while (start != end)
|
||||
{
|
||||
uint32_t cp = start++;
|
||||
uint32_t cp = *start++;
|
||||
if (cp > 0xffff)
|
||||
{
|
||||
//make a surrogate pair
|
||||
|
||||
Reference in New Issue
Block a user