From a4299c2a4b88d1cbdcea1301a190da6081abf876 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 13 May 2023 15:46:27 +0200 Subject: Core/Misc: Replace string to int conversion functions from Common.h with c++17 std::from_chars based ones Trinity::StringTo (cherry picked from commit 3260b94dd627b7b0c7114f94bb97d108b005af3e) --- src/common/Utilities/Util.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'src/common/Utilities/Util.cpp') diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 6763685ba70..9c77e9f9821 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -658,15 +658,13 @@ std::string Trinity::Impl::ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen op = -1; } - std::ostringstream ss; + std::string result; + result.reserve(arrayLen * 2); + auto inserter = std::back_inserter(result); for (int32 i = init; i != end; i += op) - { - char buffer[4]; - sprintf(buffer, "%02X", bytes[i]); - ss << buffer; - } + Trinity::StringFormatTo(inserter, "{:02X}", bytes[i]); - return ss.str(); + return result; } void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t outlen, bool reverse /*= false*/) @@ -686,10 +684,7 @@ void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t o uint32 j = 0; for (int32 i = init; i != end; i += 2 * op) - { - char buffer[3] = { str[i], str[i + 1], '\0' }; - out[j++] = uint8(strtoul(buffer, nullptr, 16)); - } + out[j++] = Trinity::StringTo(str.substr(i, 2), 16).value_or(0); } bool StringEqualI(std::string_view a, std::string_view b) -- cgit v1.2.3