diff --git a/src/network/network_string.cpp b/src/network/network_string.cpp index 36182f9ba..8f0406780 100644 --- a/src/network/network_string.cpp +++ b/src/network/network_string.cpp @@ -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 diff --git a/src/network/network_string.hpp b/src/network/network_string.hpp index 94bcb4a99..8293f34a7 100644 --- a/src/network/network_string.hpp +++ b/src/network/network_string.hpp @@ -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); diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 3a657a53e..a74fb5287 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -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)) diff --git a/src/utils/utf8/checked.h b/src/utils/utf8/checked.h index aa9c6a7d2..0b4522fb8 100644 --- a/src/utils/utf8/checked.h +++ b/src/utils/utf8/checked.h @@ -289,7 +289,7 @@ namespace utf8 { while (start != end) { - uint32_t cp = start++; + uint32_t cp = *start++; if (cp > 0xffff) { //make a surrogate pair diff --git a/src/utils/utf8/unchecked.h b/src/utils/utf8/unchecked.h index f4b41e98d..57b9c0146 100644 --- a/src/utils/utf8/unchecked.h +++ b/src/utils/utf8/unchecked.h @@ -187,7 +187,7 @@ namespace utf8 { while (start != end) { - uint32_t cp = start++; + uint32_t cp = *start++; if (cp > 0xffff) { //make a surrogate pair