| 78 | } // end anonymous namespace |
| 79 | |
| 80 | std::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 | |
| 101 | std::string string_printf(size_t hint_size, const char* format, ...) { |
| 102 | // snprintf will tell us how large the output buffer should be, but |