-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.c
More file actions
110 lines (84 loc) · 2.46 KB
/
Copy pathexamples.c
File metadata and controls
110 lines (84 loc) · 2.46 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
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
// SPDX-License-Identifier: Apache-2.0
#include <stdio.h>
#include <stdlib.h>
#include "fmt.h"
#define BUFF_SZ 64
#define demo_section(name) \
do { \
puts(""); \
} while (0)
#define example_block(stat) \
do { \
size_t len = BUFF_SZ; \
stat; \
} while (0)
fmt_error_t fmt_write_io_impl(const char* buff, size_t write_len)
{
// optional: write buff to IO
fwrite(buff, 1, write_len, stdout);
return FmtError_Ok;
}
int main(void)
{
char buff[BUFF_SZ];
size_t len;
demo_section("io_write");
fmt_write("fmt(\"{{}}\", \"{}\") -> \"{0}\"\n", "Hello");
demo_section("simple");
// step1. set len to max_buff_len
len = BUFF_SZ;
// step2. fmt
fmt_error_t err = fmt_tobuff("{}", buff, &len, 42);
// step3. check error
if (err == FmtError_Ok) {
printf("fmt(\"{}\", 42) -> %s\n", buff);
} else {
printf("fmt(\"{}\", 42) -> error [%d]\n", (int)err);
}
// integer fmt
demo_section("integer");
example_block({
fmt_tobuff("{:d}", buff, &len, 42);
printf("fmt(\"{:d}\", 42) -> %s\n", buff);
});
example_block({
fmt_tobuff("{:h}", buff, &len, 42);
printf("fmt(\"{:h}\", 42) -> %s\n", buff);
});
example_block({
fmt_tobuff("{:x}", buff, &len, 42);
printf("fmt(\"{:x}\", 42) -> %s\n", buff);
});
// align
demo_section("align");
example_block({
fmt_tobuff("{:>8d}", buff, &len, 42);
printf("fmt(\"{:>8d}\", 42) -> |%s|\n", buff);
});
example_block({
fmt_tobuff("{:<8d}", buff, &len, 42);
printf("fmt(\"{:<8d}\", 42) -> |%s|\n", buff);
});
example_block({
fmt_tobuff("{:^8d}", buff, &len, 42);
printf("fmt(\"{:^8d}\", 42) -> |%s|\n", buff);
});
// pointer/string
demo_section("pointer/string");
const char* pstr = "QwQ";
example_block({
fmt_tobuff("{}", buff, &len, pstr);
printf("fmt(\"{}\", \"QwQ\") -> %s\n", buff);
});
example_block({
fmt_tobuff("{:p}", buff, &len, pstr);
printf("fmt(\"{:p}\", \"QwQ\") -> %s\n", buff);
});
// order
demo_section("arguments order");
example_block({
fmt_tobuff("0:{} 1:{} 1:{1} 0:{0}", buff, &len, 42, pstr);
printf("fmt(\"0:{} 1:{} 1:{1} 0:{0}\", 42, \"QwQ\") -> %s\n", buff);
});
return 0;
}