您的位置:首页 > 其它

std::string stringf(const char* format, ...)

2015-01-19 09:48 465 查看
std::string stringf(const char* format, ...)
{
va_list arg_list;

va_start(arg_list, format);

// SUSv2 version doesn't work for buf NULL/size 0, so try printing
// into a small buffer that avoids the double-rendering and alloca path too...
char short_buf[256];
const size_t needed = vsnprintf(short_buf, sizeof short_buf,
format, arg_list) + 1;
if (needed <= sizeof short_buf)
return short_buf;

// need more space...
char* p = static_cast<char*>(alloca(needed));
vsnprintf(p, needed, format, arg_list);
return p;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐