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]);
89 mp_str_from_mem(struct mempool *a, const void *mem, uns len)
91 char *str = mp_alloc_noalign(a, len+1);
92 memcpy(str, mem, len);
103 struct mempool *p = mp_new(64);
104 char *s = mp_strdup(p, "12345");
105 char *c = mp_multicat(p, "<<", s, ">>", NULL);
107 char *a[] = { "bugs", "gnats", "insects" };
108 puts(mp_strjoin(p, a, 3, '.'));
109 puts(mp_strjoin(p, a, 3, 0));
110 puts(mp_str_from_mem(p, s+1, 2));