changeset 674:dfabc7eef3dd

Add new functions th_split_string() and th_join_string().
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 25 Feb 2020 05:16:42 +0200
parents 1763d9b26a58
children fb4093ad1f7b
files th_string.c th_string.h
diffstat 2 files changed, 89 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Mon Feb 24 16:11:38 2020 +0200
+++ b/th_string.c	Tue Feb 25 05:16:42 2020 +0200
@@ -556,6 +556,92 @@
 }
 
 
+int th_split_string(const th_char_t *str, th_char_t ***elems, size_t *nelems, const th_char_t *sep)
+{
+    size_t start = 0, end;
+    BOOL match = FALSE;
+
+    if (elems == NULL || nelems == NULL)
+        return THERR_NULLPTR;
+
+    *elems = NULL;
+    *nelems = 0;
+
+    do
+    {
+        // Split foremost str element out
+        match = FALSE;
+        for (end = start; str[end] != 0; end++)
+        if (strchr(sep, str[end]) != NULL)
+        {
+            match = TRUE;
+            break;
+        }
+
+        // If the element is there, create it
+        if (str[start] != 0 && end >= start)
+        {
+            th_char_t *elem = th_strndup(str + start, end - start);
+            if (elem == NULL)
+                return THERR_MALLOC;
+
+            if ((*elems = th_realloc(*elems, sizeof(th_char_t **) * (*nelems + 1))) == NULL)
+                return THERR_MALLOC;
+
+            (*elems)[*nelems] = elem;
+            (*nelems)++;
+        }
+
+        start = end + 1;
+    } while (match);
+
+    return THERR_OK;
+}
+
+
+int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep)
+{
+    size_t len, n, offs, seplen, *elemlens;
+
+    if (elems == NULL || str == NULL || sep == NULL)
+        return THERR_NULLPTR;
+
+    if ((elemlens = th_malloc(nelems * sizeof(size_t))) == NULL)
+        return THERR_MALLOC;
+
+    seplen = strlen(sep);
+
+    for (len = n = 0; n < nelems; n++)
+    {
+        len += elemlens[n] = strlen(elems[n]);
+    }
+
+    len += 1 + n * seplen;
+
+    if ((*str = th_malloc(len)) == NULL)
+    {
+        th_free(elemlens);
+        return THERR_MALLOC;
+    }
+
+    for (offs = n = 0; n < nelems; n++)
+    {
+        if (n > 0)
+        {
+            memcpy((*str) + offs, sep, seplen);
+            offs += seplen;
+        }
+
+        memcpy((*str) + offs, elems[n], elemlens[n]);
+        offs += elemlens[n];
+    }
+
+    (*str)[offs] = 0;
+
+    return THERR_OK;
+}
+
+
 /* Find next non-whitespace character in string.
  * Updates iPos into the position of such character and
  * returns pointer to the string.
--- a/th_string.h	Mon Feb 24 16:11:38 2020 +0200
+++ b/th_string.h	Tue Feb 25 05:16:42 2020 +0200
@@ -142,6 +142,9 @@
 int         th_pstr_cpy(th_char_t **pdst, const th_char_t *src);
 int         th_pstr_cat(th_char_t **pdst, const th_char_t *src);
 
+int         th_split_string(const char *str, char ***elems, size_t *nelems, const char *sep);
+int         th_join_string(char **str, char **elems, const size_t nelems, const char *sep);
+
 
 /* Internal printf() implementation. NOTICE! This API may be unstable.
  */