]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/mempool-str.c
Opt: Introduced contexts
[libucw.git] / ucw / mempool-str.c
index 136f5b4cc57a355f2b7d276e1e3527c137ed8f12..9391cbb6eb1db3b8739d74ed364a5a619c7c71bb 100644 (file)
@@ -7,8 +7,8 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/mempool.h"
+#include <ucw/lib.h>
+#include <ucw/mempool.h>
 
 #include <alloca.h>
 #include <string.h>
@@ -16,6 +16,8 @@
 char *
 mp_strdup(struct mempool *p, const char *s)
 {
+  if (!s)
+    return NULL;
   uns l = strlen(s) + 1;
   char *t = mp_alloc_fast_noalign(p, l);
   memcpy(t, s, l);
@@ -83,6 +85,15 @@ mp_strjoin(struct mempool *p, char **a, uns n, uns sep)
   return dest;
 }
 
+char *
+mp_str_from_mem(struct mempool *a, const void *mem, uns len)
+{
+  char *str = mp_alloc_noalign(a, len+1);
+  memcpy(str, mem, len);
+  str[len] = 0;
+  return str;
+}
+
 #ifdef TEST
 
 #include <stdio.h>
@@ -96,6 +107,7 @@ int main(void)
   char *a[] = { "bugs", "gnats", "insects" };
   puts(mp_strjoin(p, a, 3, '.'));
   puts(mp_strjoin(p, a, 3, 0));
+  puts(mp_str_from_mem(p, s+1, 2));
   return 0;
 }