changeset 680:39c82d877251

Add new th_strelems_t structure for and join/split functions using them to make certain operations nicer.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 04 Mar 2020 19:47:11 +0200
parents 83a48de05fe9
children 81d1a3f53b87
files th_string.c th_string.h
diffstat 2 files changed, 68 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Wed Mar 04 19:45:26 2020 +0200
+++ b/th_string.c	Wed Mar 04 19:47:11 2020 +0200
@@ -556,16 +556,16 @@
 }
 
 
-int th_split_string(const th_char_t *str, th_char_t ***elems, size_t *nelems, const th_char_t *sep)
+int th_split_string_elems(const th_char_t *str, th_strelems_t *ctx, const th_char_t *sep)
 {
     size_t start = 0, end;
     BOOL match = FALSE;
 
-    if (elems == NULL || nelems == NULL)
+    if (str == NULL || ctx == NULL || sep == NULL)
         return THERR_NULLPTR;
 
-    *elems = NULL;
-    *nelems = 0;
+    ctx->elems = NULL;
+    ctx->nelems = 0;
 
     do
     {
@@ -588,11 +588,11 @@
             if (elem == NULL)
                 return THERR_MALLOC;
 
-            if ((*elems = th_realloc(*elems, sizeof(th_char_t **) * (*nelems + 1))) == NULL)
+            if ((ctx->elems = th_realloc(ctx->elems, sizeof(th_char_t **) * (ctx->nelems + 1))) == NULL)
                 return THERR_MALLOC;
 
-            (*elems)[*nelems] = elem;
-            (*nelems)++;
+            ctx->elems[ctx->nelems] = elem;
+            ctx->nelems++;
         }
 
         start = end + 1;
@@ -602,19 +602,37 @@
 }
 
 
-int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep)
+int th_split_string(const th_char_t *str, th_char_t ***elems, size_t *nelems, const th_char_t *sep)
+{
+    th_strelems_t ctx;
+    int res;
+
+    if (elems == NULL || nelems == NULL)
+        return THERR_NULLPTR;
+
+    if ((res = th_split_string_elems(str, &ctx, sep)) == THERR_OK)
+        return res;
+
+    *elems = ctx.elems;
+    *nelems = ctx.nelems;
+
+    return THERR_OK;
+}
+
+
+int th_join_string_elems(th_char_t **str, const th_strelems_t *ctx, const th_char_t *sep)
 {
     size_t len, n, offs, seplen, *elemlens;
 
-    if (elems == NULL || str == NULL || sep == NULL)
+    if (str == NULL || ctx == NULL || sep == NULL)
         return THERR_NULLPTR;
 
-    if ((elemlens = th_malloc(nelems * sizeof(size_t))) == NULL)
+    if ((elemlens = th_malloc(ctx->nelems * sizeof(size_t))) == NULL)
         return THERR_MALLOC;
 
     seplen = th_strlen(sep);
 
-    for (len = n = 0; n < nelems; n++)
+    for (len = n = 0; n < ctx->nelems; n++)
     {
         len += elemlens[n] = th_strlen(ctx->elems[n]);
     }
@@ -627,7 +645,7 @@
         return THERR_MALLOC;
     }
 
-    for (offs = n = 0; n < nelems; n++)
+    for (offs = n = 0; n < ctx->nelems; n++)
     {
         if (n > 0)
         {
@@ -635,7 +653,7 @@
             offs += seplen;
         }
 
-        memcpy((*str) + offs, elems[n], elemlens[n] * sizeof(th_char_t));
+        memcpy((*str) + offs, ctx->elems[n], elemlens[n] * sizeof(th_char_t));
         offs += elemlens[n];
     }
 
@@ -645,6 +663,31 @@
 }
 
 
+int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep)
+{
+    th_strelems_t ctx;
+
+    ctx.elems = elems;
+    ctx.nelems = nelems;
+
+    return th_join_string_elems(str, &ctx, sep);
+}
+
+
+void th_strelems_free(th_strelems_t *ctx)
+{
+    if (ctx != NULL)
+    {
+        for (size_t n = 0; n < ctx->nelems; n++)
+        {
+            th_free(ctx->elems[n]);
+        }
+
+        th_free(ctx->elems);
+    }
+}
+
+
 /* 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	Wed Mar 04 19:45:26 2020 +0200
+++ b/th_string.h	Wed Mar 04 19:47:11 2020 +0200
@@ -50,6 +50,13 @@
 };
 
 
+typedef struct
+{
+    size_t nelems;
+    th_char_t **elems;
+} th_strelems_t;
+
+
 /** @brief
  * Internal *printf() implementation flags
  */
@@ -162,8 +169,11 @@
 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);
+int         th_split_string_elems(const th_char_t *str, th_strelems_t *ctx, const th_char_t *sep);
+int         th_split_string(const th_char_t *str, th_char_t ***elems, size_t *nelems, const th_char_t *sep);
+int         th_join_string_elems(th_char_t **str, const th_strelems_t *ctx, const th_char_t *sep);
+int         th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep);
+void        th_strlems_free(th_strelems_t *ctx);
 
 
 /* Internal printf() implementation. NOTICE! This API may be unstable.