-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.h
44 lines (39 loc) · 1.43 KB
/
format.h
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
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
// Base case of recursion: No arguments left to process
// Recursive function to process arguments one by one
template <typename T, typename... Args>
void format_impl(std::ostringstream &oss, const std::string &fmt, size_t pos,
const T &arg, const Args &...args) {
size_t next_brace = fmt.find('{', pos);
if (next_brace != std::string::npos) {
// Process the part of the format string before the current placeholder
oss << fmt.substr(pos, next_brace - pos);
// Process the argument
size_t end_brace = fmt.find('}', next_brace + 1);
if (end_brace != std::string::npos) {
oss << " " << std::boolalpha << arg; // here adds a white space
// Continue with the rest of the format string and remaining arguments
format_impl(oss, fmt, end_brace + 1, args...);
} else {
// Invalid format string (missing closing brace)
oss << fmt.substr(next_brace);
}
} else {
// No more placeholders, append the rest of the format string
oss << fmt.substr(pos);
}
}
// Entry point for format function
template <typename... Args>
void format(const std::string &fmt, const Args &...args) {
std::ostringstream oss;
format_impl(oss, fmt, 0, args...);
std::cout << oss.str();
}
template <typename... Args>
void format_impl(std::ostringstream &oss, const std::string &fmt, size_t pos) {
oss << fmt.substr(pos);
}