aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/Util.cpp
diff options
context:
space:
mode:
authorTreeston <treeston.mmoc@gmail.com>2018-12-25 01:47:24 +0100
committerTreeston <treeston.mmoc@gmail.com>2018-12-25 01:47:24 +0100
commit6d6077e36fe9c5cb8ea8e4f981d637e72ee87037 (patch)
tree4b1bc526645b3fe02e03b5d08a23cd676eab1c3b /src/common/Utilities/Util.cpp
parentafdbc5ffe38e9b845f6ef123849139e846a540ae (diff)
Core/Utils: Fix Unicode handling
Diffstat (limited to 'src/common/Utilities/Util.cpp')
-rw-r--r--src/common/Utilities/Util.cpp24
1 files changed, 7 insertions, 17 deletions
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp
index d42997d522e..baa5e867fcf 100644
--- a/src/common/Utilities/Util.cpp
+++ b/src/common/Utilities/Util.cpp
@@ -18,6 +18,7 @@
#include "Util.h"
#include "Common.h"
+#include "Containers.h"
#include "IpAddress.h"
#include <utf8.h>
#include <algorithm>
@@ -252,18 +253,10 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
{
try
{
- size_t len = utf8::distance(utf8str, utf8str+csize);
- if (len > wsize)
- {
- if (wsize > 0)
- wstr[0] = L'\0';
- wsize = 0;
- return false;
- }
-
- wsize = len;
- utf8::utf8to16(utf8str, utf8str+csize, wstr);
- wstr[len] = L'\0';
+ Trinity::BufferWriteGuard<wchar_t> guard(wstr, wsize);
+ guard = utf8::utf8to16(utf8str, utf8str+csize, guard);
+ wsize -= guard.size(); // remaining unused space
+ wstr[wsize] = L'\0';
}
catch(std::exception)
{
@@ -278,13 +271,10 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
{
+ wstr.clear();
try
{
- if (size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size()))
- {
- wstr.resize(len);
- utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), &wstr[0]);
- }
+ utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), std::back_inserter(wstr));
}
catch(std::exception)
{