From 3ab2d9575e4be758a65884d526721fc8bdc6c777 Mon Sep 17 00:00:00 2001 From: Treeston Date: Fri, 31 Jan 2020 20:49:10 +0100 Subject: [PATCH] Core/Utils: Fix Unicode handling --- src/common/Utilities/Containers.h | 30 ++++++++++++++++++++++++++++++ src/common/Utilities/Util.cpp | 28 +++++++++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index 3b04682879..d0340a3b60 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -21,6 +21,8 @@ #include "Define.h" #include "Random.h" #include +#include +#include #include #include @@ -38,6 +40,34 @@ namespace Trinity return std::addressof(not_ptr); } + template + 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 { // replace with std::size in C++17 diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 695b497054..015a23c4ce 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -17,6 +17,7 @@ #include "Util.h" #include "Common.h" +#include "Containers.h" #include "IpAddress.h" #include #include @@ -285,20 +286,12 @@ 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 guard(wstr, wsize); + guard = utf8::utf8to16(utf8str, utf8str + csize, guard); + wsize -= guard.size(); // remaining unused space + wstr[wsize] = L'\0'; } - catch(std::exception) + catch (std::exception) { if (wsize > 0) wstr[0] = L'\0'; @@ -311,15 +304,12 @@ 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) + catch (std::exception) { wstr.clear(); return false;