-
Notifications
You must be signed in to change notification settings - Fork 30
/
strbuf.h
479 lines (430 loc) · 16.1 KB
/
strbuf.h
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* Yash: yet another shell */
/* strbuf.h: modifiable string buffer */
/* (C) 2007-2020 magicant */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef YASH_STRBUF_H
#define YASH_STRBUF_H
#include <stdarg.h>
#include <stdlib.h>
#include <wchar.h>
#define Size_max ((size_t) -1) // = SIZE_MAX
#ifndef XSTRBUF_INITSIZE
#define XSTRBUF_INITSIZE 15
#endif
#ifndef XWCSBUF_INITSIZE
#define XWCSBUF_INITSIZE 15
#endif
typedef struct xstrbuf_T {
char *contents;
size_t length, maxlength;
} xstrbuf_T;
typedef struct xwcsbuf_T {
wchar_t *contents;
size_t length, maxlength;
} xwcsbuf_T;
static inline xstrbuf_T *sb_init(xstrbuf_T *buf)
__attribute__((nonnull));
extern xstrbuf_T *sb_initwith(xstrbuf_T *restrict buf, char *restrict s)
__attribute__((nonnull));
extern xstrbuf_T *sb_initwithmax(xstrbuf_T *buf, size_t max)
__attribute__((nonnull));
static inline void sb_destroy(xstrbuf_T *buf)
__attribute__((nonnull));
static inline char *sb_tostr(xstrbuf_T *buf)
__attribute__((nonnull));
extern xstrbuf_T *sb_setmax(xstrbuf_T *buf, size_t newmax)
__attribute__((nonnull));
extern xstrbuf_T *sb_ensuremax(xstrbuf_T *buf, size_t max)
__attribute__((nonnull));
static inline xstrbuf_T *sb_truncate(xstrbuf_T *buf, size_t newlength)
__attribute__((nonnull));
static inline xstrbuf_T *sb_clear(xstrbuf_T *buf)
__attribute__((nonnull));
extern xstrbuf_T *sb_replace_force(
xstrbuf_T *restrict buf, size_t i, size_t bn,
const char *restrict s, size_t sn)
__attribute__((nonnull));
extern xstrbuf_T *sb_replace(
xstrbuf_T *restrict buf, size_t i, size_t bn,
const char *restrict s, size_t sn)
__attribute__((nonnull));
static inline xstrbuf_T *sb_ninsert_force(
xstrbuf_T *restrict buf, size_t i, const char *restrict s, size_t n)
__attribute__((nonnull));
static inline xstrbuf_T *sb_ninsert(
xstrbuf_T *restrict buf, size_t i, const char *restrict s, size_t n)
__attribute__((nonnull));
static inline xstrbuf_T *sb_insert(
xstrbuf_T *restrict buf, size_t i, const char *restrict s)
__attribute__((nonnull));
static inline xstrbuf_T *sb_ncat_force(
xstrbuf_T *restrict buf, const char *restrict s, size_t n)
__attribute__((nonnull));
static inline xstrbuf_T *sb_ncat(
xstrbuf_T *restrict buf, const char *restrict s, size_t n)
__attribute__((nonnull));
static inline xstrbuf_T *sb_cat(
xstrbuf_T *restrict buf, const char *restrict s)
__attribute__((nonnull));
static inline xstrbuf_T *sb_catfree(
xstrbuf_T *restrict buf, char *restrict s)
__attribute__((nonnull));
static inline xstrbuf_T *sb_remove(xstrbuf_T *buf, size_t i, size_t n)
__attribute__((nonnull));
extern xstrbuf_T *sb_ccat(xstrbuf_T *buf, char c)
__attribute__((nonnull));
extern xstrbuf_T *sb_ccat_repeat(xstrbuf_T *buf, char c, size_t n)
__attribute__((nonnull));
extern _Bool sb_wccat(
xstrbuf_T *restrict buf, wchar_t c, mbstate_t *restrict ps)
__attribute__((nonnull));
extern wchar_t *sb_wcsncat(xstrbuf_T *restrict buf,
const wchar_t *restrict s, size_t n, mbstate_t *restrict ps)
__attribute__((nonnull));
#if HAVE_WCSNRTOMBS
static inline
#else
extern
#endif
wchar_t *sb_wcscat(xstrbuf_T *restrict buf,
const wchar_t *restrict s, mbstate_t *restrict ps)
__attribute__((nonnull));
extern int sb_vprintf(
xstrbuf_T *restrict buf, const char *restrict format, va_list ap)
__attribute__((nonnull(1,2),format(printf,2,0)));
extern int sb_printf(
xstrbuf_T *restrict buf, const char *restrict format, ...)
__attribute__((nonnull(1,2),format(printf,2,3)));
static inline xwcsbuf_T *wb_init(xwcsbuf_T *buf)
__attribute__((nonnull));
extern xwcsbuf_T *wb_initwith(xwcsbuf_T *restrict buf, wchar_t *restrict s)
__attribute__((nonnull));
extern xwcsbuf_T *wb_initwithmax(xwcsbuf_T *buf, size_t max)
__attribute__((nonnull));
static inline void wb_destroy(xwcsbuf_T *buf)
__attribute__((nonnull));
static inline wchar_t *wb_towcs(xwcsbuf_T *buf)
__attribute__((nonnull));
extern xwcsbuf_T *wb_setmax(xwcsbuf_T *buf, size_t newmax)
__attribute__((nonnull));
extern xwcsbuf_T *wb_ensuremax(xwcsbuf_T *buf, size_t max)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_truncate(xwcsbuf_T *buf, size_t newlength)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_clear(xwcsbuf_T *buf)
__attribute__((nonnull));
extern xwcsbuf_T *wb_replace_force(
xwcsbuf_T *restrict buf, size_t i, size_t bn,
const wchar_t *restrict s, size_t sn)
__attribute__((nonnull));
extern xwcsbuf_T *wb_replace(
xwcsbuf_T *restrict buf, size_t i, size_t bn,
const wchar_t *restrict s, size_t sn)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_ninsert_force(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s, size_t n)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_ninsert(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s, size_t n)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_insert(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_ncat_force(
xwcsbuf_T *restrict buf, const wchar_t *restrict s, size_t n)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_ncat(
xwcsbuf_T *restrict buf, const wchar_t *restrict s, size_t n)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_cat(
xwcsbuf_T *restrict buf, const wchar_t *restrict s)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_catfree(
xwcsbuf_T *restrict buf, wchar_t *restrict s)
__attribute__((nonnull));
static inline xwcsbuf_T *wb_remove(xwcsbuf_T *buf, size_t i, size_t n)
__attribute__((nonnull));
extern xwcsbuf_T *wb_wccat(xwcsbuf_T *buf, wchar_t c)
__attribute__((nonnull));
extern char *wb_mbscat(xwcsbuf_T *restrict buf, const char *restrict s)
__attribute__((nonnull));
extern int wb_vwprintf(
xwcsbuf_T *restrict buf, const wchar_t *restrict format, va_list ap)
__attribute__((nonnull(1,2)));
extern int wb_wprintf(
xwcsbuf_T *restrict buf, const wchar_t *restrict format, ...)
__attribute__((nonnull(1,2)));
extern char *malloc_wcsntombs(const wchar_t *s, size_t n)
__attribute__((nonnull,malloc,warn_unused_result));
#if HAVE_WCSNRTOMBS
static inline
#else
extern
#endif
char *malloc_wcstombs(const wchar_t *s)
__attribute__((nonnull,malloc,warn_unused_result));
static inline char *realloc_wcstombs(wchar_t *s)
__attribute__((nonnull,malloc,warn_unused_result));
extern wchar_t *malloc_mbstowcs(const char *s)
__attribute__((nonnull,malloc,warn_unused_result));
static inline wchar_t *realloc_mbstowcs(char *s)
__attribute__((nonnull,malloc,warn_unused_result));
extern char *malloc_vprintf(const char *format, va_list ap)
__attribute__((nonnull(1),malloc,warn_unused_result,format(printf,1,0)));
extern char *malloc_printf(const char *format, ...)
__attribute__((nonnull(1),malloc,warn_unused_result,format(printf,1,2)));
extern wchar_t *malloc_vwprintf(const wchar_t *format, va_list ap)
__attribute__((nonnull(1),malloc,warn_unused_result));
extern wchar_t *malloc_wprintf(const wchar_t *format, ...)
__attribute__((nonnull(1),malloc,warn_unused_result));
extern wchar_t *joinwcsarray(void *const *array, const wchar_t *padding)
__attribute__((malloc,warn_unused_result,nonnull));
/* Initializes the specified string buffer as an empty string. */
xstrbuf_T *sb_init(xstrbuf_T *buf)
{
return sb_initwithmax(buf, XSTRBUF_INITSIZE);
}
/* Frees the specified multibyte string buffer. The contents are lost. */
void sb_destroy(xstrbuf_T *buf)
{
free(sb_tostr(buf));
}
/* Frees the specified multibyte string buffer and returns the contents.
* The caller must `free' the return value. */
char *sb_tostr(xstrbuf_T *buf)
{
char *s = buf->contents;
#ifndef NDEBUG
buf->contents = &s[buf->maxlength];
buf->length = buf->maxlength = Size_max;
#endif
return s;
}
/* Shrinks the length of the buffer to `newlength'.
* `newlength' must not be larger than the current length.
* Characters beyond the new length are lost.
* `maxlength' of the buffer is not changed. */
xstrbuf_T *sb_truncate(xstrbuf_T *buf, size_t newlength)
{
#ifdef assert
assert(newlength <= buf->length);
#endif
buf->contents[buf->length = newlength] = '\0';
return buf;
}
/* Clears the contents of the specified string buffer.
* `maxlength' of the buffer is not changed. */
xstrbuf_T *sb_clear(xstrbuf_T *buf)
{
return sb_truncate(buf, 0);
}
/* Inserts the first `n' bytes of multibyte string `s' at offset `i' in buffer
* `buf'.
* No boundary checks are done and null characters are not considered special.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_ninsert_force(
xstrbuf_T *restrict buf, size_t i, const char *restrict s, size_t n)
{
return sb_replace_force(buf, i, 0, s, n);
}
/* Inserts the first `n' bytes of multibyte string `s' at offset `i' in buffer
* `buf'.
* If (strlen(s) <= n), the whole of `s' is inserted.
* If (buf->length <= i), `s' is appended to the end of the buffer.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_ninsert(
xstrbuf_T *restrict buf, size_t i, const char *restrict s, size_t n)
{
return sb_replace(buf, i, 0, s, n);
}
/* Inserts multibyte string `s' at offset `i' in buffer `buf'.
* If (buf->length <= i), `s' is appended to the end of the buffer.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_insert(xstrbuf_T *restrict buf, size_t i, const char *restrict s)
{
return sb_replace(buf, i, 0, s, Size_max);
}
/* Appends the first `n' bytes of multibyte string `s' to buffer `buf'.
* No boundary checks are done and null characters are not considered special.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_ncat_force(
xstrbuf_T *restrict buf, const char *restrict s, size_t n)
{
return sb_replace_force(buf, buf->length, 0, s, n);
}
/* Appends the first `n' bytes of multibyte string `s' to buffer `buf'.
* If (strlen(s) <= n), the whole of `s' is appended.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_ncat(xstrbuf_T *restrict buf, const char *restrict s, size_t n)
{
return sb_replace(buf, Size_max, 0, s, n);
}
/* Appends multibyte string `s' to buffer `buf'.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_cat(xstrbuf_T *restrict buf, const char *restrict s)
{
return sb_replace(buf, Size_max, 0, s, Size_max);
}
/* Appends multibyte string `s' to buffer `buf' and free the string.
* `s' must not be part of `buf->contents'. */
xstrbuf_T *sb_catfree(xstrbuf_T *restrict buf, char *restrict s)
{
sb_cat(buf, s);
free(s);
return buf;
}
/* Removes `n' bytes at offset `i' in buffer `buf'.
* If (buf->length <= i), `buf' is unchanged.
* If (buf->length <= i + n), `buf' is truncated to `i' bytes. */
xstrbuf_T *sb_remove(xstrbuf_T *buf, size_t i, size_t n)
{
return sb_replace(buf, i, n, "", 0);
}
#if HAVE_WCSNRTOMBS
wchar_t *sb_wcscat(xstrbuf_T *restrict buf,
const wchar_t *restrict s, mbstate_t *restrict ps)
{
return sb_wcsncat(buf, s, Size_max, ps);
}
#endif
/* Initializes the specified wide string buffer as an empty string. */
xwcsbuf_T *wb_init(xwcsbuf_T *buf)
{
return wb_initwithmax(buf, XWCSBUF_INITSIZE);
}
/* Frees the specified wide string buffer. The contents are lost. */
void wb_destroy(xwcsbuf_T *buf)
{
free(wb_towcs(buf));
}
/* Frees the specified wide string buffer and returns the contents.
* The caller must `free' the return value. */
wchar_t *wb_towcs(xwcsbuf_T *buf)
{
wchar_t *s = buf->contents;
#ifndef NDEBUG
buf->contents = &s[buf->maxlength];
buf->length = buf->maxlength = Size_max;
#endif
return s;
}
/* Shrinks the length of the specified buffer to `newlength'.
* `newlength' must not be larger than the current length.
* Characters beyond the new length are lost.
* `maxlength' of the buffer is not changed. */
xwcsbuf_T *wb_truncate(xwcsbuf_T *buf, size_t newlength)
{
#ifdef assert
assert(newlength <= buf->length);
#endif
buf->contents[buf->length = newlength] = L'\0';
return buf;
}
/* Clears the contents of the specified string buffer.
* `maxlength' of the buffer is not changed. */
xwcsbuf_T *wb_clear(xwcsbuf_T *buf)
{
return wb_truncate(buf, 0);
}
/* Inserts the first `n' characters of wide string `s' at offset `i' in buffer
* `buf'.
* No boundary checks are done and null characters are not considered special.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_ninsert_force(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s, size_t n)
{
return wb_replace_force(buf, i, 0, s, n);
}
/* Inserts the first `n' characters of wide string `s` at offset `i' in buffer
* `buf'.
* If (wcslen(s) <= n), the whole of `s' is inserted.
* If (buf->length <= i), `s' is appended to the end of the buffer.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_ninsert(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s, size_t n)
{
return wb_replace(buf, i, 0, s, n);
}
/* Inserts wide string `s' at offset `i' in buffer `buf'.
* If (buf->length <= i), `s' is appended to the end of the buffer.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_insert(
xwcsbuf_T *restrict buf, size_t i, const wchar_t *restrict s)
{
return wb_replace(buf, i, 0, s, Size_max);
}
/* Appends the first `n' characters of wide string `s' to buffer `buf'.
* No boundary checks are done and null characters are not considered special.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_ncat_force(
xwcsbuf_T *restrict buf, const wchar_t *restrict s, size_t n)
{
return wb_replace_force(buf, buf->length, 0, s, n);
}
/* Appends the first `n' characters of wide string `s' to buffer `buf'.
* If (wcslen(s) <= n), the whole of `s' is appended.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_ncat(xwcsbuf_T *restrict buf, const wchar_t *restrict s, size_t n)
{
return wb_replace(buf, Size_max, 0, s, n);
}
/* Appends wide string `s' to buffer `buf'.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_cat(xwcsbuf_T *restrict buf, const wchar_t *restrict s)
{
return wb_replace(buf, Size_max, 0, s, Size_max);
}
/* Appends wide string `s' to buffer and frees the string.
* `s' must not be part of `buf->contents'. */
xwcsbuf_T *wb_catfree(xwcsbuf_T *restrict buf, wchar_t *restrict s)
{
wb_cat(buf, s);
free(s);
return buf;
}
/* Removes `n' characters at offset `i' in buffer `buf'.
* If (buf->length <= i), `buf' is unchanged.
* If (buf->length <= i + n), `buf' is truncated to `i' characters. */
xwcsbuf_T *wb_remove(xwcsbuf_T *buf, size_t i, size_t n)
{
return wb_replace(buf, i, n, L"", 0);
}
#if HAVE_WCSNRTOMBS
char *malloc_wcstombs(const wchar_t *s)
{
return malloc_wcsntombs(s, Size_max);
}
#endif
/* Converts the specified wide string into a newly malloced multibyte string and
* frees the original wide string.
* Returns NULL on error. The wide string is freed anyway.
* The resulting string starts and ends in the initial shift state.*/
char *realloc_wcstombs(wchar_t *s)
{
char *result = malloc_wcstombs(s);
free(s);
return result;
}
/* Converts the specified multibyte string into a newly malloced wide string and
* frees the multibyte string.
* Returns NULL on error. The multibyte string is freed anyway. */
wchar_t *realloc_mbstowcs(char *s)
{
wchar_t *result = malloc_mbstowcs(s);
free(s);
return result;
}
#undef Size_max
#endif /* YASH_STRBUF_H */
/* vim: set ts=8 sts=4 sw=4 et tw=80: */