From aaa6e73c8ca6d60e943cb964605536eb78219db2 Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 15 Aug 2023 20:10:04 +0200 Subject: Core/Logging: Switch from fmt::sprintf to fmt::format (c++20 standard compatible api) (cherry picked from commit d791afae1dfcfaf592326f787755ca32d629e4d3) --- src/common/Utilities/StringFormat.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/common/Utilities/StringFormat.h') diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index da1615575a4..b4d4d9e5743 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -18,22 +18,24 @@ #ifndef TRINITYCORE_STRING_FORMAT_H #define TRINITYCORE_STRING_FORMAT_H -#include "fmt/printf.h" +#include "fmt/core.h" namespace Trinity { + template + using FormatString = std::string_view; + /// Default TC string format function. - template - inline std::string StringFormat(Format&& fmt, Args&&... args) + template + inline std::string StringFormat(FormatString fmt, Args&&... args) { try { - return fmt::sprintf(std::forward(fmt), std::forward(args)...); + return fmt::format(fmt, std::forward(args)...); } - catch (const fmt::format_error& formatError) + catch (std::exception const& formatError) { - std::string error = "An error occurred formatting string \"" + std::string(fmt) + "\" : " + std::string(formatError.what()); - return error; + return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); } } @@ -48,6 +50,12 @@ namespace Trinity { return fmt.empty(); } + + /// Returns true if the given std::string is empty. + inline constexpr bool IsFormatEmptyOrNull(std::string_view fmt) + { + return fmt.empty(); + } } #endif -- cgit v1.2.3