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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
#include "strvec.h"
#include "dvec.h"
#include "allocator.h"
#include <stddef.h>
#include <string.h>
int
strvec_init(struct strvec *strvec, size_t cap,
const struct allocator *allocator)
{
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!strvec || !allocator);
rc = dvec_init(&strvec->vec, sizeof(char *), cap, allocator);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
return rc;
}
int
strvec_deinit(struct strvec *strvec)
{
int rc;
LIBSTRVEC_ABORT_ON_ALLOC(!strvec);
rc = dvec_deinit(&strvec->vec);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
return rc;
}
struct strvec *
strvec_alloc(size_t cap, const struct allocator *allocator, int *_rc)
{
struct strvec *strvec;
int *rc, stub;
LIBSTRVEC_ABORT_ON_ARGS(!allocator);
rc = _rc ? _rc : &stub;
strvec = allocator->alloc(allocator, sizeof(struct strvec), rc);
if (!strvec && _rc) *rc = -*rc;
if (!strvec) return NULL;
*rc = strvec_init(strvec, cap, allocator);
if (*rc) {
allocator->free(allocator, strvec);
return NULL;
}
return strvec;
}
int
strvec_free(struct strvec *strvec)
{
const struct allocator *allocator;
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!allocator);
allocator = strvec->vec.allocator;
rc = dvec_deinit(&strvec->vec);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
if (rc) return rc;
rc = allocator->free(allocator, strvec);
LIBSTRVEC_ABORT_ON_ALLOC(rc);
if (rc) return -rc;
return 0;
}
int
strvec_copy(struct strvec *dst, struct strvec *src,
const struct allocator *allocator)
{
char **str, *nstr;
size_t len;
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!dst || !src);
dvec_copy(&dst->vec, &src->vec);
for (DVEC_ITER(&dst->vec, str)) {
if (!*str) continue;
len = strlen(*str);
nstr = allocator->alloc(allocator, len + 1, &rc);
LIBSTRVEC_ABORT_ON_ALLOC(!nstr);
if (!nstr) return rc;
strncpy(nstr, *str, len + 1);
*str = nstr;
}
return 0;
}
void
strvec_move(struct strvec *dst, struct strvec *src)
{
LIBSTRVEC_ABORT_ON_ARGS(!dst || !src);
memcpy(dst, src, sizeof(struct strvec));
}
void
strvec_swap(struct strvec *dst, struct strvec *src)
{
struct strvec tmp;
LIBSTRVEC_ABORT_ON_ARGS(!dst || !src);
memcpy(&tmp, dst, sizeof(struct dvec));
memcpy(dst, src, sizeof(struct dvec));
memcpy(src, &tmp, sizeof(struct dvec));
}
int
strvec_reserve(struct strvec *strvec, size_t cap)
{
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
rc = dvec_reserve(&strvec->vec, cap);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
return rc;
}
void
strvec_clear(struct strvec *strvec)
{
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
dvec_clear(&strvec->vec);
}
int
strvec_shrink(struct strvec *strvec)
{
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
rc = dvec_shrink(&strvec->vec);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
return rc;
}
int
strvec_pushn(struct strvec *strvec, const char **str, size_t count)
{
const char **dst;
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!strvec || !str || !count);
rc = dvec_add_back(&strvec->vec, count);
LIBSTRVEC_ABORT_ON_ALLOC(rc < 0);
if (rc) return rc;
dst = dvec_at_back(&strvec->vec, count - 1);
memcpy(dst, str, count * sizeof(char *));
return 0;
}
const char **
strvec_popn(struct strvec *strvec, size_t count)
{
const char **pos;
LIBSTRVEC_ABORT_ON_ARGS(!strvec || !count);
pos = dvec_at_back(&strvec->vec, count);
dvec_rm_back(&strvec->vec, count);
return pos;
}
void
strvec_replace(struct strvec *strvec, size_t index, const char *str)
{
const char **pos;
LIBSTRVEC_ABORT_ON_ARGS(!strvec || index >= dvec_len(&strvec->vec))
pos = dvec_at(&strvec->vec, index);
*pos = str;
}
void
strvec_remove(struct strvec *strvec, size_t index, size_t count)
{
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
dvec_rm(&strvec->vec, index, count);
}
int
strvec_remove_str(struct strvec *strvec, const char *str,
const struct allocator *allocator)
{
const char **ent;
size_t i;
int rc;
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
for (i = 0; i < strvec->vec.len; ) {
ent = strvec_at(strvec, i);
if (!str && !*ent || !strcmp(str, *ent)) {
if (allocator) {
rc = allocator->free(allocator, (char *) *ent);
if (rc) return -rc;
}
strvec_remove(strvec, i, 1);
} else {
i++;
}
}
return 0;
}
ssize_t
strvec_find(struct strvec *strvec, size_t start, const char *str)
{
const char **ent;
size_t i;
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
for (i = start; i < strvec->vec.len; i++) {
ent = strvec_at(strvec, i);
if (!str && !*ent) return (ssize_t) i;
if (!strcmp(str, *ent)) return (ssize_t) i;
}
return -1;
}
char *
strvec_join(struct strvec *strvec, const char *sep,
const struct allocator *allocator, int *_rc)
{
const char **ent;
size_t len, seplen;
char *str;
seplen = strlen(sep);
len = 0;
for (STRVEC_ITER(strvec, ent)) {
if (!*ent) continue;
if (len) len += seplen;
len += strlen(*ent);
}
str = allocator->alloc(allocator, len + 1, _rc);
if (!str && _rc) *_rc = -*_rc;
if (!str) return NULL;
len = 0;
for (STRVEC_ITER(strvec, ent)) {
if (!*ent) continue;
if (len) {
strcpy(str + len, sep);
len += seplen;
}
strcpy(str + len, *ent);
len += strlen(*ent);
}
return str;
}
const char **
strvec_iter_fwd(const struct strvec *strvec, const char **p)
{
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
return dvec_iter_fwd(&strvec->vec, p);
}
const char **
strvec_iter_bwd(const struct strvec *strvec, const char **p)
{
LIBSTRVEC_ABORT_ON_ARGS(!strvec);
return dvec_iter_bwd(&strvec->vec, p);
}
|