mirror of
https://github.com/ziglang/zig.git
synced 2026-11-22 04:37:39 -05:00
preprocessor runs once
This commit is contained in:
1 parent
e71521335a
commit
d519ce87dd
6 files changed
+99
-32
No files matched your search
@@ -0,0 +1,25 @@
|
||||
#include "buffer.hpp"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Buf *buf_sprintf(const char *format, ...) {
|
||||
va_list ap, ap2;
|
||||
va_start(ap, format);
|
||||
va_copy(ap2, ap);
|
||||
|
||||
int len1 = vsnprintf(nullptr, 0, format, ap);
|
||||
assert(len1 >= 0);
|
||||
|
||||
size_t required_size = len1 + 1;
|
||||
|
||||
Buf *buf = buf_alloc_fixed(len1);
|
||||
|
||||
int len2 = vsnprintf(buf_ptr(buf), required_size, format, ap2);
|
||||
assert(len2 == len1);
|
||||
|
||||
va_end(ap2);
|
||||
va_end(ap);
|
||||
|
||||
return buf;
|
||||
}
|
||||
Reference in new issue
Block a user