diff options
Diffstat (limited to 'src/common/Utilities')
-rw-r--r-- | src/common/Utilities/StringFormat.h | 13 | ||||
-rw-r--r-- | src/common/Utilities/Util.cpp | 17 |
2 files changed, 19 insertions, 11 deletions
diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index 7c136185f4a..b2248aff6f1 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -39,6 +39,19 @@ namespace Trinity } } + template<typename OutputIt, typename... Args> + inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args) + { + try + { + return fmt::format_to(out, fmt, std::forward<Args>(args)...); + } + catch (std::exception const& formatError) + { + return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); + } + } + /// Returns true if the given char pointer is null. inline bool IsFormatEmptyOrNull(char const* fmt) { diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index fdd49b49855..ac97e724902 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -843,15 +843,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*/) @@ -871,10 +869,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<uint8>(str.substr(i, 2), 16).value_or(0); } bool StringEqualI(std::string_view a, std::string_view b) |