dml: Replace usage of wchar_t with char16_t

This commit is contained in:
Joshua Scott 2018-03-30 20:05:01 +01:00
parent 2e17370337
commit 2f56518b66
3 changed files with 12 additions and 11 deletions

View File

@ -13,7 +13,7 @@ namespace dml
typedef int32_t INT; typedef int32_t INT;
typedef uint32_t UINT; typedef uint32_t UINT;
typedef std::string STR; typedef std::string STR;
typedef std::wstring WSTR; typedef std::u16string WSTR;
typedef float FLT; typedef float FLT;
typedef double DBL; typedef double DBL;
typedef uint64_t GID; typedef uint64_t GID;

View File

@ -14,7 +14,7 @@ namespace dml
if (data.buff[0] == ((m_value.length() & 0xFF00) >> 8)) if (data.buff[0] == ((m_value.length() & 0xFF00) >> 8))
std::reverse(&data.buff[0], &data.buff[1]); std::reverse(&data.buff[0], &data.buff[1]);
ostream.write(data.buff, sizeof(USHRT)); ostream.write(data.buff, sizeof(USHRT));
ostream.write((char *)m_value.c_str(), m_value.length() * sizeof(wchar_t)); ostream.write((char *)m_value.c_str(), m_value.length() * sizeof(char16_t));
} }
template <> template <>
@ -29,22 +29,22 @@ namespace dml
std::reverse(&length_data.buff[0], &length_data.buff[1]); std::reverse(&length_data.buff[0], &length_data.buff[1]);
// Read the data into a buffer // Read the data into a buffer
size_t length = length_data.value * sizeof(wchar_t); size_t length = length_data.value * sizeof(char16_t);
char *data = new char[length + sizeof(wchar_t)]{ 0 }; char *data = new char[length + sizeof(char16_t)]{ 0 };
istream.read(data, length); istream.read(data, length);
for (int i = 0; i < length; i += 2) for (int i = 0; i < length; i += 2)
{ {
if (endianness_check.buff[0] == 0x01) if (endianness_check.buff[0] == 0x01)
std::reverse(&data[i], &data[i + 1]); std::reverse(&data[i], &data[i + 1]);
} }
m_value = WSTR((wchar_t *)data); m_value = WSTR((char16_t *)data);
delete[] data; delete[] data;
} }
template <> template <>
size_t WStrField::get_size() const size_t WStrField::get_size() const
{ {
return sizeof(USHRT) + (m_value.length() * sizeof(wchar_t)); return sizeof(USHRT) + (m_value.length() * sizeof(char16_t));
} }
} }
} }

View File

@ -108,7 +108,7 @@ TEST_CASE("Field Serialization", "[dml]")
SECTION("WSTR Fields") SECTION("WSTR Fields")
{ {
record->add_field<WSTR>("TestWStr")->set_value(L"TEST"); record->add_field<WSTR>("TestWStr")->set_value(u"TEST");
record->write_to(ss); record->write_to(ss);
union union
@ -119,9 +119,10 @@ TEST_CASE("Field Serialization", "[dml]")
memcpy(length_bytes.buff, ss.str().data(), 2); memcpy(length_bytes.buff, ss.str().data(), 2);
REQUIRE(length_bytes.length == 0x4); REQUIRE(length_bytes.length == 0x4);
wchar_t value_buff[5] = { 0 }; char16_t value_buff[5] = { 0 };
memcpy(value_buff, ss.str().substr(2).data(), 8); memcpy(value_buff, ss.str().substr(2).data(), 8);
REQUIRE(wcscmp(value_buff, L"TEST") == 0); std::u16string value(value_buff);
REQUIRE(value == u"TEST");
} }
SECTION("FLT Fields") SECTION("FLT Fields")
@ -227,13 +228,13 @@ TEST_CASE("Field Deserialization", "[dml]")
{ {
char buff[10]; char buff[10];
memcpy(buff, "\x04\x00", sizeof(USHRT)); memcpy(buff, "\x04\x00", sizeof(USHRT));
memcpy(&buff[2], (char *)L"TEST", 8); memcpy(&buff[2], (char *)u"TEST", 8);
ss.write(buff, 10); ss.write(buff, 10);
ss.seekg(std::stringstream::beg); ss.seekg(std::stringstream::beg);
auto *field = record->add_field<WSTR>("TestWStr"); auto *field = record->add_field<WSTR>("TestWStr");
record->read_from(ss); record->read_from(ss);
REQUIRE(field->get_value() == L"TEST"); REQUIRE(field->get_value() == u"TEST");
} }
SECTION("FLT Fields") SECTION("FLT Fields")