# HG changeset patch # User Matti Hamalainen # Date 1568863130 -10800 # Node ID 702c64a6c570f65625d5253a9bba3efff53c7679 # Parent bf984b494f071631ce7dd45fbfe71b455a6b1fbd Add new string helper functions th_strndup_no0() and th_strndup_no0_trim() to make copies of non-NUL terminated strings. diff -r bf984b494f07 -r 702c64a6c570 th_string.c --- 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 // diff -r bf984b494f07 -r 702c64a6c570 th_string.h --- 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);