2 * UCW Library -- Memory Pools (String Operations)
4 * (c) 2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "ucw/mempool.h"
17 mp_strdup(struct mempool *p, const char *s)
19 uns l = strlen(s) + 1;
20 char *t = mp_alloc_fast_noalign(p, l);
26 mp_memdup(struct mempool *p, const void *s, uns len)
28 void *t = mp_alloc_fast(p, len);
34 mp_multicat(struct mempool *p, ...)
41 while (x = va_arg(a, char *))
43 uns *sizes = alloca(cnt * sizeof(uns));
48 while (x = va_arg(a, char *))
49 len += sizes[cnt++] = strlen(x);
50 char *buf = mp_alloc_fast_noalign(p, len);
54 while (x = va_arg(args, char *))
56 memcpy(y, x, sizes[cnt]);
65 mp_strjoin(struct mempool *p, char **a, uns n, uns sep)
69 for (uns i=0; i<n; i++)
70 len += sizes[i] = strlen(a[i]);
73 char *dest = mp_alloc_fast_noalign(p, len);
75 for (uns i=0; i<n; i++)
79 memcpy(d, a[i], sizes[i]);
92 struct mempool *p = mp_new(64);
93 char *s = mp_strdup(p, "12345");
94 char *c = mp_multicat(p, "<<", s, ">>", NULL);
96 char *a[] = { "bugs", "gnats", "insects" };
97 puts(mp_strjoin(p, a, 3, '.'));
98 puts(mp_strjoin(p, a, 3, 0));