-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsample.c
114 lines (85 loc) · 3.34 KB
/
sample.c
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
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>
#include "mustache.h"
typedef struct Data {
char* title;
char* body;
} Data;
static mustache_path_resolution capacity_hint(const mustache_userdata_handle user_data_handle, mustache_path* path, uint32_t* out_value) {
Data* data = (Data*)user_data_handle;
if (path->root->next == NULL) {
const mustache_path_part* part = path->root;
if (strncmp(part->value, "title", part->size) == 0) {
*out_value = strlen(data->title);
return FIELD;
} else if (strncmp(part->value, "body", part->size) == 0) {
*out_value = strlen(data->body);
return FIELD;
}
}
return NOT_FOUND_IN_CONTEXT;
}
static mustache_path_resolution interpolate(const mustache_writer_handle writer_handle, mustache_write_fn write_fn, const mustache_userdata_handle user_data_handle, mustache_path* path) {
Data* data = (Data*)user_data_handle;
if (path->root->next == NULL) {
const mustache_path_part* part = path->root;
if (strncmp(part->value, "title", part->size) == 0) {
int status = write_fn(writer_handle, data->title, strlen(data->title));
if (status != SUCCESS) return CHAIN_BROKEN;
return FIELD;
} else if (strncmp(part->value, "body", part->size) == 0) {
int status = write_fn(writer_handle, data->body, strlen(data->body));
if (status != SUCCESS) return CHAIN_BROKEN;
return FIELD;
}
}
return NOT_FOUND_IN_CONTEXT;
}
int main(int argc, char **argv)
{
const char* template_text = "<title>{{title}}</title><h1>{{ title }}</h1><div>{{{body}}}</div>";
mustache_template_handle template;
int status = mustache_create_template(template_text, strlen(template_text) , &template);
if (status != SUCCESS) {
fprintf(stderr, "error: failed to parse the template\n");
return 2;
}
Data data;
data.body = "Hello, Mustache!";
data.title = "This is a really simple test of the rendering!";
mustache_userdata user_data;
user_data.handle = &data;
user_data.capacity_hint = &capacity_hint;
user_data.interpolate = &interpolate;
char* buffer;
uint32_t buffer_len;
status = mustache_render(template, user_data, &buffer, &buffer_len);
if (status != SUCCESS) {
fprintf(stderr, "error: failed to render\n");
return 2;
}
fprintf(stdout, "Rendering this simple template 1 million times\n%s\n\n", buffer);
(void)mustache_free_buffer(buffer, buffer_len);
long total = 0;
clock_t start = clock();
for(int i=0; i<1000000;i++) {
status = mustache_render(template, user_data, &buffer, &buffer_len);
(void)mustache_free_buffer(buffer, buffer_len);
if (status != SUCCESS) return 2;
total += buffer_len;
}
clock_t end = clock();
float elapsed = (float)(end - start)/CLOCKS_PER_SEC;
fprintf(stdout, "C FFI\n");
fprintf(stdout, "Total time %.3f s\n", elapsed);
fprintf(stdout, "%.0f ops/s\n", (float)1000000 / elapsed);
fprintf(stdout, "%d ns/iter\n", (int)(elapsed * 1000));
fprintf(stdout, "%.3f MB/s\n", (float)total / 1024 / 1024 / elapsed);
(void)mustache_free_template(template);
return 0;
}