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.
8 #define va_copy(d,s) ((d) = (s)) //KLUGE: for MS compilers
13 static void StringAppendV(string
* dst
, const char* format
, va_list ap
) {
14 // First try with a small fixed size buffer
17 // It's possible for methods that use a va_list to invalidate
18 // the data in it upon use. The fix is to make a copy
19 // of the structure before using it and use that copy instead.
21 va_copy(backup_ap
, ap
);
22 int result
= vsnprintf(space
, sizeof(space
), format
, backup_ap
);
25 if ((result
>= 0) && (result
< sizeof(space
))) {
27 dst
->append(space
, result
);
31 // Repeatedly increase buffer size until it fits
32 int length
= sizeof(space
);
35 // Older behavior: just try doubling the buffer size
38 // We need exactly "result+1" characters
41 char* buf
= new char[length
];
43 // Restore the va_list before we use it again
44 va_copy(backup_ap
, ap
);
45 result
= vsnprintf(buf
, length
, format
, backup_ap
);
48 if ((result
>= 0) && (result
< length
)) {
50 dst
->append(buf
, result
);
58 string
StringPrintf(const char* format
, ...) {
62 StringAppendV(&result
, format
, ap
);
67 void SStringPrintf(string
* dst
, const char* format
, ...) {
71 StringAppendV(dst
, format
, ap
);
75 void StringAppendF(string
* dst
, const char* format
, ...) {
78 StringAppendV(dst
, format
, ap
);