| 23 | namespace text { |
| 24 | |
| 25 | std::string VFormat(const char* format, va_list argptr) { |
| 26 | // Counts actual characters. Null terminator is accounted separately. |
| 27 | const int initial_max_symbol_count = 1024; |
| 28 | |
| 29 | // Use vector as the output buffer for c-style formatting routines. |
| 30 | // Do not write directly to std::string internal storage because its |
| 31 | // structure and null terminator convention is not standardized. |
| 32 | std::vector<char> buffer(initial_max_symbol_count + 1 /*null terminator*/); |
| 33 | |
| 34 | // The va_list will be modified by the call to vsnprintf. Use a copy in case we need to try again. |
| 35 | va_list argptr2; |
| 36 | va_copy(argptr2, argptr); |
| 37 | const int symbol_count = vsnprintf(buffer.data(), buffer.size(), format, argptr2); |
| 38 | va_end(argptr2); |
| 39 | |
| 40 | if (symbol_count < 0) { |
| 41 | assert(false && "unexpected vsnprintf error"); |
| 42 | return {}; |
| 43 | } |
| 44 | if (symbol_count > initial_max_symbol_count) { |
| 45 | buffer.resize(symbol_count + 1 /*null terminator*/); |
| 46 | vsnprintf(buffer.data(), buffer.size(), format, argptr); |
| 47 | } |
| 48 | std::string str(buffer.data()); |
| 49 | return str; |
| 50 | } |
| 51 | |
| 52 | std::string Format(const char* format, ...) { |
| 53 | va_list argptr; |
no test coverage detected