-
Notifications
You must be signed in to change notification settings - Fork 747
/
buffer.c
129 lines (113 loc) · 2.64 KB
/
buffer.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2012 Rui Ueyama. Released under the MIT license.
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "8cc.h"
#define INIT_SIZE 8
Buffer *make_buffer() {
Buffer *r = malloc(sizeof(Buffer));
r->body = malloc(INIT_SIZE);
r->nalloc = INIT_SIZE;
r->len = 0;
return r;
}
static void realloc_body(Buffer *b) {
int newsize = b->nalloc * 2;
char *body = malloc(newsize);
memcpy(body, b->body, b->len);
b->body = body;
b->nalloc = newsize;
}
char *buf_body(Buffer *b) {
return b->body;
}
int buf_len(Buffer *b) {
return b->len;
}
void buf_write(Buffer *b, char c) {
if (b->nalloc == (b->len + 1))
realloc_body(b);
b->body[b->len++] = c;
}
void buf_append(Buffer *b, char *s, int len) {
for (int i = 0; i < len; i++)
buf_write(b, s[i]);
}
void buf_printf(Buffer *b, char *fmt, ...) {
va_list args;
for (;;) {
int avail = b->nalloc - b->len;
va_start(args, fmt);
int written = vsnprintf(b->body + b->len, avail, fmt, args);
va_end(args);
if (avail <= written) {
realloc_body(b);
continue;
}
b->len += written;
return;
}
}
char *vformat(char *fmt, va_list ap) {
Buffer *b = make_buffer();
va_list aq;
for (;;) {
int avail = b->nalloc - b->len;
va_copy(aq, ap);
int written = vsnprintf(b->body + b->len, avail, fmt, aq);
va_end(aq);
if (avail <= written) {
realloc_body(b);
continue;
}
b->len += written;
return buf_body(b);
}
}
char *format(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
char *r = vformat(fmt, ap);
va_end(ap);
return r;
}
static char *quote(char c) {
switch (c) {
case '"': return "\\\"";
case '\\': return "\\\\";
case '\b': return "\\b";
case '\f': return "\\f";
case '\n': return "\\n";
case '\r': return "\\r";
case '\t': return "\\t";
}
return NULL;
}
static void print(Buffer *b, char c) {
char *q = quote(c);
if (q) {
buf_printf(b, "%s", q);
} else if (isprint(c)) {
buf_printf(b, "%c", c);
} else {
buf_printf(b, "\\x%02x", c);
}
}
char *quote_cstring(char *p) {
Buffer *b = make_buffer();
while (*p)
print(b, *p++);
return buf_body(b);
}
char *quote_cstring_len(char *p, int len) {
Buffer *b = make_buffer();
for (int i = 0; i < len; i++)
print(b, p[i]);
return buf_body(b);
}
char *quote_char(char c) {
if (c == '\\') return "\\\\";
if (c == '\'') return "\\'";
return format("%c", c);
}