changeset 487:702c64a6c570

Add new string helper functions th_strndup_no0() and th_strndup_no0_trim() to make copies of non-NUL terminated strings.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 19 Sep 2019 06:18:50 +0300
parents bf984b494f07
children 8917c39ef6c5
files th_string.c th_string.h
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Thu Sep 19 06:18:18 2019 +0300
+++ b/th_string.c	Thu Sep 19 06:18:50 2019 +0300
@@ -108,6 +108,24 @@
 }
 
 
+char *th_strndup_no0(const char *src, const size_t len)
+{
+    if ((res = th_malloc(len + 1)) == NULL)
+        return NULL;
+
+    memcpy(res, src, len);
+    res[len] = 0;
+
+    return res;
+}
+
+
+char *th_strndup_no0_trim(const char *src, const size_t len, const int flags)
+{
+    return th_strdup_trim_do(src, len, flags);
+}
+
+
 //
 // Simple implementations of printf() type functions
 //
--- a/th_string.h	Thu Sep 19 06:18:18 2019 +0300
+++ b/th_string.h	Thu Sep 19 06:18:50 2019 +0300
@@ -95,6 +95,9 @@
 char    *th_strdup_trim(const char *src, const int flags);
 char    *th_strndup_trim(const char *src, const size_t n, const int flags);
 
+char    *th_strndup_no0(const char *src, const size_t len);
+char    *th_strndup_no0_trim(const char *src, const size_t len, const int flags);
+
 int     th_strcasecmp(const char *haystack, const char *needle);
 int     th_strncasecmp(const char *haystack, const char *needle, size_t n);
 char    *th_strrcasecmp(char *haystack, const char *needle);