I'm trying to write a function which works like C#'s String::Format, where instead of taking arguments starting in '%' ("%d %s %i"), it takes arguments like "{0} {1} {2}", and I've gotten it to work (mostly).
It finds and replaces all occurrences correctly, but then breaks when it gets to the end of args. Right before it breaks the debugger shows 'result' getting set to "\f;@" where the final character is a random, "non-standard" character.
Notes**: 'string' is std::string, String::Format1 works correctly and uses vsnprintf_s, and String::Replace finds and replaces all occurrences of find with replace.
string String::Format2(const string format, ...)
{
string output = format;
va_list args;
va_start(args, format);
{
uint i = 0;
while (args[i] != NULL)
{
string find = String::Format1("{%i}", i);
// Breaks here
string replace = va_arg(args, const char*);
output = String::Replace(output, find, replace);
i++;
}
}
va_end(args);
return output;
}