Core/String: Add Format static method

This commit is contained in:
Lynix 2016-12-16 18:37:06 +01:00
parent 997a874c0e
commit 7de3c4905f
3 changed files with 38 additions and 1 deletions

View File

@ -11,6 +11,7 @@
#include <Nazara/Core/Endianness.hpp> #include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/SerializationContext.hpp> #include <Nazara/Core/SerializationContext.hpp>
#include <atomic> #include <atomic>
#include <cstdarg>
#include <iosfwd> #include <iosfwd>
#include <memory> #include <memory>
#include <string> #include <string>
@ -239,6 +240,8 @@ namespace Nz
static String Boolean(bool boolean); static String Boolean(bool boolean);
static int Compare(const String& first, const String& second); 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(float number);
static String Number(double number); static String Number(double number);
static String Number(long double number); static String Number(long double number);

View File

@ -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 * \brief Releases the content to the string
*/ */

View File

@ -5079,7 +5079,7 @@ namespace Nz
* \return The expected result * \return The expected result
* *
* \param first First string to use for comparison * \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) int String::Compare(const String& first, const String& second)
@ -5093,6 +5093,23 @@ namespace Nz
return std::strcmp(first.GetConstBuffer(), second.GetConstBuffer()); 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<SharedString>(length);
std::vsnprintf(str->string.get(), length + 1, format, args);
return String(std::move(str));
}
/*! /*!
* \brief Converts the number to string * \brief Converts the number to string
* \return String representation of the number * \return String representation of the number