roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
string_printf.cpp
Go to the documentation of this file.
2
3#include <memory>
4
5namespace roo_io {
6
7std::string StringPrintf(const char* format, ...) {
10 std::string result = StringVPrintf(format, arg);
11 va_end(arg);
12 return result;
13}
14
15std::string StringVPrintf(const char* format, va_list arg) {
16 // Opportunistically try with a small buffer, to minimize the risk of
17 // overflowing the stack, but also, avoid dynamic memory allocation in the
18 // common case.
19 char buf[128];
20 int len = vsnprintf(buf, 128, format, arg);
21
22 if (len <= 127) {
23 return std::string(buf, len);
24 }
25 std::unique_ptr<char[]> dbuf(new char[len + 1]);
26 vsnprintf(dbuf.get(), len + 1, format, arg);
27 return std::string(dbuf.get(), len);
28}
29
30} // namespace roo_io
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
std::string StringVPrintf(const char *format, va_list arg)
std::string StringPrintf(const char *format,...)