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
|
#include "asl/format.hpp"
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>
// @Todo Improve this to use our utilities, not the C stdlib
static_assert(asl::formattable<decltype("Hello")>);
class StringSink : public asl::writer
{
int64_t m_current_len{};
char* m_data{};
public:
void write(const char* str, int64_t len) override
{
m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1));
memcpy(m_data + m_current_len, str, (size_t)len);
m_current_len += len;
m_data[m_current_len] = '\0';
}
constexpr const char* cstr() const { return m_data; }
void reset()
{
m_current_len = 0;
free(m_data);
m_data = nullptr;
}
};
int main()
{
StringSink sink;
// @Todo Use the testing framework
asl::format(&sink, "Hello, world!");
assert(strcmp(sink.cstr(), "Hello, world!") == 0);
sink.reset();
asl::format(&sink, "");
assert(strcmp(sink.cstr(), "") == 0);
sink.reset();
asl::format(&sink, "Hello, {}!", "world");
assert(strcmp(sink.cstr(), "Hello, world!") == 0);
sink.reset();
asl::format(&sink, "Hello, {}! {}", "world");
assert(strcmp(sink.cstr(), "Hello, world! <ERROR>") == 0);
sink.reset();
asl::format(&sink, "Hello, pup!", "world");
assert(strcmp(sink.cstr(), "Hello, pup!") == 0);
sink.reset();
asl::format(&sink, "{}", "CHEESE");
assert(strcmp(sink.cstr(), "CHEESE") == 0);
sink.reset();
asl::format(&sink, "{ ", "CHEESE");
assert(strcmp(sink.cstr(), "<ERROR> ") == 0);
sink.reset();
asl::format(&sink, "{", "CHEESE");
assert(strcmp(sink.cstr(), "<ERROR>") == 0);
sink.reset();
asl::format(&sink, "a{{b");
assert(strcmp(sink.cstr(), "a{b") == 0);
sink.reset();
asl::format(&sink, "{{{}}} }", "CHEESE");
assert(strcmp(sink.cstr(), "{CHEESE} }") == 0);
return 0;
}
|