[Bf-blender-cvs] [0baae183754] master: BLI_string_utils: add BLI_string_join_array_by_sep_char

Campbell Barton noreply at git.blender.org
Wed Mar 4 05:27:19 CET 2020


Commit: 0baae1837548f5617c11da4acd8a282400b2bd62
Author: Campbell Barton
Date:   Wed Mar 4 15:12:06 2020 +1100
Branches: master
https://developer.blender.org/rB0baae1837548f5617c11da4acd8a282400b2bd62

BLI_string_utils: add BLI_string_join_array_by_sep_char

Utility to join strings into a fixed size buffer.

===================================================================

M	source/blender/blenlib/BLI_string_utils.h
M	source/blender/blenlib/intern/string_utils.c

===================================================================

diff --git a/source/blender/blenlib/BLI_string_utils.h b/source/blender/blenlib/BLI_string_utils.h
index b3aed438641..273f20315bd 100644
--- a/source/blender/blenlib/BLI_string_utils.h
+++ b/source/blender/blenlib/BLI_string_utils.h
@@ -48,6 +48,11 @@ char *BLI_string_join_array(char *result,
                             size_t result_len,
                             const char *strings[],
                             uint strings_len) ATTR_NONNULL();
+char *BLI_string_join_array_by_sep_char(char *result,
+                                        size_t result_len,
+                                        char sep,
+                                        const char *strings[],
+                                        uint strings_len) ATTR_NONNULL();
 
 char *BLI_string_join_arrayN(const char *strings[], uint strings_len) ATTR_WARN_UNUSED_RESULT
     ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/string_utils.c b/source/blender/blenlib/intern/string_utils.c
index 85cb32b6457..a579327c73e 100644
--- a/source/blender/blenlib/intern/string_utils.c
+++ b/source/blender/blenlib/intern/string_utils.c
@@ -434,6 +434,29 @@ char *BLI_string_join_array(char *result,
   return c;
 }
 
+/**
+ * A version of #BLI_string_join that takes a separator which can be any character including '\0'.
+ */
+char *BLI_string_join_array_by_sep_char(
+    char *result, size_t result_len, char sep, const char *strings[], uint strings_len)
+{
+  char *c = result;
+  char *c_end = &result[result_len - 1];
+  for (uint i = 0; i < strings_len; i++) {
+    if (i != 0) {
+      if (c < c_end) {
+        *c++ = sep;
+      }
+    }
+    const char *p = strings[i];
+    while (*p && (c < c_end)) {
+      *c++ = *p++;
+    }
+  }
+  *c = '\0';
+  return c;
+}
+
 /**
  * Join an array of strings into a newly allocated, null terminated string.
  */



More information about the Bf-blender-cvs mailing list