redcarpet 1.0.0 → 2.0.0

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.
data/ext/buffer.c DELETED
@@ -1,318 +0,0 @@
1
- /* buffer.c - automatic buffer structure */
2
-
3
- /*
4
- * Copyright (c) 2008, Natacha Porté
5
- *
6
- * Permission to use, copy, modify, and distribute this software for any
7
- * purpose with or without fee is hereby granted, provided that the above
8
- * copyright notice and this permission notice appear in all copies.
9
- *
10
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
- */
18
-
19
- /*
20
- * COMPILE TIME OPTIONS
21
- *
22
- * BUFFER_STATS • if defined, stats are kept about memory usage
23
- */
24
- #define BUFFER_STATS
25
-
26
- #define BUFFER_STDARG
27
-
28
- #include "buffer.h"
29
-
30
- #include <stdio.h>
31
- #include <stdlib.h>
32
- #include <string.h>
33
-
34
-
35
- /********************
36
- * GLOBAL VARIABLES *
37
- ********************/
38
-
39
- #ifdef BUFFER_STATS
40
- long buffer_stat_nb = 0;
41
- size_t buffer_stat_alloc_bytes = 0;
42
- #endif
43
-
44
-
45
- /***************************
46
- * STATIC HELPER FUNCTIONS *
47
- ***************************/
48
-
49
- /* lower • retruns the lower-case variant of the input char */
50
- static char
51
- lower(char c) {
52
- return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; }
53
-
54
-
55
-
56
- /********************
57
- * BUFFER FUNCTIONS *
58
- ********************/
59
-
60
- /* bufcasecmp • case-insensitive buffer comparison */
61
- int
62
- bufcasecmp(const struct buf *a, const struct buf *b) {
63
- size_t i = 0;
64
- size_t cmplen;
65
- if (a == b) return 0;
66
- if (!a) return -1; else if (!b) return 1;
67
- cmplen = (a->size < b->size) ? a->size : b->size;
68
- while (i < cmplen && lower(a->data[i]) == lower(b->data[i])) ++i;
69
- if (i < a->size) {
70
- if (i < b->size) return lower(a->data[i]) - lower(b->data[i]);
71
- else return 1; }
72
- else { if (i < b->size) return -1;
73
- else return 0; } }
74
-
75
-
76
- /* bufcmp • case-sensitive buffer comparison */
77
- int
78
- bufcmp(const struct buf *a, const struct buf *b) {
79
- size_t i = 0;
80
- size_t cmplen;
81
- if (a == b) return 0;
82
- if (!a) return -1; else if (!b) return 1;
83
- cmplen = (a->size < b->size) ? a->size : b->size;
84
- while (i < cmplen && a->data[i] == b->data[i]) ++i;
85
- if (i < a->size) {
86
- if (i < b->size) return a->data[i] - b->data[i];
87
- else return 1; }
88
- else { if (i < b->size) return -1;
89
- else return 0; } }
90
-
91
-
92
- /* bufcmps • case-sensitive comparison of a string to a buffer */
93
- int
94
- bufcmps(const struct buf *a, const char *b) {
95
- const size_t len = strlen(b);
96
- size_t cmplen = len;
97
- int r;
98
- if (!a || !a->size) return b ? 0 : -1;
99
- if (len < a->size) cmplen = a->size;
100
- r = strncmp(a->data, b, cmplen);
101
- if (r) return r;
102
- else if (a->size == len) return 0;
103
- else if (a->size < len) return -1;
104
- else return 1; }
105
-
106
- int
107
- bufprefix(const struct buf *buf, const char *prefix)
108
- {
109
- size_t i;
110
-
111
- for (i = 0; i < buf->size; ++i) {
112
- if (prefix[i] == 0)
113
- return 0;
114
-
115
- if (buf->data[i] != prefix[i])
116
- return buf->data[i] - prefix[i];
117
- }
118
-
119
- return 0;
120
- }
121
-
122
-
123
- /* bufdup • buffer duplication */
124
- struct buf *
125
- bufdup(const struct buf *src, size_t dupunit) {
126
- size_t blocks;
127
- struct buf *ret;
128
- if (src == 0) return 0;
129
- ret = malloc(sizeof (struct buf));
130
- if (ret == 0) return 0;
131
- ret->unit = dupunit;
132
- ret->size = src->size;
133
- ret->ref = 1;
134
- if (!src->size) {
135
- ret->asize = 0;
136
- ret->data = 0;
137
- return ret; }
138
- blocks = (src->size + dupunit - 1) / dupunit;
139
- ret->asize = blocks * dupunit;
140
- ret->data = malloc(ret->asize);
141
- if (ret->data == 0) {
142
- free(ret);
143
- return 0; }
144
- memcpy(ret->data, src->data, src->size);
145
- #ifdef BUFFER_STATS
146
- buffer_stat_nb += 1;
147
- buffer_stat_alloc_bytes += ret->asize;
148
- #endif
149
- return ret; }
150
-
151
- /* bufgrow • increasing the allocated size to the given value */
152
- int
153
- bufgrow(struct buf *buf, size_t neosz) {
154
- size_t neoasz;
155
- void *neodata;
156
- if (!buf || !buf->unit) return 0;
157
- if (buf->asize >= neosz) return 1;
158
- neoasz = buf->asize + buf->unit;
159
- while (neoasz < neosz) neoasz += buf->unit;
160
- neodata = realloc(buf->data, neoasz);
161
- if (!neodata) return 0;
162
- #ifdef BUFFER_STATS
163
- buffer_stat_alloc_bytes += (neoasz - buf->asize);
164
- #endif
165
- buf->data = neodata;
166
- buf->asize = neoasz;
167
- return 1; }
168
-
169
-
170
- /* bufnew • allocation of a new buffer */
171
- struct buf *
172
- bufnew(size_t unit) {
173
- struct buf *ret;
174
- ret = malloc(sizeof (struct buf));
175
- if (ret) {
176
- #ifdef BUFFER_STATS
177
- buffer_stat_nb += 1;
178
- #endif
179
- ret->data = 0;
180
- ret->size = ret->asize = 0;
181
- ret->ref = 1;
182
- ret->unit = unit; }
183
- return ret; }
184
-
185
-
186
- /* bufnullterm • NUL-termination of the string array (making a C-string) */
187
- void
188
- bufnullterm(struct buf *buf) {
189
- if (!buf || !buf->unit) return;
190
- if (buf->size < buf->asize && buf->data[buf->size] == 0) return;
191
- if (bufgrow(buf, buf->size + 1))
192
- buf->data[buf->size] = 0; }
193
-
194
-
195
- /* bufprintf • formatted printing to a buffer */
196
- void
197
- bufprintf(struct buf *buf, const char *fmt, ...) {
198
- va_list ap;
199
- if (!buf || !buf->unit) return;
200
- va_start(ap, fmt);
201
- vbufprintf(buf, fmt, ap);
202
- va_end(ap); }
203
-
204
-
205
- /* bufput • appends raw data to a buffer */
206
- void
207
- bufput(struct buf *buf, const void *data, size_t len) {
208
- if (!buf || !bufgrow(buf, buf->size + len)) return;
209
- memcpy(buf->data + buf->size, data, len);
210
- buf->size += len; }
211
-
212
-
213
- /* bufputs • appends a NUL-terminated string to a buffer */
214
- void
215
- bufputs(struct buf *buf, const char *str) {
216
- bufput(buf, str, strlen (str)); }
217
-
218
-
219
- /* bufputc • appends a single char to a buffer */
220
- void
221
- bufputc(struct buf *buf, char c) {
222
- if (!buf || !bufgrow(buf, buf->size + 1)) return;
223
- buf->data[buf->size] = c;
224
- buf->size += 1; }
225
-
226
-
227
- /* bufrelease • decrease the reference count and free the buffer if needed */
228
- void
229
- bufrelease(struct buf *buf) {
230
- if (!buf || !buf->unit || !buf->asize) return;
231
- buf->ref -= 1;
232
- if (buf->ref == 0) {
233
- #ifdef BUFFER_STATS
234
- buffer_stat_nb -= 1;
235
- buffer_stat_alloc_bytes -= buf->asize;
236
- #endif
237
- free(buf->data);
238
- free(buf); } }
239
-
240
-
241
- /* bufreset • frees internal data of the buffer */
242
- void
243
- bufreset(struct buf *buf) {
244
- if (!buf || !buf->unit || !buf->asize) return;
245
- #ifdef BUFFER_STATS
246
- buffer_stat_alloc_bytes -= buf->asize;
247
- #endif
248
- free(buf->data);
249
- buf->data = 0;
250
- buf->size = buf->asize = 0; }
251
-
252
-
253
- /* bufset • safely assigns a buffer to another */
254
- void
255
- bufset(struct buf **dest, struct buf *src) {
256
- if (src) {
257
- if (!src->asize) src = bufdup(src, 1);
258
- else src->ref += 1; }
259
- bufrelease(*dest);
260
- *dest = src; }
261
-
262
-
263
- /* bufslurp • removes a given number of bytes from the head of the array */
264
- void
265
- bufslurp(struct buf *buf, size_t len) {
266
- if (!buf || !buf->unit || len <= 0) return;
267
- if (len >= buf->size) {
268
- buf->size = 0;
269
- return; }
270
- buf->size -= len;
271
- memmove(buf->data, buf->data + len, buf->size); }
272
-
273
-
274
- /* buftoi • converts the numbers at the beginning of the buf into an int */
275
- int
276
- buftoi(struct buf *buf, size_t offset_i, size_t *offset_o) {
277
- int r = 0, neg = 0;
278
- size_t i = offset_i;
279
- if (!buf || !buf->size) return 0;
280
- if (buf->data[i] == '+') i += 1;
281
- else if (buf->data[i] == '-') {
282
- neg = 1;
283
- i += 1; }
284
- while (i < buf->size && buf->data[i] >= '0' && buf->data[i] <= '9') {
285
- r = (r * 10) + buf->data[i] - '0';
286
- i += 1; }
287
- if (offset_o) *offset_o = i;
288
- return neg ? -r : r; }
289
-
290
-
291
-
292
- /* vbufprintf • stdarg variant of formatted printing into a buffer */
293
- void
294
- vbufprintf(struct buf *buf, const char *fmt, va_list ap) {
295
- int n;
296
- va_list ap_save;
297
- if (buf == 0
298
- || (buf->size >= buf->asize && !bufgrow (buf, buf->size + 1)))
299
- return;
300
-
301
- va_copy(ap_save, ap);
302
- n = vsnprintf(buf->data + buf->size, buf->asize - buf->size, fmt, ap);
303
-
304
- if (n < 0 || (size_t)n >= buf->asize - buf->size) {
305
- if (!bufgrow (buf, buf->size + n + 1))
306
- return;
307
-
308
- n = vsnprintf (buf->data + buf->size, buf->asize - buf->size, fmt, ap_save);
309
- }
310
- va_end(ap_save);
311
-
312
- if (n < 0)
313
- return;
314
-
315
- buf->size += n;
316
- }
317
-
318
- /* vim: set filetype=c: */
data/ext/buffer.h DELETED
@@ -1,147 +0,0 @@
1
- /* buffer.h - automatic buffer structure */
2
-
3
- /*
4
- * Copyright (c) 2008, Natacha Porté
5
- *
6
- * Permission to use, copy, modify, and distribute this software for any
7
- * purpose with or without fee is hereby granted, provided that the above
8
- * copyright notice and this permission notice appear in all copies.
9
- *
10
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
- */
18
-
19
- #ifndef LITHIUM_BUFFER_H
20
- #define LITHIUM_BUFFER_H
21
-
22
- #include <stddef.h>
23
-
24
-
25
- /********************
26
- * TYPE DEFINITIONS *
27
- ********************/
28
-
29
- /* struct buf • character array buffer */
30
- struct buf {
31
- char * data; /* actual character data */
32
- size_t size; /* size of the string */
33
- size_t asize; /* allocated size (0 = volatile buffer) */
34
- size_t unit; /* reallocation unit size (0 = read-only buffer) */
35
- int ref; }; /* reference count */
36
-
37
-
38
-
39
- /**********
40
- * MACROS *
41
- **********/
42
-
43
- /* CONST_BUF • global buffer from a string litteral */
44
- #define CONST_BUF(name, string) \
45
- static struct buf name = { string, sizeof string -1, sizeof string }
46
-
47
-
48
- /* VOLATILE_BUF • macro for creating a volatile buffer on the stack */
49
- #define VOLATILE_BUF(name, strname) \
50
- struct buf name = { strname, strlen(strname) }
51
-
52
-
53
- /* BUFPUTSL • optimized bufputs of a string litteral */
54
- #define BUFPUTSL(output, litteral) \
55
- bufput(output, litteral, sizeof litteral - 1)
56
-
57
-
58
-
59
- /********************
60
- * BUFFER FUNCTIONS *
61
- ********************/
62
-
63
- /* bufcasecmp • case-insensitive buffer comparison */
64
- int
65
- bufcasecmp(const struct buf *, const struct buf *);
66
-
67
- /* bufcmp • case-sensitive buffer comparison */
68
- int
69
- bufcmp(const struct buf *, const struct buf *);
70
-
71
- /* bufcmps • case-sensitive comparison of a string to a buffer */
72
- int
73
- bufcmps(const struct buf *, const char *);
74
-
75
- /* bufprefix * compare the beggining of a buffer with a string */
76
- int
77
- bufprefix(const struct buf *buf, const char *prefix);
78
-
79
- /* bufdup • buffer duplication */
80
- struct buf *
81
- bufdup(const struct buf *, size_t)
82
- __attribute__ ((malloc));
83
-
84
- /* bufgrow • increasing the allocated size to the given value */
85
- int
86
- bufgrow(struct buf *, size_t);
87
-
88
- /* bufnew • allocation of a new buffer */
89
- struct buf *
90
- bufnew(size_t)
91
- __attribute__ ((malloc));
92
-
93
- /* bufnullterm • NUL-termination of the string array (making a C-string) */
94
- void
95
- bufnullterm(struct buf *);
96
-
97
- /* bufprintf • formatted printing to a buffer */
98
- void
99
- bufprintf(struct buf *, const char *, ...)
100
- __attribute__ ((format (printf, 2, 3)));
101
-
102
- /* bufput • appends raw data to a buffer */
103
- void
104
- bufput(struct buf *, const void*, size_t);
105
-
106
- /* bufputs • appends a NUL-terminated string to a buffer */
107
- void
108
- bufputs(struct buf *, const char*);
109
-
110
- /* bufputc • appends a single char to a buffer */
111
- void
112
- bufputc(struct buf *, char);
113
-
114
- /* bufrelease • decrease the reference count and free the buffer if needed */
115
- void
116
- bufrelease(struct buf *);
117
-
118
- /* bufreset • frees internal data of the buffer */
119
- void
120
- bufreset(struct buf *);
121
-
122
- /* bufset • safely assigns a buffer to another */
123
- void
124
- bufset(struct buf **, struct buf *);
125
-
126
- /* bufslurp • removes a given number of bytes from the head of the array */
127
- void
128
- bufslurp(struct buf *, size_t);
129
-
130
- /* buftoi • converts the numbers at the beginning of the buf into an int */
131
- int
132
- buftoi(struct buf *, size_t, size_t *);
133
-
134
-
135
-
136
- #ifdef BUFFER_STDARG
137
- #include <stdarg.h>
138
-
139
- /* vbufprintf • stdarg variant of formatted printing into a buffer */
140
- void
141
- vbufprintf(struct buf *, const char*, va_list);
142
-
143
- #endif /* def BUFFER_STDARG */
144
-
145
- #endif /* ndef LITHIUM_BUFFER_H */
146
-
147
- /* vim: set filetype=c: */