Added a very simplified implementation of std::format

This commit is contained in:
Leon Styhre 2024-07-15 17:39:03 +02:00
parent 9a78863f03
commit 4ffc5d6bf9
2 changed files with 25 additions and 0 deletions

View file

@ -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;

View file

@ -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);