mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
Add STL string formatters
Add STL string formatters, these formatters allow for formatting the STL strings using C-style format specifiers.
This commit is contained in:
parent
be3690d0b3
commit
ed7186e4d3
@ -1014,6 +1014,48 @@ string PrintPercentageEscape(const string& svInput)
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// For formatting a STL string using C-style format specifiers (va_list version).
|
||||
string FormatV(const char* szFormat, va_list args)
|
||||
{
|
||||
// Initialize use of the variable argument array.
|
||||
va_list argsCopy;
|
||||
va_copy(argsCopy, args);
|
||||
|
||||
// Dry run to obtain required buffer size.
|
||||
const int iLen = std::vsnprintf(nullptr, 0, szFormat, argsCopy);
|
||||
va_end(argsCopy);
|
||||
|
||||
assert(iLen >= 0);
|
||||
string result;
|
||||
|
||||
if (iLen < 0)
|
||||
{
|
||||
result.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
result.resize(iLen);
|
||||
std::vsnprintf(&result[0], iLen+sizeof(char), szFormat, args);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// For formatting a STL string using C-style format specifiers.
|
||||
string Format(const char* szFormat, ...)
|
||||
{
|
||||
string result;
|
||||
|
||||
va_list args;
|
||||
va_start(args, szFormat);
|
||||
result = FormatV(szFormat, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// For obtaining a duration from a certain interval.
|
||||
std::chrono::nanoseconds IntervalToDuration(const float flInterval)
|
||||
|
@ -79,6 +79,9 @@ void PrintM128i64(__m128i in);
|
||||
void AppendPrintf(char* pBuffer, size_t nBufSize, char const* pFormat, ...);
|
||||
string PrintPercentageEscape(const string& svInput);
|
||||
|
||||
string FormatV(const char* szFormat, va_list args);
|
||||
string Format(const char* szFormat, ...);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Time
|
||||
std::chrono::nanoseconds IntervalToDuration(const float flInterval);
|
||||
|
Loading…
x
Reference in New Issue
Block a user