aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/fmt.cpp
blob: 397bf9f45c94146ccfc41e818a2d88ca9ff7d692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdarg>
#include <cstdio>
#include <string>

#include "fmt.h"

using namespace std;

string crepe::util::va_stringf(va_list args, const char * fmt) {
	string out;

	va_list args_copy;
	va_copy(args_copy, args);
	size_t length = vsnprintf(NULL, 0, fmt, args_copy);
	// resize to include terminating null byte
	out.resize(length + 1);
	va_end(args_copy);

	// vsnprintf adds terminating null byte
	vsnprintf(out.data(), out.size(), fmt, args);
	// resize to actual length
	out.resize(length);

	va_end(args);

	return out;
}

string crepe::util::stringf(const char * fmt, ...) {
	va_list args;
	va_start(args, fmt);
	string out = va_stringf(args, fmt);
	va_end(args);
	return out;
}