From 4ffc5d6bf991117b5dd6ca84f35021d7f330e1c7 Mon Sep 17 00:00:00 2001 From: Leon Styhre <leon@leonstyhre.com> Date: Mon, 15 Jul 2024 17:39:03 +0200 Subject: [PATCH] Added a very simplified implementation of std::format --- es-core/src/utils/StringUtil.cpp | 24 ++++++++++++++++++++++++ es-core/src/utils/StringUtil.h | 1 + 2 files changed, 25 insertions(+) diff --git a/es-core/src/utils/StringUtil.cpp b/es-core/src/utils/StringUtil.cpp index 6a14197e9..7d29018e3 100644 --- a/es-core/src/utils/StringUtil.cpp +++ b/es-core/src/utils/StringUtil.cpp @@ -19,6 +19,7 @@ #include "utils/PlatformUtil.h" #include <algorithm> +#include <cstdarg> #include <locale> namespace Utils @@ -626,6 +627,29 @@ namespace Utils return result; } + std::string format(const std::string stringArg, ...) + { + if (stringArg.empty()) + return ""; + + // Extract all the variadic function arguments. + va_list args; + va_list copy; + + va_start(args, stringArg); + va_copy(copy, args); + + const int length {vsnprintf(nullptr, 0, &stringArg[0], copy)}; + std::string buffer(length, '\0'); + + vsnprintf(&buffer[0], length + 1, &stringArg[0], copy); + + va_end(copy); + va_end(args); + + return buffer; + } + std::wstring stringToWideString(const std::string& stringArg) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> stringConverter; diff --git a/es-core/src/utils/StringUtil.h b/es-core/src/utils/StringUtil.h index 9e7bfe188..7835f0c39 100644 --- a/es-core/src/utils/StringUtil.h +++ b/es-core/src/utils/StringUtil.h @@ -35,6 +35,7 @@ namespace Utils std::string replace(const std::string& stringArg, const std::string& from, const std::string& to); + std::string format(const std::string stringArg, ...); std::wstring stringToWideString(const std::string& stringArg); std::string wideStringToString(const std::wstring& stringArg); bool startsWith(const std::string& stringArg, const std::string& start);