-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_printf.cpp
More file actions
81 lines (66 loc) · 2.56 KB
/
my_printf.cpp
File metadata and controls
81 lines (66 loc) · 2.56 KB
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
72
73
74
75
76
77
78
79
80
81
#include <bitset>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using std::cout;
using std::endl;
constexpr int my_find(const char *s, int pos = 0) {
return s[0] == '\0' ? -1 : (s[0] == '%' ? pos : my_find(s + 1, pos + 1));
}
template <typename T, char U>
constexpr bool typeIsCorrect(const char *format, T t) {
return format[my_find(format) + 1] == U;
}
// constexpr bool typeIsCorrect(const char* format, int i)
// {
// return typeIsCorrect<int, 'i'>(format, i);
// }
template <> constexpr bool typeIsCorrect<int, 'i'>(const char *format, int i);
template <>
constexpr bool typeIsCorrect<const char *, 's'>(const char *format,
const char *s);
// constexpr bool typeIsCorrect(const char* format, const char* s)
// {
// return typeIsCorrect<const char *, 's'>(format, s);
// }
template <typename Head>
constexpr bool typesAreCorrect(const char *format, Head head) {
return typeIsCorrect(format, head);
}
template <typename Head, typename... Tail>
constexpr bool typesAreCorrect(const char *format, Head head, Tail... tail) {
return typeIsCorrect(format, head) &&
typesAreCorrect(format + my_find(format) + 1, tail...);
}
// void printLeadingChunk
template <typename Head>
constexpr bool my_printf(std::string format, Head head) {
// cout << "##instring is '" << format << "'" << endl;
size_t pos = format.find("%");
cout << format.substr(0, pos) << head << format.substr(pos + 2) << endl;
}
template <typename Head, typename... Tail>
constexpr bool my_printf(std::string format, Head head, Tail... tail) {
// cout << "#instring is '" << format << "'" << endl;
size_t pos = format.find("%");
/// cout << "#first chunk is '" << format.substr(0, pos) << "'" << endl;
cout << format.substr(0, pos) << head;
my_printf(format.substr(pos + 2), tail...);
}
#define MY_PRINTF(f, args...) \
do { \
static_assert(typesAreCorrect(f, args), "Type does not match"); \
my_printf(f, args); \
} while (false);
int main() {
static_assert(my_find("bajskorv") == -1, "Hej hej");
static_assert(my_find("bajs%") == 4, "Hej hej");
static_assert(typeIsCorrect("bajs%s", "hej"), "Hej hej");
static_assert(typeIsCorrect("bajs%i", 1), "Wrong type for %i");
MY_PRINTF("bajs%s\n", "korv");
MY_PRINTF("bajs%i\n", 42);
MY_PRINTF("bajs %s %s %sen", "k", "o", "rv");
}