Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 1 | #include "stringprintf.h" |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdarg.h> |
| 5 | |
| 6 | string 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 | } |