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)
21 uns l = strlen(s) + 1;
22 char *t = mp_alloc_fast_noalign(p, l);
28 mp_memdup(struct mempool *p, const void *s, uns len)
30 void *t = mp_alloc_fast(p, len);
36 mp_multicat(struct mempool *p, ...)
43 while (x = va_arg(a, char *))
45 uns *sizes = alloca(cnt * sizeof(uns));
50 while (x = va_arg(a, char *))
51 len += sizes[cnt++] = strlen(x);
52 char *buf = mp_alloc_fast_noalign(p, len);
56 while (x = va_arg(args, char *))
58 memcpy(y, x, sizes[cnt]);
67 mp_strjoin(struct mempool *p, char **a, uns n, uns sep)
71 for (uns i=0; i<n; i++)
72 len += sizes[i] = strlen(a[i]);
75 char *dest = mp_alloc_fast_noalign(p, len);
77 for (uns i=0; i<n; i++)
81 memcpy(d, a[i], sizes[i]);
94 struct mempool *p = mp_new(64);
95 char *s = mp_strdup(p, "12345");
96 char *c = mp_multicat(p, "<<", s, ">>", NULL);
98 char *a[] = { "bugs", "gnats", "insects" };
99 puts(mp_strjoin(p, a, 3, '.'));
100 puts(mp_strjoin(p, a, 3, 0));