File tree 2 files changed +33
-10
lines changed 2 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -34,24 +34,35 @@ int strb_grow(strb *sb, size_t n) {
34
34
return 0 ;
35
35
}
36
36
37
- void strb_appendf (strb * sb , const char * f , ... ) {
38
- va_list ap ;
39
- int s ;
37
+ void strb_appendv (strb * sb , const char * f , va_list ap ) {
38
+ va_list apSave ;
39
+ int s ;
40
40
41
- va_start (ap , f );
42
41
#ifdef _MSC_VER
43
- s = _vscprintf (f , ap );
42
+ /**
43
+ * va_copy() is a C99 novelty that a particular company should have started
44
+ * supporting a long time ago, to their undying shame.
45
+ */
46
+
47
+ apSave = ap ;
48
+ s = _vscprintf (f , apSave );
44
49
#else
45
- s = vsnprintf (NULL , 0 , f , ap );
50
+ va_copy (apSave , ap );
51
+ s = vsnprintf (NULL , 0 , f , apSave );
46
52
#endif
47
- va_end (ap );
48
-
53
+ va_end (apSave );
54
+
49
55
if (s < 0 ) { strb_seterror (sb ); return ; }
50
56
s += 1 ;
51
57
52
58
if (strb_ensure (sb , s )) return ;
53
- va_start (ap , f );
54
59
s = vsnprintf (sb -> s + sb -> l , s , f , ap );
55
- va_end (ap );
56
60
sb -> l += s ;
57
61
}
62
+
63
+ void strb_appendf (strb * sb , const char * f , ...) {
64
+ va_list ap ;
65
+ va_start (ap , f );
66
+ strb_appendv (sb , f , ap );
67
+ va_end (ap );
68
+ }
Original file line number Diff line number Diff line change 2
2
#define STRB_H
3
3
4
4
#include "private_config.h"
5
+ #include <stdarg.h>
5
6
6
7
#ifdef __cplusplus
7
8
extern "C" {
@@ -170,6 +171,17 @@ static inline void strb_appendb(strb *sb, strb *sb2) {
170
171
*/
171
172
GPUARRAY_LOCAL void strb_appendf (strb * , const char * f , ...);
172
173
174
+ /*
175
+ * Appends the result of a sprintf using the format string `f` and
176
+ * following variadic arguments list, excluding terminating nul.
177
+ *
178
+ * Unlike sprintf, this function makes sure not to run off the end of
179
+ * memory and behaves like asprintf in that respect.
180
+ *
181
+ * A format error will place the strb in error mode.
182
+ */
183
+ GPUARRAY_LOCAL void strb_appendv (strb * , const char * f , va_list ap );
184
+
173
185
/*
174
186
* Returns a C string from the content of the strb.
175
187
*
You can’t perform that action at this time.
0 commit comments