1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Add a helper funtion to generate formated string.

This commit is contained in:
Sandu Liviu Catalin 2020-09-06 23:54:28 +03:00
parent 50a61c69e6
commit 67f514c77c
2 changed files with 60 additions and 0 deletions

View File

@ -941,6 +941,56 @@ bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String &
return true;
}
// ------------------------------------------------------------------------------------------------
size_t PrintToStrF(String & out, CSStr str, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, str);
// Forward to the actual implementation
const size_t r = PrintToStrFv(out, str, args);
// Finalize the variable argument list
va_end(args);
// Return result
return r;
}
// ------------------------------------------------------------------------------------------------
size_t PrintToStrFv(String & out, CSStr str, va_list vl)
{
va_list args;
// Backup original size to revert back if necessary
const size_t size = out.size();
// The estimated buffer required
ssize_t len = 256;
begin:
// Do not modify the original va_list
va_copy(args, vl);
// Reserve the necessary space
out.resize(size + static_cast< size_t >(len), '\0');
// Attempt to generate the specified string
int res = std::vsnprintf(&out[0] + size, len, str, args);
// Do we need more space?
if (res >= len)
{
// Adjust to required size
len = res + 1;
// Try again
goto begin;
}
// Did the format failed?
else if (res < 0)
{
// Discard changes
out.resize(size);
}
else
{
// Discard extra characters
out.resize(size + static_cast< size_t >(res));
}
// Return the amount of written characters
return static_cast< size_t >(res);
}
// ------------------------------------------------------------------------------------------------
void SqThrowLastF(CSStr msg, ...)
{

View File

@ -175,4 +175,14 @@ template < typename T > inline void SqSetDelimiter(SQInteger c)
*/
bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String & spec);
/* ------------------------------------------------------------------------------------------------
* Append a formatted string to a string container.
*/
size_t PrintToStrF(String & out, CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Append a formatted string to a string container.
*/
size_t PrintToStrFv(String & out, CSStr str, va_list vl);
} // Namespace:: SqMod