Mercurial > hg > th-libs
changeset 220:099a1a156fd7
Implement th_strndup_trim() and factorize implementation of th_strdup_trim().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 15 Feb 2016 10:11:10 +0200 |
parents | faeeac291a6c |
children | 81b4688f684b |
files | th_string.c th_string.h |
diffstat | 2 files changed, 26 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/th_string.c Mon Feb 15 06:05:42 2016 +0200 +++ b/th_string.c Mon Feb 15 10:11:10 2016 +0200 @@ -53,15 +53,14 @@ * according to specified flags. See TH_TRIM_* in * th_string.h */ -char *th_strdup_trim(const char *src, const int flags) +static inline th_strdup_trim_do(const char src, size_t len, const int flags) { char *res; - size_t start, end, len; - if (src == NULL) + size_t start, end; + + if (len == 0) return NULL; - len = strlen(src); - // Trim start: find first non-whitespace character if (flags & TH_TRIM_START) for (start = 0; start < len && th_isspace(src[start]); start++); @@ -85,6 +84,27 @@ } +char *th_strdup_trim(const char *src, const int flags) +{ + if (src == NULL) + return NULL; + + return th_strdup_trim_do(src, strlen(src), flags); +} + + +char *th_strndup_trim(const char *src, const size_t n, const int flags) +{ + size_t len; + if (src == NULL || n == 0) + return NULL; + + for (len = 0; len < n && src[len]; len++); + + return th_strdup_trim_do(src, len, flags); +} + + /* Simulate a sprintf() that allocates memory */ char *th_strdup_vprintf(const char *fmt, va_list args)
--- a/th_string.h Mon Feb 15 06:05:42 2016 +0200 +++ b/th_string.h Mon Feb 15 10:11:10 2016 +0200 @@ -53,6 +53,7 @@ char *th_strdup(const char *src); char *th_strndup(const char *src, const size_t n); char *th_strdup_trim(const char *, const int flags); +char *th_strndup_trim(const char *, const size_t n, const int flags); int th_strcasecmp(const char *haystack, const char *needle); int th_strncasecmp(const char *haystack, const char *needle, size_t n);