blob: d4fe74f7317fbaa8bc29017c6dc70a51ec066dba [file] [log] [blame]
Shinichiro Hamaji776ca302015-06-06 03:52:48 +09001#include "stringprintf.h"
2
3#include <assert.h>
4#include <stdarg.h>
5
6string StringPrintf(const char* format, ...) {
7 string str;
8 str.resize(128);
9 for (int i = 0; i < 2; i++) {
10 va_list args;
11 va_start(args, format);
12 int ret = vsnprintf(&str[0], str.size(), format, args);
13 va_end(args);
14 assert(ret >= 0);
15 if (static_cast<size_t>(ret) < str.size()) {
16 str.resize(ret);
17 return str;
18 }
19 str.resize(ret + 1);
20 }
21 assert(false);
22}