-
Notifications
You must be signed in to change notification settings - Fork 0
/
Print.h
146 lines (125 loc) · 3.87 KB
/
Print.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef PRINT_H
#define PRINT_H
#include <iostream>
#include <thread>
#ifndef SNPRINTF
#ifndef STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#endif
#define SNPRINTF stbsp_snprintf
#endif
struct String_Buffer {
char *data; // Pointer to start of data
size_t length; // How much we have used
size_t capacity; // How much we have total
};
template<typename T>
inline T min(T x, T y)
{
return x < y ? x : y;
}
inline void sprint1(String_Buffer *buffer, const char *cstr)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%s", cstr);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, char c)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%c", c);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, int x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%d", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, long int x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%ld", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, long long int x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%lld", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, unsigned x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%u", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, long unsigned x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%lu", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, long long unsigned x)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%llu", x);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, float f)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%f", f);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, double f)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
"%f", f);
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
inline void sprint1(String_Buffer *buffer, bool b)
{
int n = SNPRINTF(
buffer->data + buffer->length,
buffer->capacity - buffer->length,
b ? "true" : "false");
buffer->length = min(buffer->length + n, buffer->capacity - 1);
}
template<typename ...Types>
inline void sprint(String_Buffer *buffer, Types... args)
{
(sprint1(buffer, args), ...);
}
template<typename ...Types>
inline void print(Types... args)
{
thread_local char data[1024 * 4];
String_Buffer buffer = {};
buffer.data = data;
buffer.capacity = sizeof(data);
(sprint1(&buffer, args), ...);
std::cout.write(buffer.data, buffer.length);
std::cout.flush(); // TODO: Is there a better way?
}
#endif // PRINT_H