diff --git a/include/Nazara/Core/String.hpp b/include/Nazara/Core/String.hpp index ba7f34ff7..c3056e835 100644 --- a/include/Nazara/Core/String.hpp +++ b/include/Nazara/Core/String.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -239,6 +240,8 @@ namespace Nz static String Boolean(bool boolean); static int Compare(const String& first, const String& second); + static inline String Format(const char* format, ...); + static String FormatVA(const char* format, va_list arg); static String Number(float number); static String Number(double number); static String Number(long double number); diff --git a/include/Nazara/Core/String.inl b/include/Nazara/Core/String.inl index a5e099a3c..f4cf48714 100644 --- a/include/Nazara/Core/String.inl +++ b/include/Nazara/Core/String.inl @@ -19,6 +19,23 @@ namespace Nz { } + /*! + * \brief Build a string using a format and returns it + * \return Formatted string + * + * \param format String format + * \param ... Format arguments + */ + String String::Format(const char* format, ...) + { + va_list args; + va_start(args, format); + String result = FormatVA(format, args); + va_end(args); + + return result; + } + /*! * \brief Releases the content to the string */ diff --git a/src/Nazara/Core/String.cpp b/src/Nazara/Core/String.cpp index ac01bf92c..b0ff09ed1 100644 --- a/src/Nazara/Core/String.cpp +++ b/src/Nazara/Core/String.cpp @@ -5079,7 +5079,7 @@ namespace Nz * \return The expected result * * \param first First string to use for comparison - * \parma second Second string to use for comparison + * \param second Second string to use for comparison */ int String::Compare(const String& first, const String& second) @@ -5093,6 +5093,23 @@ namespace Nz return std::strcmp(first.GetConstBuffer(), second.GetConstBuffer()); } + /*! + * \brief Build a string using a format and returns it + * \return Formatted string + * + * \param format String format + * \param args Format arguments + */ + String String::FormatVA(const char* format, va_list args) + { + std::size_t length = std::vsnprintf(nullptr, 0, format, args); + + auto str = std::make_shared(length); + std::vsnprintf(str->string.get(), length + 1, format, args); + + return String(std::move(str)); + } + /*! * \brief Converts the number to string * \return String representation of the number