redcarpet_yt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/COPYING +20 -0
- data/Gemfile +9 -0
- data/README.markdown +394 -0
- data/Rakefile +60 -0
- data/bin/redcarpet +7 -0
- data/ext/redcarpet/autolink.c +302 -0
- data/ext/redcarpet/autolink.h +55 -0
- data/ext/redcarpet/buffer.c +203 -0
- data/ext/redcarpet/buffer.h +89 -0
- data/ext/redcarpet/extconf.rb +6 -0
- data/ext/redcarpet/houdini.h +51 -0
- data/ext/redcarpet/houdini_href_e.c +124 -0
- data/ext/redcarpet/houdini_html_e.c +105 -0
- data/ext/redcarpet/html.c +825 -0
- data/ext/redcarpet/html.h +84 -0
- data/ext/redcarpet/html_blocks.h +229 -0
- data/ext/redcarpet/html_smartypants.c +457 -0
- data/ext/redcarpet/markdown.c +2917 -0
- data/ext/redcarpet/markdown.h +143 -0
- data/ext/redcarpet/rc_markdown.c +168 -0
- data/ext/redcarpet/rc_render.c +545 -0
- data/ext/redcarpet/redcarpet.h +52 -0
- data/ext/redcarpet/stack.c +84 -0
- data/ext/redcarpet/stack.h +48 -0
- data/lib/redcarpet/cli.rb +86 -0
- data/lib/redcarpet/compat.rb +73 -0
- data/lib/redcarpet/render_man.rb +65 -0
- data/lib/redcarpet/render_strip.rb +60 -0
- data/lib/redcarpet_yt.rb +103 -0
- data/redcarpet_yt.gemspec +71 -0
- data/test/benchmark.rb +24 -0
- data/test/custom_render_test.rb +28 -0
- data/test/fixtures/benchmark.md +232 -0
- data/test/html5_test.rb +69 -0
- data/test/html_render_test.rb +254 -0
- data/test/html_toc_render_test.rb +75 -0
- data/test/markdown_test.rb +371 -0
- data/test/pathological_inputs_test.rb +34 -0
- data/test/redcarpet_bin_test.rb +80 -0
- data/test/redcarpet_compat_test.rb +38 -0
- data/test/safe_render_test.rb +35 -0
- data/test/smarty_html_test.rb +45 -0
- data/test/smarty_pants_test.rb +53 -0
- data/test/stripdown_render_test.rb +61 -0
- data/test/test_helper.rb +39 -0
- metadata +151 -0
data/bin/redcarpet
ADDED
@@ -0,0 +1,302 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
3
|
+
*
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
9
|
+
* furnished to do so, subject to the following conditions:
|
10
|
+
*
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
12
|
+
* all copies or substantial portions of the Software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
* THE SOFTWARE.
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include "buffer.h"
|
24
|
+
#include "autolink.h"
|
25
|
+
|
26
|
+
#include <string.h>
|
27
|
+
#include <stdlib.h>
|
28
|
+
#include <stdio.h>
|
29
|
+
#include <ctype.h>
|
30
|
+
|
31
|
+
#if defined(_WIN32)
|
32
|
+
#define strncasecmp _strnicmp
|
33
|
+
#endif
|
34
|
+
|
35
|
+
int
|
36
|
+
sd_autolink_issafe(const uint8_t *link, size_t link_len)
|
37
|
+
{
|
38
|
+
static const size_t valid_uris_count = 6;
|
39
|
+
static const char *valid_uris[] = {
|
40
|
+
"#", "/", "http://", "https://", "ftp://", "mailto:"
|
41
|
+
};
|
42
|
+
|
43
|
+
size_t i;
|
44
|
+
|
45
|
+
for (i = 0; i < valid_uris_count; ++i) {
|
46
|
+
size_t len = strlen(valid_uris[i]);
|
47
|
+
|
48
|
+
if (link_len > len &&
|
49
|
+
strncasecmp((char *)link, valid_uris[i], len) == 0 &&
|
50
|
+
isalnum(link[len]))
|
51
|
+
return 1;
|
52
|
+
}
|
53
|
+
|
54
|
+
return 0;
|
55
|
+
}
|
56
|
+
|
57
|
+
static size_t
|
58
|
+
autolink_delim(uint8_t *data, size_t link_end, size_t max_rewind, size_t size)
|
59
|
+
{
|
60
|
+
uint8_t cclose, copen = 0;
|
61
|
+
size_t i;
|
62
|
+
|
63
|
+
for (i = 0; i < link_end; ++i)
|
64
|
+
if (data[i] == '<') {
|
65
|
+
link_end = i;
|
66
|
+
break;
|
67
|
+
}
|
68
|
+
|
69
|
+
while (link_end > 0) {
|
70
|
+
if (strchr("?!.,", data[link_end - 1]) != NULL)
|
71
|
+
link_end--;
|
72
|
+
|
73
|
+
else if (data[link_end - 1] == ';') {
|
74
|
+
size_t new_end = link_end - 2;
|
75
|
+
|
76
|
+
while (new_end > 0 && isalpha(data[new_end]))
|
77
|
+
new_end--;
|
78
|
+
|
79
|
+
if (new_end < link_end - 2 && data[new_end] == '&')
|
80
|
+
link_end = new_end;
|
81
|
+
else
|
82
|
+
link_end--;
|
83
|
+
}
|
84
|
+
else break;
|
85
|
+
}
|
86
|
+
|
87
|
+
if (link_end == 0)
|
88
|
+
return 0;
|
89
|
+
|
90
|
+
cclose = data[link_end - 1];
|
91
|
+
|
92
|
+
switch (cclose) {
|
93
|
+
case '"': copen = '"'; break;
|
94
|
+
case '\'': copen = '\''; break;
|
95
|
+
case ')': copen = '('; break;
|
96
|
+
case ']': copen = '['; break;
|
97
|
+
case '}': copen = '{'; break;
|
98
|
+
}
|
99
|
+
|
100
|
+
if (copen != 0) {
|
101
|
+
size_t closing = 0;
|
102
|
+
size_t opening = 0;
|
103
|
+
size_t i = 0;
|
104
|
+
|
105
|
+
/* Try to close the final punctuation sign in this same line;
|
106
|
+
* if we managed to close it outside of the URL, that means that it's
|
107
|
+
* not part of the URL. If it closes inside the URL, that means it
|
108
|
+
* is part of the URL.
|
109
|
+
*
|
110
|
+
* Examples:
|
111
|
+
*
|
112
|
+
* foo http://www.pokemon.com/Pikachu_(Electric) bar
|
113
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
114
|
+
*
|
115
|
+
* foo (http://www.pokemon.com/Pikachu_(Electric)) bar
|
116
|
+
* => http://www.pokemon.com/Pikachu_(Electric)
|
117
|
+
*
|
118
|
+
* foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
119
|
+
* => http://www.pokemon.com/Pikachu_(Electric))
|
120
|
+
*
|
121
|
+
* (foo http://www.pokemon.com/Pikachu_(Electric)) bar
|
122
|
+
* => foo http://www.pokemon.com/Pikachu_(Electric)
|
123
|
+
*/
|
124
|
+
|
125
|
+
while (i < link_end) {
|
126
|
+
if (data[i] == copen)
|
127
|
+
opening++;
|
128
|
+
else if (data[i] == cclose)
|
129
|
+
closing++;
|
130
|
+
|
131
|
+
i++;
|
132
|
+
}
|
133
|
+
|
134
|
+
if (closing != opening)
|
135
|
+
link_end--;
|
136
|
+
}
|
137
|
+
|
138
|
+
return link_end;
|
139
|
+
}
|
140
|
+
|
141
|
+
static size_t
|
142
|
+
check_domain(uint8_t *data, size_t size, int allow_short)
|
143
|
+
{
|
144
|
+
size_t i, np = 0;
|
145
|
+
|
146
|
+
if (!isalnum(data[0]))
|
147
|
+
return 0;
|
148
|
+
|
149
|
+
for (i = 1; i < size - 1; ++i) {
|
150
|
+
if (strchr(".:", data[i]) != NULL) np++;
|
151
|
+
else if (!isalnum(data[i]) && data[i] != '-') break;
|
152
|
+
}
|
153
|
+
|
154
|
+
if (allow_short) {
|
155
|
+
/* We don't need a valid domain in the strict sense (with
|
156
|
+
* least one dot; so just make sure it's composed of valid
|
157
|
+
* domain characters and return the length of the the valid
|
158
|
+
* sequence. */
|
159
|
+
return i;
|
160
|
+
} else {
|
161
|
+
/* a valid domain needs to have at least a dot.
|
162
|
+
* that's as far as we get */
|
163
|
+
return np ? i : 0;
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
size_t
|
168
|
+
sd_autolink__www(
|
169
|
+
size_t *rewind_p,
|
170
|
+
struct buf *link,
|
171
|
+
uint8_t *data,
|
172
|
+
size_t max_rewind,
|
173
|
+
size_t size,
|
174
|
+
unsigned int flags)
|
175
|
+
{
|
176
|
+
size_t link_end;
|
177
|
+
|
178
|
+
if (max_rewind > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
|
179
|
+
return 0;
|
180
|
+
|
181
|
+
if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
|
182
|
+
return 0;
|
183
|
+
|
184
|
+
link_end = check_domain(data, size, 0);
|
185
|
+
|
186
|
+
if (link_end == 0)
|
187
|
+
return 0;
|
188
|
+
|
189
|
+
while (link_end < size && !isspace(data[link_end]))
|
190
|
+
link_end++;
|
191
|
+
|
192
|
+
link_end = autolink_delim(data, link_end, max_rewind, size);
|
193
|
+
|
194
|
+
if (link_end == 0)
|
195
|
+
return 0;
|
196
|
+
|
197
|
+
bufput(link, data, link_end);
|
198
|
+
*rewind_p = 0;
|
199
|
+
|
200
|
+
return (int)link_end;
|
201
|
+
}
|
202
|
+
|
203
|
+
size_t
|
204
|
+
sd_autolink__email(
|
205
|
+
size_t *rewind_p,
|
206
|
+
struct buf *link,
|
207
|
+
uint8_t *data,
|
208
|
+
size_t max_rewind,
|
209
|
+
size_t size,
|
210
|
+
unsigned int flags)
|
211
|
+
{
|
212
|
+
size_t link_end, rewind;
|
213
|
+
int nb = 0, np = 0;
|
214
|
+
|
215
|
+
for (rewind = 0; rewind < max_rewind; ++rewind) {
|
216
|
+
uint8_t c = data[-rewind - 1];
|
217
|
+
|
218
|
+
if (isalnum(c))
|
219
|
+
continue;
|
220
|
+
|
221
|
+
if (strchr(".+-_", c) != NULL)
|
222
|
+
continue;
|
223
|
+
|
224
|
+
break;
|
225
|
+
}
|
226
|
+
|
227
|
+
if (rewind == 0)
|
228
|
+
return 0;
|
229
|
+
|
230
|
+
for (link_end = 0; link_end < size; ++link_end) {
|
231
|
+
uint8_t c = data[link_end];
|
232
|
+
|
233
|
+
if (isalnum(c))
|
234
|
+
continue;
|
235
|
+
|
236
|
+
if (c == '@')
|
237
|
+
nb++;
|
238
|
+
else if (c == '.' && link_end < size - 1)
|
239
|
+
np++;
|
240
|
+
else if (c != '-' && c != '_')
|
241
|
+
break;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (link_end < 2 || nb != 1 || np == 0)
|
245
|
+
return 0;
|
246
|
+
|
247
|
+
link_end = autolink_delim(data, link_end, max_rewind, size);
|
248
|
+
|
249
|
+
if (link_end == 0)
|
250
|
+
return 0;
|
251
|
+
|
252
|
+
bufput(link, data - rewind, link_end + rewind);
|
253
|
+
*rewind_p = rewind;
|
254
|
+
|
255
|
+
return link_end;
|
256
|
+
}
|
257
|
+
|
258
|
+
size_t
|
259
|
+
sd_autolink__url(
|
260
|
+
size_t *rewind_p,
|
261
|
+
struct buf *link,
|
262
|
+
uint8_t *data,
|
263
|
+
size_t max_rewind,
|
264
|
+
size_t size,
|
265
|
+
unsigned int flags)
|
266
|
+
{
|
267
|
+
size_t link_end, rewind = 0, domain_len;
|
268
|
+
|
269
|
+
if (size < 4 || data[1] != '/' || data[2] != '/')
|
270
|
+
return 0;
|
271
|
+
|
272
|
+
while (rewind < max_rewind && isalpha(data[-rewind - 1]))
|
273
|
+
rewind++;
|
274
|
+
|
275
|
+
if (!sd_autolink_issafe(data - rewind, size + rewind))
|
276
|
+
return 0;
|
277
|
+
|
278
|
+
link_end = strlen("://");
|
279
|
+
|
280
|
+
domain_len = check_domain(
|
281
|
+
data + link_end,
|
282
|
+
size - link_end,
|
283
|
+
flags & SD_AUTOLINK_SHORT_DOMAINS);
|
284
|
+
|
285
|
+
if (domain_len == 0)
|
286
|
+
return 0;
|
287
|
+
|
288
|
+
link_end += domain_len;
|
289
|
+
while (link_end < size && !isspace(data[link_end]))
|
290
|
+
link_end++;
|
291
|
+
|
292
|
+
link_end = autolink_delim(data, link_end, max_rewind, size);
|
293
|
+
|
294
|
+
if (link_end == 0)
|
295
|
+
return 0;
|
296
|
+
|
297
|
+
bufput(link, data - rewind, link_end + rewind);
|
298
|
+
*rewind_p = rewind;
|
299
|
+
|
300
|
+
return link_end;
|
301
|
+
}
|
302
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2015, Vicent Marti
|
3
|
+
*
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
9
|
+
* furnished to do so, subject to the following conditions:
|
10
|
+
*
|
11
|
+
* The above copyright notice and this permission notice shall be included in
|
12
|
+
* all copies or substantial portions of the Software.
|
13
|
+
*
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
* THE SOFTWARE.
|
21
|
+
*/
|
22
|
+
|
23
|
+
#ifndef AUTOLINK_H__
|
24
|
+
#define AUTOLINK_H__
|
25
|
+
|
26
|
+
#include "buffer.h"
|
27
|
+
|
28
|
+
#ifdef __cplusplus
|
29
|
+
extern "C" {
|
30
|
+
#endif
|
31
|
+
|
32
|
+
enum {
|
33
|
+
SD_AUTOLINK_SHORT_DOMAINS = (1 << 0),
|
34
|
+
};
|
35
|
+
|
36
|
+
int
|
37
|
+
sd_autolink_issafe(const uint8_t *link, size_t link_len);
|
38
|
+
|
39
|
+
size_t
|
40
|
+
sd_autolink__www(size_t *rewind_p, struct buf *link,
|
41
|
+
uint8_t *data, size_t offset, size_t size, unsigned int flags);
|
42
|
+
|
43
|
+
size_t
|
44
|
+
sd_autolink__email(size_t *rewind_p, struct buf *link,
|
45
|
+
uint8_t *data, size_t offset, size_t size, unsigned int flags);
|
46
|
+
|
47
|
+
size_t
|
48
|
+
sd_autolink__url(size_t *rewind_p, struct buf *link,
|
49
|
+
uint8_t *data, size_t offset, size_t size, unsigned int flags);
|
50
|
+
|
51
|
+
#ifdef __cplusplus
|
52
|
+
}
|
53
|
+
#endif
|
54
|
+
|
55
|
+
#endif
|
@@ -0,0 +1,203 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2008, Natacha Porté
|
3
|
+
* Copyright (c) 2015, Vicent Marti
|
4
|
+
*
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
10
|
+
* furnished to do so, subject to the following conditions:
|
11
|
+
*
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
13
|
+
* all copies or substantial portions of the Software.
|
14
|
+
*
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
* THE SOFTWARE.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#define BUFFER_MAX_ALLOC_SIZE (1024 * 1024 * 16) //16mb
|
25
|
+
#define __USE_MINGW_ANSI_STDIO 1
|
26
|
+
|
27
|
+
#include "buffer.h"
|
28
|
+
|
29
|
+
#include <stdio.h>
|
30
|
+
#include <stdlib.h>
|
31
|
+
#include <string.h>
|
32
|
+
#include <assert.h>
|
33
|
+
|
34
|
+
/* MSVC compat */
|
35
|
+
#if defined(_MSC_VER)
|
36
|
+
# define _buf_vsnprintf _vsnprintf
|
37
|
+
#else
|
38
|
+
# define _buf_vsnprintf vsnprintf
|
39
|
+
#endif
|
40
|
+
|
41
|
+
int
|
42
|
+
bufprefix(const struct buf *buf, const char *prefix)
|
43
|
+
{
|
44
|
+
size_t i;
|
45
|
+
assert(buf && buf->unit);
|
46
|
+
|
47
|
+
for (i = 0; i < buf->size; ++i) {
|
48
|
+
if (prefix[i] == 0)
|
49
|
+
return 0;
|
50
|
+
|
51
|
+
if (buf->data[i] != prefix[i])
|
52
|
+
return buf->data[i] - prefix[i];
|
53
|
+
}
|
54
|
+
|
55
|
+
return 0;
|
56
|
+
}
|
57
|
+
|
58
|
+
/* bufgrow: increasing the allocated size to the given value */
|
59
|
+
int
|
60
|
+
bufgrow(struct buf *buf, size_t neosz)
|
61
|
+
{
|
62
|
+
size_t neoasz;
|
63
|
+
void *neodata;
|
64
|
+
|
65
|
+
assert(buf && buf->unit);
|
66
|
+
|
67
|
+
if (neosz > BUFFER_MAX_ALLOC_SIZE)
|
68
|
+
return BUF_ENOMEM;
|
69
|
+
|
70
|
+
if (buf->asize >= neosz)
|
71
|
+
return BUF_OK;
|
72
|
+
|
73
|
+
neoasz = buf->asize + buf->unit;
|
74
|
+
while (neoasz < neosz)
|
75
|
+
neoasz += buf->unit;
|
76
|
+
|
77
|
+
neodata = realloc(buf->data, neoasz);
|
78
|
+
if (!neodata)
|
79
|
+
return BUF_ENOMEM;
|
80
|
+
|
81
|
+
buf->data = neodata;
|
82
|
+
buf->asize = neoasz;
|
83
|
+
return BUF_OK;
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
/* bufnew: allocation of a new buffer */
|
88
|
+
struct buf *
|
89
|
+
bufnew(size_t unit)
|
90
|
+
{
|
91
|
+
struct buf *ret;
|
92
|
+
ret = malloc(sizeof (struct buf));
|
93
|
+
|
94
|
+
if (ret) {
|
95
|
+
ret->data = 0;
|
96
|
+
ret->size = ret->asize = 0;
|
97
|
+
ret->unit = unit;
|
98
|
+
}
|
99
|
+
return ret;
|
100
|
+
}
|
101
|
+
|
102
|
+
/* bufnullterm: NULL-termination of the string array */
|
103
|
+
const char *
|
104
|
+
bufcstr(const struct buf *buf)
|
105
|
+
{
|
106
|
+
assert(buf && buf->unit);
|
107
|
+
|
108
|
+
if (buf->size < buf->asize && buf->data[buf->size] == 0)
|
109
|
+
return (char *)buf->data;
|
110
|
+
|
111
|
+
if (buf->size + 1 <= buf->asize || bufgrow(buf, buf->size + 1) == BUF_OK) {
|
112
|
+
buf->data[buf->size] = 0;
|
113
|
+
return (char *)buf->data;
|
114
|
+
}
|
115
|
+
|
116
|
+
return NULL;
|
117
|
+
}
|
118
|
+
|
119
|
+
/* bufprintf: formatted printing to a buffer */
|
120
|
+
void
|
121
|
+
bufprintf(struct buf *buf, const char *fmt, ...)
|
122
|
+
{
|
123
|
+
va_list ap;
|
124
|
+
int n;
|
125
|
+
|
126
|
+
assert(buf && buf->unit);
|
127
|
+
|
128
|
+
if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < BUF_OK)
|
129
|
+
return;
|
130
|
+
|
131
|
+
va_start(ap, fmt);
|
132
|
+
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
|
133
|
+
va_end(ap);
|
134
|
+
|
135
|
+
if (n < 0) {
|
136
|
+
#ifdef _MSC_VER
|
137
|
+
va_start(ap, fmt);
|
138
|
+
n = _vscprintf(fmt, ap);
|
139
|
+
va_end(ap);
|
140
|
+
#else
|
141
|
+
return;
|
142
|
+
#endif
|
143
|
+
}
|
144
|
+
|
145
|
+
if ((size_t)n >= buf->asize - buf->size) {
|
146
|
+
if (bufgrow(buf, buf->size + n + 1) < BUF_OK)
|
147
|
+
return;
|
148
|
+
|
149
|
+
va_start(ap, fmt);
|
150
|
+
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
|
151
|
+
va_end(ap);
|
152
|
+
}
|
153
|
+
|
154
|
+
if (n < 0)
|
155
|
+
return;
|
156
|
+
|
157
|
+
buf->size += n;
|
158
|
+
}
|
159
|
+
|
160
|
+
/* bufput: appends raw data to a buffer */
|
161
|
+
void
|
162
|
+
bufput(struct buf *buf, const void *data, size_t len)
|
163
|
+
{
|
164
|
+
assert(buf && buf->unit);
|
165
|
+
|
166
|
+
if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < BUF_OK)
|
167
|
+
return;
|
168
|
+
|
169
|
+
memcpy(buf->data + buf->size, data, len);
|
170
|
+
buf->size += len;
|
171
|
+
}
|
172
|
+
|
173
|
+
/* bufputs: appends a NUL-terminated string to a buffer */
|
174
|
+
void
|
175
|
+
bufputs(struct buf *buf, const char *str)
|
176
|
+
{
|
177
|
+
bufput(buf, str, strlen(str));
|
178
|
+
}
|
179
|
+
|
180
|
+
|
181
|
+
/* bufputc: appends a single uint8_t to a buffer */
|
182
|
+
void
|
183
|
+
bufputc(struct buf *buf, int c)
|
184
|
+
{
|
185
|
+
assert(buf && buf->unit);
|
186
|
+
|
187
|
+
if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < BUF_OK)
|
188
|
+
return;
|
189
|
+
|
190
|
+
buf->data[buf->size] = c;
|
191
|
+
buf->size += 1;
|
192
|
+
}
|
193
|
+
|
194
|
+
/* bufrelease: decrease the reference count and free the buffer if needed */
|
195
|
+
void
|
196
|
+
bufrelease(struct buf *buf)
|
197
|
+
{
|
198
|
+
if (!buf)
|
199
|
+
return;
|
200
|
+
|
201
|
+
free(buf->data);
|
202
|
+
free(buf);
|
203
|
+
}
|