1 // Copyright 2002 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 static void StringAppendV(string
* dst
, const char* format
, va_list ap
) {
10 // First try with a small fixed size buffer
13 // It's possible for methods that use a va_list to invalidate
14 // the data in it upon use. The fix is to make a copy
15 // of the structure before using it and use that copy instead.
17 va_copy(backup_ap
, ap
);
18 int result
= vsnprintf(space
, sizeof(space
), format
, backup_ap
);
21 if ((result
>= 0) && (result
< sizeof(space
))) {
23 dst
->append(space
, result
);
27 // Repeatedly increase buffer size until it fits
28 int length
= sizeof(space
);
31 // Older behavior: just try doubling the buffer size
34 // We need exactly "result+1" characters
37 char* buf
= new char[length
];
39 // Restore the va_list before we use it again
40 va_copy(backup_ap
, ap
);
41 result
= vsnprintf(buf
, length
, format
, backup_ap
);
44 if ((result
>= 0) && (result
< length
)) {
46 dst
->append(buf
, result
);
54 string
StringPrintf(const char* format
, ...) {
58 StringAppendV(&result
, format
, ap
);
63 void SStringPrintf(string
* dst
, const char* format
, ...) {
67 StringAppendV(dst
, format
, ap
);
71 void StringAppendF(string
* dst
, const char* format
, ...) {
74 StringAppendV(dst
, format
, ap
);