2 * UCW Library -- Memory Pools (String Operations)
4 * (c) 2004--2015 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>
12 #include <ucw/unicode.h>
18 mp_strdup(struct mempool *p, const char *s)
22 size_t l = strlen(s) + 1;
23 char *t = mp_alloc_fast_noalign(p, l);
29 mp_memdup(struct mempool *p, const void *s, size_t len)
31 void *t = mp_alloc_fast(p, len);
37 mp_multicat(struct mempool *p, ...)
44 while (x = va_arg(a, char *))
46 size_t *sizes = alloca(cnt * sizeof(*sizes));
51 while (x = va_arg(a, char *))
52 len += sizes[cnt++] = strlen(x);
53 char *buf = mp_alloc_fast_noalign(p, len);
57 while (x = va_arg(args, char *))
59 memcpy(y, x, sizes[cnt]);
68 mp_strjoin(struct mempool *p, char **a, uint n, uint sep)
72 for (uint i=0; i<n; i++)
73 len += sizes[i] = strlen(a[i]);
76 char *dest = mp_alloc_fast_noalign(p, len);
78 for (uint i=0; i<n; i++)
82 memcpy(d, a[i], sizes[i]);
90 mp_str_from_mem(struct mempool *a, const void *mem, size_t len)
92 char *str = mp_alloc_noalign(a, len+1);
93 memcpy(str, mem, len);
98 void *mp_append_utf8_32(struct mempool *pool, void *p, uint c)
100 p = mp_spread(pool, p, UTF8_MAX_LEN);
101 return utf8_32_put(p, c);
110 struct mempool *p = mp_new(64);
111 char *s = mp_strdup(p, "12345");
112 char *c = mp_multicat(p, "<<", s, ">>", NULL);
114 char *a[] = { "bugs", "gnats", "insects" };
115 puts(mp_strjoin(p, a, 3, '.'));
116 puts(mp_strjoin(p, a, 3, 0));
117 puts(mp_str_from_mem(p, s+1, 2));