Core/Utils: Fix Unicode handling

This commit is contained in:
Treeston
2018-12-25 01:47:24 +01:00
parent afdbc5ffe3
commit 6d6077e36f
2 changed files with 37 additions and 17 deletions

View File

@@ -21,6 +21,8 @@
#include "Define.h" #include "Define.h"
#include "Random.h" #include "Random.h"
#include <algorithm> #include <algorithm>
#include <exception>
#include <iterator>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -38,6 +40,34 @@ namespace Trinity
return std::addressof(not_ptr); return std::addressof(not_ptr);
} }
template <class T>
class BufferWriteGuard
{
public:
using iterator_category = std::output_iterator_tag;
using value_type = void;
using pointer = T*;
using reference = T&;
using difference_type = std::ptrdiff_t;
BufferWriteGuard(T* buf, size_t n) : _buf(buf), _n(n) {}
T& operator*() const { check(); return *_buf; }
BufferWriteGuard& operator++() { check(); ++_buf; --_n; return *this; }
T* operator++(int) { check(); T* b = _buf; ++_buf; --_n; return b; }
size_t size() const { return _n; }
private:
T* _buf;
size_t _n;
void check()
{
if (!_n)
throw std::out_of_range("index");
}
};
namespace Containers namespace Containers
{ {
// replace with std::size in C++17 // replace with std::size in C++17

View File

@@ -18,6 +18,7 @@
#include "Util.h" #include "Util.h"
#include "Common.h" #include "Common.h"
#include "Containers.h"
#include "IpAddress.h" #include "IpAddress.h"
#include <utf8.h> #include <utf8.h>
#include <algorithm> #include <algorithm>
@@ -252,18 +253,10 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
{ {
try try
{ {
size_t len = utf8::distance(utf8str, utf8str+csize); Trinity::BufferWriteGuard<wchar_t> guard(wstr, wsize);
if (len > wsize) guard = utf8::utf8to16(utf8str, utf8str+csize, guard);
{ wsize -= guard.size(); // remaining unused space
if (wsize > 0) wstr[wsize] = L'\0';
wstr[0] = L'\0';
wsize = 0;
return false;
}
wsize = len;
utf8::utf8to16(utf8str, utf8str+csize, wstr);
wstr[len] = L'\0';
} }
catch(std::exception) 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) bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
{ {
wstr.clear();
try try
{ {
if (size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size())) utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), std::back_inserter(wstr));
{
wstr.resize(len);
utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), &wstr[0]);
}
} }
catch(std::exception) catch(std::exception)
{ {