diff options
Diffstat (limited to 'src/common/Utilities')
-rw-r--r-- | src/common/Utilities/Util.cpp | 2 | ||||
-rw-r--r-- | src/common/Utilities/Util.h | 5 | ||||
-rw-r--r-- | src/common/Utilities/advstd.h | 52 |
3 files changed, 57 insertions, 2 deletions
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index f5ca8a6ac4e..7625ae9d2ff 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -699,7 +699,7 @@ bool Utf8ToUpperOnlyLatin(std::string& utf8String) return WStrToUtf8(wstr, utf8String); } -std::string ByteArrayToHexStr(uint8 const* bytes, uint32 arrayLen, bool reverse /* = false */) +std::string ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen, bool reverse /* = false */) { int32 init = 0; int32 end = arrayLen; diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index 5711397f904..b3270eb6c2c 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -22,6 +22,7 @@ #include "Errors.h" #include <string> #include <vector> +#include "advstd.h" enum LocaleConstant : uint8; @@ -301,7 +302,9 @@ TC_COMMON_API bool IsIPAddress(char const* ipaddress); TC_COMMON_API uint32 CreatePIDFile(std::string const& filename); TC_COMMON_API uint32 GetPID(); -TC_COMMON_API std::string ByteArrayToHexStr(uint8 const* bytes, uint32 length, bool reverse = false); +TC_COMMON_API std::string ByteArrayToHexStr(uint8 const* bytes, size_t length, bool reverse = false); +template <typename Container> +std::string ByteArrayToHexStr(Container const& c, bool reverse = false) { return ByteArrayToHexStr(advstd::data(c), advstd::size(c), reverse); } TC_COMMON_API void HexStrToByteArray(std::string const& str, uint8* out, bool reverse = false); TC_COMMON_API bool StringToBool(std::string const& str); diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h new file mode 100644 index 00000000000..7514f156050 --- /dev/null +++ b/src/common/Utilities/advstd.h @@ -0,0 +1,52 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_ADVSTD_H +#define TRINITY_ADVSTD_H + +#include <cstddef> +#include <initializer_list> +#include <type_traits> + +// this namespace holds implementations of upcoming stdlib features that our c++ version doesn't have yet +namespace advstd +{ + // C++17 std::size + template <typename C> + constexpr auto size(const C& c) { return c.size(); } + + template <typename T, std::size_t N> + constexpr std::size_t size(const T(&)[N]) noexcept { return N; } + + // C++17 std::data + template <typename C> + constexpr auto data(C& c) { return c.data(); } + + template <typename C> + constexpr auto data(C const& c) { return c.data(); } + + template <typename T, std::size_t N> + constexpr T* data(T(&a)[N]) noexcept { return a; } + + template <typename T, std::size_t N> + constexpr T const* data(const T(&a)[N]) noexcept { return a; } + + template <typename T> + constexpr T const* data(std::initializer_list<T> l) noexcept { return l.begin(); } +} + +#endif |