changeset 224:128c8cb80205

Fix th_str(n)dup_trim() functions. :S
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 15 Feb 2016 12:01:12 +0200
parents d58b92ef5c03
children b394dfb51bd6
files th_string.c
diffstat 1 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Mon Feb 15 10:46:45 2016 +0200
+++ b/th_string.c	Mon Feb 15 12:01:12 2016 +0200
@@ -69,19 +69,19 @@
 
     // Trim end: find last non-whitespace character
     if (flags & TH_TRIM_END)
-        for (end = len; end > start && th_isspace(src[end]); end--);
+        for (end = len - 1; end > start && th_isspace(src[end]); end--);
     else
         end = len;
 
     // Allocate memory for result
-    len = end - start;
-    if (len == 0)
+    if (src[end] == 0 || th_isspace(src[end]))
         return NULL;
 
+    len = end - start + 1;
     if ((res = th_malloc(len + 1)) == NULL)
         return NULL;
 
-    memcpy(res, src, len);
+    memcpy(res, src + start, len);
     res[len] = 0;
     return res;
 }