MCPcopy Create free account
hub / github.com/baidu/tera / StringFormatAppendVA

Function StringFormatAppendVA

src/common/base/string_format.cc:7–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include "common/base/string_format.h"
6
7size_t StringFormatAppendVA(std::string* dst, const char* format, va_list ap) {
8 // First try with a small fixed size buffer
9 char space[1024];
10 // It's possible for methods that use a va_list to invalidate
11 // the data in it upon use. The fix is to make a copy
12 // of the structure before using it and use that copy instead.
13 va_list backup_ap;
14 va_copy(backup_ap, ap);
15 int result = vsnprintf(space, sizeof(space), format, backup_ap);
16 va_end(backup_ap);
17 if ((result >= 0) && (result < static_cast<int>(sizeof(space)))) {
18 dst->append(space, result);
19 return result;
20 }
21 // Repeatedly increase buffer size until it fits
22 int length = sizeof(space);
23 while (true) {
24 if (result < 0) {
25 // Older behavior: just try doubling the buffer size
26 length *= 2;
27 } else {
28 // We need exactly "result+1" characters
29 length = result + 1;
30 }
31 char* buf = new char[length];
32 // Restore the va_list before we use it again
33 va_copy(backup_ap, ap);
34 result = vsnprintf(buf, length, format, backup_ap);
35 va_end(backup_ap);
36 if ((result >= 0) && (result < length)) {
37 dst->append(buf, result);
38 delete[] buf;
39 break;
40 }
41 delete[] buf;
42 }
43 return result;
44}
45
46size_t StringFormatAppend(std::string* dst, const char* format, ...) {
47 va_list ap;

Callers 3

StringFormatAppendFunction · 0.85
StringFormatToFunction · 0.85
StringFormatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected