forked from simonask/ftl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.hpp
More file actions
84 lines (76 loc) · 2 KB
/
Copy pathprint.hpp
File metadata and controls
84 lines (76 loc) · 2 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
82
83
84
#pragma once
#ifndef PRINT_HPP_A98FKY08
#define PRINT_HPP_A98FKY08
#include "string.hpp"
#include "stringref.hpp"
#include "stringbuffer.hpp"
#include "type_conversion.hpp"
#include <stdio.h>
#include <inttypes.h>
namespace ftl {
template <typename... Args>
void print(const StringRef& fmt, Args... args) {
String s = format(fmt, args...);
printf(s.c_str());
}
template <typename T>
inline void convert_variadic_arguments_to_strings(String* target, const T& x) {
*target = TypeConverter<T, String>::convert(x);
}
template <typename T, typename... Rest>
inline void convert_variadic_arguments_to_strings(String* target, const T& x, const Rest&... rest) {
*target = TypeConverter<T, String>::convert(x);
convert_variadic_arguments_to_strings(target+1, rest...);
}
template <typename... Args>
String format(const StringRef& fmt, Args... args) {
const size_t num_args = sizeof...(args);
String converted[num_args];
convert_variadic_arguments_to_strings(converted, args...);
StringBuffer buffer;
const char* c = fmt.c_str();
bool escaping = false;
for (size_t i = 0; i < fmt.length(); ++i) {
switch (c[i]) {
case '\\': {
if (escaping) { escaping = false; buffer.push('\\'); continue; }
escaping = true;
continue;
}
case '{': {
if (!escaping) {
size_t idx = 0;
bool valid = true;
bool anything = false;
size_t j = i + 1;
for (; j < fmt.length(); ++j) {
if (is_numeric(c[j])) {
idx *= 10;
idx += c[j] - '0';
anything = true;
} else if (c[j] == '}') {
break;
} else {
// non-numeric, non-terminator
valid = false;
break;
}
}
if (valid && anything && idx < num_args) {
buffer << converted[idx];
i = j;
continue;
}
}
}
}
escaping = false;
buffer.push(c[i]);
}
return buffer.to_string();
}
String format(const StringRef& fmt) {
return fmt;
}
}
#endif /* end of include guard: PRINT_HPP_A98FKY08 */