commonmarker 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of commonmarker might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/commonmarker/cmark/CMakeLists.txt +10 -4
- data/ext/commonmarker/cmark/Makefile +5 -5
- data/ext/commonmarker/cmark/api_test/CMakeLists.txt +1 -1
- data/ext/commonmarker/cmark/api_test/main.c +16 -0
- data/ext/commonmarker/cmark/build/CMakeCache.txt +3 -4
- data/ext/commonmarker/cmark/build/CMakeFiles/2.8.10.1/CMakeSystem.cmake +4 -4
- data/ext/commonmarker/cmark/build/CMakeFiles/CMakeError.log +12 -12
- data/ext/commonmarker/cmark/build/CMakeFiles/CMakeOutput.log +97 -142
- data/ext/commonmarker/cmark/build/CMakeFiles/Makefile.cmake +0 -1
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/build.make +1 -1
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/link.txt +1 -1
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/DependInfo.cmake +1 -1
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/build.make +23 -23
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/cmake_clean.cmake +2 -2
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/link.txt +1 -1
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/blocks.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/buffer.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmark.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/commonmark.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_html_u.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/html.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/inlines.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/node.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/references.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/render.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/scanners.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/utf8.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/xml.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/cmake_install.cmake +3 -3
- data/ext/commonmarker/cmark/build/src/cmark_version.h +2 -2
- data/ext/commonmarker/cmark/build/src/config.h +6 -6
- data/ext/commonmarker/cmark/build/src/libcmark.a +0 -0
- data/ext/commonmarker/cmark/build/src/libcmark.pc +1 -1
- data/ext/commonmarker/cmark/build/testdir/CTestTestfile.cmake +4 -4
- data/ext/commonmarker/cmark/changelog.txt +46 -0
- data/ext/commonmarker/cmark/man/man3/cmark.3 +21 -20
- data/ext/commonmarker/cmark/src/CMakeLists.txt +4 -6
- data/ext/commonmarker/cmark/src/bench.h +8 -8
- data/ext/commonmarker/cmark/src/blocks.c +917 -947
- data/ext/commonmarker/cmark/src/buffer.c +213 -288
- data/ext/commonmarker/cmark/src/buffer.h +19 -21
- data/ext/commonmarker/cmark/src/chunk.h +78 -82
- data/ext/commonmarker/cmark/src/cmark.c +9 -17
- data/ext/commonmarker/cmark/src/cmark.h +113 -157
- data/ext/commonmarker/cmark/src/cmark_ctype.c +24 -35
- data/ext/commonmarker/cmark/src/commonmark.c +390 -425
- data/ext/commonmarker/cmark/src/config.h.in +6 -6
- data/ext/commonmarker/cmark/src/houdini.h +21 -15
- data/ext/commonmarker/cmark/src/houdini_href_e.c +50 -57
- data/ext/commonmarker/cmark/src/houdini_html_e.c +36 -51
- data/ext/commonmarker/cmark/src/houdini_html_u.c +119 -124
- data/ext/commonmarker/cmark/src/html.c +289 -307
- data/ext/commonmarker/cmark/src/inlines.c +976 -1030
- data/ext/commonmarker/cmark/src/inlines.h +4 -2
- data/ext/commonmarker/cmark/src/iterator.c +96 -126
- data/ext/commonmarker/cmark/src/iterator.h +5 -5
- data/ext/commonmarker/cmark/src/latex.c +379 -401
- data/ext/commonmarker/cmark/src/main.c +168 -175
- data/ext/commonmarker/cmark/src/man.c +212 -226
- data/ext/commonmarker/cmark/src/node.c +746 -839
- data/ext/commonmarker/cmark/src/node.h +47 -48
- data/ext/commonmarker/cmark/src/parser.h +14 -14
- data/ext/commonmarker/cmark/src/references.c +101 -111
- data/ext/commonmarker/cmark/src/references.h +10 -8
- data/ext/commonmarker/cmark/src/render.c +144 -167
- data/ext/commonmarker/cmark/src/render.h +22 -41
- data/ext/commonmarker/cmark/src/scanners.c +27695 -20903
- data/ext/commonmarker/cmark/src/scanners.h +2 -1
- data/ext/commonmarker/cmark/src/scanners.re +1 -1
- data/ext/commonmarker/cmark/src/utf8.c +276 -419
- data/ext/commonmarker/cmark/src/utf8.h +6 -6
- data/ext/commonmarker/cmark/src/xml.c +129 -144
- data/ext/commonmarker/cmark/test/CMakeLists.txt +4 -4
- data/ext/commonmarker/cmark/test/smart_punct.txt +8 -0
- data/ext/commonmarker/cmark/test/spec.txt +109 -47
- data/lib/commonmarker/version.rb +1 -1
- metadata +2 -2
@@ -16,371 +16,296 @@
|
|
16
16
|
unsigned char cmark_strbuf__initbuf[1];
|
17
17
|
|
18
18
|
#ifndef MIN
|
19
|
-
#define MIN(x,y)
|
19
|
+
#define MIN(x, y) ((x < y) ? x : y)
|
20
20
|
#endif
|
21
21
|
|
22
|
-
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
buf->ptr = cmark_strbuf__initbuf;
|
22
|
+
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
|
23
|
+
buf->asize = 0;
|
24
|
+
buf->size = 0;
|
25
|
+
buf->ptr = cmark_strbuf__initbuf;
|
27
26
|
|
28
|
-
|
29
|
-
|
27
|
+
if (initial_size > 0)
|
28
|
+
cmark_strbuf_grow(buf, initial_size);
|
30
29
|
}
|
31
30
|
|
32
|
-
void cmark_strbuf_overflow_err()
|
33
|
-
|
34
|
-
|
35
|
-
abort();
|
31
|
+
void cmark_strbuf_overflow_err() {
|
32
|
+
fprintf(stderr, "String buffer overflow");
|
33
|
+
abort();
|
36
34
|
}
|
37
35
|
|
38
|
-
static inline void
|
39
|
-
|
40
|
-
{
|
41
|
-
size_t target_size = (size_t)buf->size + add;
|
36
|
+
static inline void S_strbuf_grow_by(cmark_strbuf *buf, size_t add) {
|
37
|
+
size_t target_size = (size_t)buf->size + add;
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
39
|
+
if (target_size < add /* Integer overflow. */
|
40
|
+
|| target_size > BUFSIZE_MAX /* Truncation overflow. */
|
41
|
+
) {
|
42
|
+
cmark_strbuf_overflow_err();
|
43
|
+
return; /* unreachable */
|
44
|
+
}
|
49
45
|
|
50
|
-
|
51
|
-
|
46
|
+
if ((bufsize_t)target_size >= buf->asize)
|
47
|
+
cmark_strbuf_grow(buf, (bufsize_t)target_size);
|
52
48
|
}
|
53
49
|
|
54
|
-
void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size)
|
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
|
-
buf->ptr = new_ptr;
|
50
|
+
void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
|
51
|
+
unsigned char *new_ptr;
|
52
|
+
|
53
|
+
if (target_size < buf->asize)
|
54
|
+
return;
|
55
|
+
|
56
|
+
if (buf->asize == 0) {
|
57
|
+
new_ptr = NULL;
|
58
|
+
} else {
|
59
|
+
new_ptr = buf->ptr;
|
60
|
+
}
|
61
|
+
|
62
|
+
/* Oversize the buffer by 50% to guarantee amortized linear time
|
63
|
+
* complexity on append operations. */
|
64
|
+
size_t new_size = (size_t)target_size + (size_t)target_size / 2;
|
65
|
+
|
66
|
+
/* Account for terminating null byte. */
|
67
|
+
new_size += 1;
|
68
|
+
|
69
|
+
/* round allocation up to multiple of 8 */
|
70
|
+
new_size = (new_size + 7) & ~7;
|
71
|
+
|
72
|
+
if (new_size < (size_t)target_size /* Integer overflow. */
|
73
|
+
|| new_size > BUFSIZE_MAX /* Truncation overflow. */
|
74
|
+
) {
|
75
|
+
if (target_size >= BUFSIZE_MAX) {
|
76
|
+
/* No space for terminating null byte. */
|
77
|
+
cmark_strbuf_overflow_err();
|
78
|
+
return; /* unreachable */
|
79
|
+
}
|
80
|
+
/* Oversize by the maximum possible amount. */
|
81
|
+
new_size = BUFSIZE_MAX;
|
82
|
+
}
|
83
|
+
|
84
|
+
new_ptr = (unsigned char *)realloc(new_ptr, new_size);
|
85
|
+
|
86
|
+
if (!new_ptr) {
|
87
|
+
perror("realloc in cmark_strbuf_grow");
|
88
|
+
abort();
|
89
|
+
}
|
90
|
+
|
91
|
+
buf->asize = (bufsize_t)new_size;
|
92
|
+
buf->ptr = new_ptr;
|
98
93
|
}
|
99
94
|
|
100
|
-
bufsize_t cmark_strbuf_len(const cmark_strbuf *buf)
|
101
|
-
{
|
102
|
-
return buf->size;
|
103
|
-
}
|
95
|
+
bufsize_t cmark_strbuf_len(const cmark_strbuf *buf) { return buf->size; }
|
104
96
|
|
105
|
-
void cmark_strbuf_free(cmark_strbuf *buf)
|
106
|
-
|
107
|
-
|
97
|
+
void cmark_strbuf_free(cmark_strbuf *buf) {
|
98
|
+
if (!buf)
|
99
|
+
return;
|
108
100
|
|
109
|
-
|
110
|
-
|
101
|
+
if (buf->ptr != cmark_strbuf__initbuf)
|
102
|
+
free(buf->ptr);
|
111
103
|
|
112
|
-
|
104
|
+
cmark_strbuf_init(buf, 0);
|
113
105
|
}
|
114
106
|
|
115
|
-
void cmark_strbuf_clear(cmark_strbuf *buf)
|
116
|
-
|
117
|
-
buf->size = 0;
|
107
|
+
void cmark_strbuf_clear(cmark_strbuf *buf) {
|
108
|
+
buf->size = 0;
|
118
109
|
|
119
|
-
|
120
|
-
|
110
|
+
if (buf->asize > 0)
|
111
|
+
buf->ptr[0] = '\0';
|
121
112
|
}
|
122
113
|
|
123
|
-
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
|
124
|
-
{
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
114
|
+
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
|
115
|
+
bufsize_t len) {
|
116
|
+
if (len <= 0 || data == NULL) {
|
117
|
+
cmark_strbuf_clear(buf);
|
118
|
+
} else {
|
119
|
+
if (data != buf->ptr) {
|
120
|
+
if (len >= buf->asize)
|
121
|
+
cmark_strbuf_grow(buf, len);
|
122
|
+
memmove(buf->ptr, data, len);
|
123
|
+
}
|
124
|
+
buf->size = len;
|
125
|
+
buf->ptr[buf->size] = '\0';
|
126
|
+
}
|
136
127
|
}
|
137
128
|
|
138
|
-
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string)
|
139
|
-
|
140
|
-
|
141
|
-
string ? cmark_strbuf_safe_strlen(string) : 0);
|
129
|
+
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string) {
|
130
|
+
cmark_strbuf_set(buf, (const unsigned char *)string,
|
131
|
+
string ? cmark_strbuf_safe_strlen(string) : 0);
|
142
132
|
}
|
143
133
|
|
144
|
-
void cmark_strbuf_putc(cmark_strbuf *buf, int c)
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
buf->ptr[buf->size] = '\0';
|
134
|
+
void cmark_strbuf_putc(cmark_strbuf *buf, int c) {
|
135
|
+
S_strbuf_grow_by(buf, 1);
|
136
|
+
buf->ptr[buf->size++] = (unsigned char)(c & 0xFF);
|
137
|
+
buf->ptr[buf->size] = '\0';
|
149
138
|
}
|
150
139
|
|
151
|
-
void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
|
152
|
-
{
|
153
|
-
|
154
|
-
|
140
|
+
void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
|
141
|
+
bufsize_t len) {
|
142
|
+
if (len <= 0)
|
143
|
+
return;
|
155
144
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
145
|
+
S_strbuf_grow_by(buf, len);
|
146
|
+
memmove(buf->ptr + buf->size, data, len);
|
147
|
+
buf->size += len;
|
148
|
+
buf->ptr[buf->size] = '\0';
|
160
149
|
}
|
161
150
|
|
162
|
-
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string)
|
163
|
-
|
164
|
-
|
165
|
-
cmark_strbuf_safe_strlen(string));
|
151
|
+
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string) {
|
152
|
+
cmark_strbuf_put(buf, (const unsigned char *)string,
|
153
|
+
cmark_strbuf_safe_strlen(string));
|
166
154
|
}
|
167
155
|
|
168
|
-
void
|
169
|
-
{
|
170
|
-
|
171
|
-
if (expected_size <= SIZE_MAX / 2)
|
172
|
-
expected_size *= 2;
|
173
|
-
S_strbuf_grow_by(buf, expected_size);
|
174
|
-
|
175
|
-
while (1) {
|
176
|
-
va_list args;
|
177
|
-
va_copy(args, ap);
|
178
|
-
|
179
|
-
int len = vsnprintf(
|
180
|
-
(char *)buf->ptr + buf->size,
|
181
|
-
buf->asize - buf->size,
|
182
|
-
format, args
|
183
|
-
);
|
184
|
-
#ifndef HAVE_C99_SNPRINTF
|
185
|
-
// Assume we're on Windows.
|
186
|
-
if (len < 0) {
|
187
|
-
len = _vscprintf(format, args);
|
188
|
-
}
|
189
|
-
#endif
|
156
|
+
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
|
157
|
+
const cmark_strbuf *buf) {
|
158
|
+
bufsize_t copylen;
|
190
159
|
|
191
|
-
|
160
|
+
assert(buf);
|
161
|
+
if (!data || datasize <= 0)
|
162
|
+
return;
|
192
163
|
|
193
|
-
|
194
|
-
perror("vsnprintf in cmark_strbuf_vprintf");
|
195
|
-
abort();
|
196
|
-
}
|
164
|
+
data[0] = '\0';
|
197
165
|
|
198
|
-
|
199
|
-
|
200
|
-
break;
|
201
|
-
}
|
166
|
+
if (buf->size == 0 || buf->asize <= 0)
|
167
|
+
return;
|
202
168
|
|
203
|
-
|
204
|
-
|
169
|
+
copylen = buf->size;
|
170
|
+
if (copylen > datasize - 1)
|
171
|
+
copylen = datasize - 1;
|
172
|
+
memmove(data, buf->ptr, copylen);
|
173
|
+
data[copylen] = '\0';
|
205
174
|
}
|
206
175
|
|
207
|
-
void
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
va_start(ap, format);
|
212
|
-
cmark_strbuf_vprintf(buf, format, ap);
|
213
|
-
va_end(ap);
|
176
|
+
void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b) {
|
177
|
+
cmark_strbuf t = *buf_a;
|
178
|
+
*buf_a = *buf_b;
|
179
|
+
*buf_b = t;
|
214
180
|
}
|
215
181
|
|
216
|
-
|
217
|
-
|
218
|
-
bufsize_t copylen;
|
219
|
-
|
220
|
-
assert(buf);
|
221
|
-
if (!data || datasize <= 0)
|
222
|
-
return;
|
223
|
-
|
224
|
-
data[0] = '\0';
|
182
|
+
unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) {
|
183
|
+
unsigned char *data = buf->ptr;
|
225
184
|
|
226
|
-
|
227
|
-
|
185
|
+
if (buf->asize == 0) {
|
186
|
+
/* return an empty string */
|
187
|
+
return (unsigned char *)calloc(1, 1);
|
188
|
+
}
|
228
189
|
|
229
|
-
|
230
|
-
|
231
|
-
copylen = datasize - 1;
|
232
|
-
memmove(data, buf->ptr, copylen);
|
233
|
-
data[copylen] = '\0';
|
190
|
+
cmark_strbuf_init(buf, 0);
|
191
|
+
return data;
|
234
192
|
}
|
235
193
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
*buf_b = t;
|
194
|
+
int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b) {
|
195
|
+
int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size));
|
196
|
+
return (result != 0) ? result
|
197
|
+
: (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
|
241
198
|
}
|
242
199
|
|
243
|
-
|
244
|
-
|
245
|
-
|
200
|
+
bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos) {
|
201
|
+
if (pos >= buf->size)
|
202
|
+
return -1;
|
203
|
+
if (pos < 0)
|
204
|
+
pos = 0;
|
246
205
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
206
|
+
const unsigned char *p =
|
207
|
+
(unsigned char *)memchr(buf->ptr + pos, c, buf->size - pos);
|
208
|
+
if (!p)
|
209
|
+
return -1;
|
251
210
|
|
252
|
-
|
253
|
-
return data;
|
211
|
+
return (bufsize_t)(p - (const unsigned char *)buf->ptr);
|
254
212
|
}
|
255
213
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos)
|
264
|
-
{
|
265
|
-
if (pos >= buf->size)
|
266
|
-
return -1;
|
267
|
-
if (pos < 0)
|
268
|
-
pos = 0;
|
214
|
+
bufsize_t cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, bufsize_t pos) {
|
215
|
+
if (pos < 0 || buf->size == 0)
|
216
|
+
return -1;
|
217
|
+
if (pos >= buf->size)
|
218
|
+
pos = buf->size - 1;
|
269
219
|
|
270
|
-
|
271
|
-
|
272
|
-
|
220
|
+
for (bufsize_t i = pos; i >= 0; i--) {
|
221
|
+
if (buf->ptr[i] == (unsigned char)c)
|
222
|
+
return i;
|
223
|
+
}
|
273
224
|
|
274
|
-
|
225
|
+
return -1;
|
275
226
|
}
|
276
227
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
return -1;
|
281
|
-
if (pos >= buf->size)
|
282
|
-
pos = buf->size - 1;
|
283
|
-
|
284
|
-
for (bufsize_t i = pos; i >= 0; i--) {
|
285
|
-
if (buf->ptr[i] == (unsigned char) c)
|
286
|
-
return i;
|
287
|
-
}
|
228
|
+
void cmark_strbuf_truncate(cmark_strbuf *buf, bufsize_t len) {
|
229
|
+
if (len < 0)
|
230
|
+
len = 0;
|
288
231
|
|
289
|
-
|
232
|
+
if (len < buf->size) {
|
233
|
+
buf->size = len;
|
234
|
+
buf->ptr[buf->size] = '\0';
|
235
|
+
}
|
290
236
|
}
|
291
237
|
|
292
|
-
void
|
293
|
-
{
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
buf->ptr[buf->size] = '\0';
|
300
|
-
}
|
301
|
-
}
|
238
|
+
void cmark_strbuf_drop(cmark_strbuf *buf, bufsize_t n) {
|
239
|
+
if (n > 0) {
|
240
|
+
if (n > buf->size)
|
241
|
+
n = buf->size;
|
242
|
+
buf->size = buf->size - n;
|
243
|
+
if (buf->size)
|
244
|
+
memmove(buf->ptr, buf->ptr + n, buf->size);
|
302
245
|
|
303
|
-
|
304
|
-
|
305
|
-
if (n > 0) {
|
306
|
-
if (n > buf->size)
|
307
|
-
n = buf->size;
|
308
|
-
buf->size = buf->size - n;
|
309
|
-
if (buf->size)
|
310
|
-
memmove(buf->ptr, buf->ptr + n, buf->size);
|
311
|
-
|
312
|
-
buf->ptr[buf->size] = '\0';
|
313
|
-
}
|
246
|
+
buf->ptr[buf->size] = '\0';
|
247
|
+
}
|
314
248
|
}
|
315
249
|
|
316
|
-
void cmark_strbuf_rtrim(cmark_strbuf *buf)
|
317
|
-
|
318
|
-
|
319
|
-
return;
|
250
|
+
void cmark_strbuf_rtrim(cmark_strbuf *buf) {
|
251
|
+
if (!buf->size)
|
252
|
+
return;
|
320
253
|
|
321
|
-
|
322
|
-
|
323
|
-
|
254
|
+
while (buf->size > 0) {
|
255
|
+
if (!cmark_isspace(buf->ptr[buf->size - 1]))
|
256
|
+
break;
|
324
257
|
|
325
|
-
|
326
|
-
|
258
|
+
buf->size--;
|
259
|
+
}
|
327
260
|
|
328
|
-
|
261
|
+
buf->ptr[buf->size] = '\0';
|
329
262
|
}
|
330
263
|
|
331
|
-
void cmark_strbuf_trim(cmark_strbuf *buf)
|
332
|
-
|
333
|
-
bufsize_t i = 0;
|
264
|
+
void cmark_strbuf_trim(cmark_strbuf *buf) {
|
265
|
+
bufsize_t i = 0;
|
334
266
|
|
335
|
-
|
336
|
-
|
267
|
+
if (!buf->size)
|
268
|
+
return;
|
337
269
|
|
338
|
-
|
339
|
-
|
270
|
+
while (i < buf->size && cmark_isspace(buf->ptr[i]))
|
271
|
+
i++;
|
340
272
|
|
341
|
-
|
273
|
+
cmark_strbuf_drop(buf, i);
|
342
274
|
|
343
|
-
|
275
|
+
cmark_strbuf_rtrim(buf);
|
344
276
|
}
|
345
277
|
|
346
278
|
// Destructively modify string, collapsing consecutive
|
347
279
|
// space and newline characters into a single space.
|
348
|
-
void cmark_strbuf_normalize_whitespace(cmark_strbuf *s)
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
s->ptr[w++] = s->ptr[r];
|
366
|
-
last_char_was_space = false;
|
367
|
-
}
|
368
|
-
}
|
369
|
-
|
370
|
-
cmark_strbuf_truncate(s, w);
|
280
|
+
void cmark_strbuf_normalize_whitespace(cmark_strbuf *s) {
|
281
|
+
bool last_char_was_space = false;
|
282
|
+
bufsize_t r, w;
|
283
|
+
|
284
|
+
for (r = 0, w = 0; r < s->size; ++r) {
|
285
|
+
if (cmark_isspace(s->ptr[r])) {
|
286
|
+
if (!last_char_was_space) {
|
287
|
+
s->ptr[w++] = ' ';
|
288
|
+
last_char_was_space = true;
|
289
|
+
}
|
290
|
+
} else {
|
291
|
+
s->ptr[w++] = s->ptr[r];
|
292
|
+
last_char_was_space = false;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
cmark_strbuf_truncate(s, w);
|
371
297
|
}
|
372
298
|
|
373
299
|
// Destructively unescape a string: remove backslashes before punctuation chars.
|
374
|
-
extern void cmark_strbuf_unescape(cmark_strbuf *buf)
|
375
|
-
|
376
|
-
bufsize_t r, w;
|
300
|
+
extern void cmark_strbuf_unescape(cmark_strbuf *buf) {
|
301
|
+
bufsize_t r, w;
|
377
302
|
|
378
|
-
|
379
|
-
|
380
|
-
|
303
|
+
for (r = 0, w = 0; r < buf->size; ++r) {
|
304
|
+
if (buf->ptr[r] == '\\' && cmark_ispunct(buf->ptr[r + 1]))
|
305
|
+
r++;
|
381
306
|
|
382
|
-
|
383
|
-
|
307
|
+
buf->ptr[w++] = buf->ptr[r];
|
308
|
+
}
|
384
309
|
|
385
|
-
|
310
|
+
cmark_strbuf_truncate(buf, w);
|
386
311
|
}
|
@@ -14,13 +14,14 @@ extern "C" {
|
|
14
14
|
typedef int bufsize_t;
|
15
15
|
|
16
16
|
typedef struct {
|
17
|
-
|
18
|
-
|
17
|
+
unsigned char *ptr;
|
18
|
+
bufsize_t asize, size;
|
19
19
|
} cmark_strbuf;
|
20
20
|
|
21
21
|
extern unsigned char cmark_strbuf__initbuf[];
|
22
22
|
|
23
|
-
#define GH_BUF_INIT
|
23
|
+
#define GH_BUF_INIT \
|
24
|
+
{ cmark_strbuf__initbuf, 0, 0 }
|
24
25
|
#define BUFSIZE_MAX INT_MAX
|
25
26
|
|
26
27
|
/**
|
@@ -44,23 +45,22 @@ bufsize_t cmark_strbuf_len(const cmark_strbuf *buf);
|
|
44
45
|
int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b);
|
45
46
|
|
46
47
|
unsigned char *cmark_strbuf_detach(cmark_strbuf *buf);
|
47
|
-
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
|
48
|
+
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
|
49
|
+
const cmark_strbuf *buf);
|
48
50
|
|
49
|
-
static
|
50
|
-
|
51
|
-
return (char *)buf->ptr;
|
51
|
+
static CMARK_INLINE const char *cmark_strbuf_cstr(const cmark_strbuf *buf) {
|
52
|
+
return (char *)buf->ptr;
|
52
53
|
}
|
53
54
|
|
54
55
|
#define cmark_strbuf_at(buf, n) ((buf)->ptr[n])
|
55
56
|
|
56
|
-
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
|
57
|
+
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
|
58
|
+
bufsize_t len);
|
57
59
|
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
|
58
60
|
void cmark_strbuf_putc(cmark_strbuf *buf, int c);
|
59
|
-
void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
|
61
|
+
void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
|
62
|
+
bufsize_t len);
|
60
63
|
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
|
61
|
-
void cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...)
|
62
|
-
CMARK_ATTRIBUTE((format (printf, 2, 3)));
|
63
|
-
void cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap);
|
64
64
|
void cmark_strbuf_clear(cmark_strbuf *buf);
|
65
65
|
|
66
66
|
bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos);
|
@@ -75,17 +75,15 @@ void cmark_strbuf_unescape(cmark_strbuf *s);
|
|
75
75
|
/* Print error and abort. */
|
76
76
|
void cmark_strbuf_overflow_err(void);
|
77
77
|
|
78
|
-
static
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
return (bufsize_t)size;
|
78
|
+
static CMARK_INLINE bufsize_t cmark_strbuf_check_bufsize(size_t size) {
|
79
|
+
if (size > BUFSIZE_MAX) {
|
80
|
+
cmark_strbuf_overflow_err();
|
81
|
+
}
|
82
|
+
return (bufsize_t)size;
|
84
83
|
}
|
85
84
|
|
86
|
-
static
|
87
|
-
|
88
|
-
return cmark_strbuf_check_bufsize(strlen(str));
|
85
|
+
static CMARK_INLINE bufsize_t cmark_strbuf_safe_strlen(const char *str) {
|
86
|
+
return cmark_strbuf_check_bufsize(strlen(str));
|
89
87
|
}
|
90
88
|
|
91
89
|
#ifdef __cplusplus
|