redcarpet 2.0.0b3 → 2.0.0b4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of redcarpet might be problematic. Click here for more details.

@@ -1,300 +0,0 @@
1
- /* array.c - automatic dynamic array for pointers */
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
- #include "array.h"
20
-
21
- #include <string.h>
22
-
23
-
24
- /***************************
25
- * STATIC HELPER FUNCTIONS *
26
- ***************************/
27
-
28
- /* arr_realloc • realloc memory of a struct array */
29
- static int
30
- arr_realloc(struct array* arr, int neosz) {
31
- void* neo;
32
- neo = realloc(arr->base, neosz * arr->unit);
33
- if (neo == 0) return 0;
34
- arr->base = neo;
35
- arr->asize = neosz;
36
- if (arr->size > neosz) arr->size = neosz;
37
- return 1; }
38
-
39
-
40
- /* parr_realloc • realloc memory of a struct parray */
41
- static int
42
- parr_realloc(struct parray* arr, int neosz) {
43
- void* neo;
44
- neo = realloc(arr->item, neosz * sizeof (void*));
45
- if (neo == 0) return 0;
46
- arr->item = neo;
47
- arr->asize = neosz;
48
- if (arr->size > neosz) arr->size = neosz;
49
- return 1; }
50
-
51
-
52
-
53
- /***************************
54
- * GENERIC ARRAY FUNCTIONS *
55
- ***************************/
56
-
57
- /* arr_adjust • shrink the allocated memory to fit exactly the needs */
58
- int
59
- arr_adjust(struct array *arr) {
60
- return arr_realloc(arr, arr->size); }
61
-
62
-
63
- /* arr_free • frees the structure contents (buf NOT the struct itself) */
64
- void
65
- arr_free(struct array *arr) {
66
- if (!arr) return;
67
- free(arr->base);
68
- arr->base = 0;
69
- arr->size = arr->asize = 0; }
70
-
71
-
72
- /* arr_grow • increases the array size to fit the given number of elements */
73
- int
74
- arr_grow(struct array *arr, int need) {
75
- if (arr->asize >= need) return 1;
76
- else return arr_realloc(arr, need); }
77
-
78
-
79
- /* arr_init • initialization of the contents of the struct */
80
- void
81
- arr_init(struct array *arr, size_t unit) {
82
- arr->base = 0;
83
- arr->size = arr->asize = 0;
84
- arr->unit = unit; }
85
-
86
-
87
- /* arr_insert • inserting nb elements before the nth one */
88
- int
89
- arr_insert(struct array *arr, int nb, int n) {
90
- char *src, *dst;
91
- size_t len;
92
- if (!arr || nb <= 0 || n < 0
93
- || !arr_grow(arr, arr->size + nb))
94
- return 0;
95
- if (n < arr->size) {
96
- src = arr->base;
97
- src += n * arr->unit;
98
- dst = src + nb * arr->unit;
99
- len = (arr->size - n) * arr->unit;
100
- memmove(dst, src, len); }
101
- arr->size += nb;
102
- return 1; }
103
-
104
-
105
- /* arr_item • returns a pointer to the n-th element */
106
- void *
107
- arr_item(struct array *arr, int no) {
108
- char *ptr;
109
- if (!arr || no < 0 || no >= arr->size) return 0;
110
- ptr = arr->base;
111
- ptr += no * arr->unit;
112
- return ptr; }
113
-
114
-
115
- /* arr_newitem • returns the index of a new element appended to the array */
116
- int
117
- arr_newitem(struct array *arr) {
118
- if (!arr_grow(arr, arr->size + 1)) return -1;
119
- arr->size += 1;
120
- return arr->size - 1; }
121
-
122
-
123
- /* arr_remove • removes the n-th elements of the array */
124
- void
125
- arr_remove(struct array *arr, int idx) {
126
- if (!arr || idx < 0 || idx >= arr->size) return;
127
- arr->size -= 1;
128
- if (idx < arr->size) {
129
- char *dst = arr->base;
130
- char *src;
131
- dst += idx * arr->unit;
132
- src = dst + arr->unit;
133
- memmove(dst, src, (arr->size - idx) * arr->unit); } }
134
-
135
-
136
- /* arr_sorted_find • O(log n) search in a sorted array, returning entry */
137
- void *
138
- arr_sorted_find(struct array *arr, void *key, array_cmp_fn cmp) {
139
- int mi, ma, cu, ret;
140
- char *ptr = arr->base;
141
- mi = -1;
142
- ma = arr->size;
143
- while (mi < ma - 1) {
144
- cu = mi + (ma - mi) / 2;
145
- ret = cmp(key, ptr + cu * arr->unit);
146
- if (ret == 0) return ptr + cu * arr->unit;
147
- else if (ret < 0) ma = cu;
148
- else /* if (ret > 0) */ mi = cu; }
149
- return 0; }
150
-
151
-
152
- /* arr_sorted_find_i • O(log n) search in a sorted array,
153
- * returning index of the smallest element larger than the key */
154
- int
155
- arr_sorted_find_i(struct array *arr, void *key, array_cmp_fn cmp) {
156
- int mi, ma, cu, ret;
157
- char *ptr = arr->base;
158
- mi = -1;
159
- ma = arr->size;
160
- while (mi < ma - 1) {
161
- cu = mi + (ma - mi) / 2;
162
- ret = cmp(key, ptr + cu * arr->unit);
163
- if (ret == 0) {
164
- while (cu < arr->size && ret == 0) {
165
- cu += 1;
166
- ret = cmp(key, ptr + cu * arr->unit); }
167
- return cu; }
168
- else if (ret < 0) ma = cu;
169
- else /* if (ret > 0) */ mi = cu; }
170
- return ma; }
171
-
172
-
173
-
174
- /***************************
175
- * POINTER ARRAY FUNCTIONS *
176
- ***************************/
177
-
178
- /* parr_adjust • shrinks the allocated memory to fit exactly the needs */
179
- int
180
- parr_adjust(struct parray* arr) {
181
- return parr_realloc (arr, arr->size); }
182
-
183
-
184
- /* parr_free • frees the structure contents (buf NOT the struct itself) */
185
- void
186
- parr_free(struct parray *arr) {
187
- if (!arr) return;
188
- free (arr->item);
189
- arr->item = 0;
190
- arr->size = 0;
191
- arr->asize = 0; }
192
-
193
-
194
- /* parr_grow • increases the array size to fit the given number of elements */
195
- int
196
- parr_grow(struct parray *arr, int need) {
197
- if (arr->asize >= need) return 1;
198
- else return parr_realloc (arr, need); }
199
-
200
-
201
- /* parr_init • initialization of the struct (which is equivalent to zero) */
202
- void
203
- parr_init(struct parray *arr) {
204
- arr->item = 0;
205
- arr->size = 0;
206
- arr->asize = 0; }
207
-
208
-
209
- /* parr_insert • inserting nb elements before the nth one */
210
- int
211
- parr_insert(struct parray *parr, int nb, int n) {
212
- char *src, *dst;
213
- size_t len, i;
214
- if (!parr || nb <= 0 || n < 0
215
- || !parr_grow(parr, parr->size + nb))
216
- return 0;
217
- if (n < parr->size) {
218
- src = (void *)parr->item;
219
- src += n * sizeof (void *);
220
- dst = src + nb * sizeof (void *);
221
- len = (parr->size - n) * sizeof (void *);
222
- memmove(dst, src, len);
223
- for (i = 0; i < (size_t)nb; ++i)
224
- parr->item[n + i] = 0; }
225
- parr->size += nb;
226
- return 1; }
227
-
228
-
229
- /* parr_pop • pops the last item of the array and returns it */
230
- void *
231
- parr_pop(struct parray *arr) {
232
- if (arr->size <= 0) return 0;
233
- arr->size -= 1;
234
- return arr->item[arr->size]; }
235
-
236
-
237
- /* parr_push • pushes a pointer at the end of the array (= append) */
238
- int
239
- parr_push(struct parray *arr, void *i) {
240
- if (!parr_grow(arr, arr->size + 1)) return 0;
241
- arr->item[arr->size] = i;
242
- arr->size += 1;
243
- return 1; }
244
-
245
-
246
- /* parr_remove • removes the n-th element of the array and returns it */
247
- void *
248
- parr_remove(struct parray *arr, int idx) {
249
- void* ret;
250
- int i;
251
- if (!arr || idx < 0 || idx >= arr->size) return 0;
252
- ret = arr->item[idx];
253
- for (i = idx+1; i < arr->size; ++i)
254
- arr->item[i - 1] = arr->item[i];
255
- arr->size -= 1;
256
- return ret; }
257
-
258
-
259
- /* parr_sorted_find • O(log n) search in a sorted array, returning entry */
260
- void *
261
- parr_sorted_find(struct parray *arr, void *key, array_cmp_fn cmp) {
262
- int mi, ma, cu, ret;
263
- mi = -1;
264
- ma = arr->size;
265
- while (mi < ma - 1) {
266
- cu = mi + (ma - mi) / 2;
267
- ret = cmp(key, arr->item[cu]);
268
- if (ret == 0) return arr->item[cu];
269
- else if (ret < 0) ma = cu;
270
- else /* if (ret > 0) */ mi = cu; }
271
- return 0; }
272
-
273
-
274
- /* parr_sorted_find_i • O(log n) search in a sorted array,
275
- * returning index of the smallest element larger than the key */
276
- int
277
- parr_sorted_find_i(struct parray *arr, void *key, array_cmp_fn cmp) {
278
- int mi, ma, cu, ret;
279
- mi = -1;
280
- ma = arr->size;
281
- while (mi < ma - 1) {
282
- cu = mi + (ma - mi) / 2;
283
- ret = cmp(key, arr->item[cu]);
284
- if (ret == 0) {
285
- while (cu < arr->size && ret == 0) {
286
- cu += 1;
287
- ret = cmp(key, arr->item[cu]); }
288
- return cu; }
289
- else if (ret < 0) ma = cu;
290
- else /* if (ret > 0) */ mi = cu; }
291
- return ma; }
292
-
293
-
294
- /* parr_top • returns the top the stack (i.e. the last element of the array) */
295
- void *
296
- parr_top(struct parray *arr) {
297
- if (arr == 0 || arr->size <= 0) return 0;
298
- else return arr->item[arr->size - 1]; }
299
-
300
- /* vim: set filetype=c: */
@@ -1,147 +0,0 @@
1
- /* array.h - automatic dynamic array for pointers */
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_ARRAY_H
20
- #define LITHIUM_ARRAY_H
21
-
22
- #include <stdlib.h>
23
-
24
- /********************
25
- * TYPE DEFINITIONS *
26
- ********************/
27
-
28
- /* struct array • generic linear array */
29
- struct array {
30
- void* base;
31
- int size;
32
- int asize;
33
- size_t unit; };
34
-
35
-
36
- /* struct parray • array of pointers */
37
- struct parray {
38
- void ** item;
39
- int size;
40
- int asize; };
41
-
42
-
43
- /* array_cmp_fn • comparison functions for sorted arrays */
44
- typedef int (*array_cmp_fn)(void *key, void *array_entry);
45
-
46
-
47
-
48
- /***************************
49
- * GENERIC ARRAY FUNCTIONS *
50
- ***************************/
51
-
52
- /* arr_adjust • shrink the allocated memory to fit exactly the needs */
53
- int
54
- arr_adjust(struct array *);
55
-
56
- /* arr_free • frees the structure contents (buf NOT the struct itself) */
57
- void
58
- arr_free(struct array *);
59
-
60
- /* arr_grow • increases the array size to fit the given number of elements */
61
- int
62
- arr_grow(struct array *, int);
63
-
64
- /* arr_init • initialization of the contents of the struct */
65
- void
66
- arr_init(struct array *, size_t);
67
-
68
- /* arr_insert • inserting elements nb before the nth one */
69
- int
70
- arr_insert(struct array *, int nb, int n);
71
-
72
- /* arr_item • returns a pointer to the n-th element */
73
- void *
74
- arr_item(struct array *, int);
75
-
76
- /* arr_newitem • returns the index of a new element appended to the array */
77
- int
78
- arr_newitem(struct array *);
79
-
80
- /* arr_remove • removes the n-th elements of the array */
81
- void
82
- arr_remove(struct array *, int);
83
-
84
- /* arr_sorted_find • O(log n) search in a sorted array, returning entry */
85
- /* equivalent to bsearch(key, arr->base, arr->size, arr->unit, cmp) */
86
- void *
87
- arr_sorted_find(struct array *, void *key, array_cmp_fn cmp);
88
-
89
- /* arr_sorted_find_i • O(log n) search in a sorted array,
90
- * returning index of the smallest element larger than the key */
91
- int
92
- arr_sorted_find_i(struct array *, void *key, array_cmp_fn cmp);
93
-
94
-
95
- /***************************
96
- * POINTER ARRAY FUNCTIONS *
97
- ***************************/
98
-
99
- /* parr_adjust • shrinks the allocated memory to fit exactly the needs */
100
- int
101
- parr_adjust(struct parray *);
102
-
103
- /* parr_free • frees the structure contents (buf NOT the struct itself) */
104
- void
105
- parr_free(struct parray *);
106
-
107
- /* parr_grow • increases the array size to fit the given number of elements */
108
- int
109
- parr_grow(struct parray *, int);
110
-
111
- /* parr_init • initialization of the struct (which is equivalent to zero) */
112
- void
113
- parr_init(struct parray *);
114
-
115
- /* parr_insert • inserting nb elements before the nth one */
116
- int
117
- parr_insert(struct parray *, int nb, int n);
118
-
119
- /* parr_pop • pops the last item of the array and returns it */
120
- void *
121
- parr_pop(struct parray *);
122
-
123
- /* parr_push • pushes a pointer at the end of the array (= append) */
124
- int
125
- parr_push(struct parray *, void *);
126
-
127
- /* parr_remove • removes the n-th element of the array and returns it */
128
- void *
129
- parr_remove(struct parray *, int);
130
-
131
- /* parr_sorted_find • O(log n) search in a sorted array, returning entry */
132
- void *
133
- parr_sorted_find(struct parray *, void *key, array_cmp_fn cmp);
134
-
135
- /* parr_sorted_find_i • O(log n) search in a sorted array,
136
- * returning index of the smallest element larger than the key */
137
- int
138
- parr_sorted_find_i(struct parray *, void *key, array_cmp_fn cmp);
139
-
140
- /* parr_top • returns the top the stack (i.e. the last element of the array) */
141
- void *
142
- parr_top(struct parray *);
143
-
144
-
145
- #endif /* ndef LITHIUM_ARRAY_H */
146
-
147
- /* vim: set filetype=c: */