pad.mattdiesel.co.uk

Snippet - formatString

formatString (C++)


Created 2017-01-17 14:45:39.379950 by Matt.

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <string>
#include <sstream>
#include <stdexcept>
#include <cctype>


/** Prints the Nth argument of a packed parameter list (specialization for length = 1)
 *
 * @author Diesel
 */
template<typename T>
bool fmtprint(std::ostream& os, int N, T v) {
	if (!N) {
		os << v;
		return true;
	}
	return false;
}

/** Prints the Nth argument of a packed parameter list.
 *
 * @author Diesel
 */
template<typename T, typename... Args>
bool fmtprint(std::ostream& os, int N, T first, Args... args) {
	if (!N) {
		os << first;
		return true;
	}
	return fmtprint(os, N-1, args...);
}



/** Formats a string according to a format pattern.
 * 
 * @author Diesel
 */
template<class ...TA>
std::string formatString(std::string f, TA... pargs) {
	std::ostringstream oss;

	std::string::size_type c = 0, p, n;
	for (;c < f.length();) {
		p = f.find('{', c);

		if (p == std::string::npos) {
			oss << f.substr(c);
			break;
		}

		oss << f.substr(c, p-c);

		n = 0;
		while (std::isdigit(f[++p])) {
			n = n*10 + (f[p]-'0');
		}

		if (f[p] != '}')
			throw std::invalid_argument("Format string bad.");
		p++;

		if (!fmtprint(oss, n, pargs...)) {
			throw std::out_of_range("Index was out of range of arguments");
		}

		c = p;
	}

	return oss.str();
}