MCPcopy Create free account
hub / github.com/apache/brpc / string_printf

Function string_printf

src/butil/string_printf.cpp:80–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78} // end anonymous namespace
79
80std::string string_printf(const char* format, ...) {
81 // snprintf will tell us how large the output buffer should be, but
82 // we then have to call it a second time, which is costly. By
83 // guestimating the final size, we avoid the double snprintf in many
84 // cases, resulting in a performance win. We use this constructor
85 // of std::string to avoid a double allocation, though it does pad
86 // the resulting string with nul bytes. Our guestimation is twice
87 // the format string size, or 32 bytes, whichever is larger. This
88 // is a heuristic that doesn't affect correctness but attempts to be
89 // reasonably fast for the most common cases.
90 std::string ret;
91 va_list ap;
92 va_start(ap, format);
93 if (string_printf_impl(ret, std::max(32UL, strlen(format) * 2),
94 format, ap) != 0) {
95 ret.clear();
96 }
97 va_end(ap);
98 return ret;
99}
100
101std::string string_printf(size_t hint_size, const char* format, ...) {
102 // snprintf will tell us how large the output buffer should be, but

Callers 15

mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
thread_workerFunction · 0.85
mainFunction · 0.85
senderFunction · 0.85
senderFunction · 0.85
mainFunction · 0.85

Calls 3

string_printf_implFunction · 0.85
string_vprintfFunction · 0.85
clearMethod · 0.45

Tested by 15

TEST_FFunction · 0.68
TestLabelsFunction · 0.68
TESTFunction · 0.68
TestSuccessParallel2Method · 0.68
TEST_FFunction · 0.68
string_appenderFunction · 0.68
SendDataMethod · 0.68
TESTFunction · 0.68
TEST_FFunction · 0.68
TEST_FFunction · 0.68
TEST_FFunction · 0.68
rdlockerFunction · 0.68