Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created May 13, 2014 08:37
Show Gist options
  • Select an option

  • Save psycharo-zz/d339d3787645c253e168 to your computer and use it in GitHub Desktop.

Select an option

Save psycharo-zz/d339d3787645c253e168 to your computer and use it in GitHub Desktop.
string string_format(const string fmt, ...)
{
int size = 512;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size) {
str.resize(n);
return str;
}
if (n > -1)
size = n + 1;
else
size *= 2;
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment