BLI_string_utils: add utility to join a string into a fixed buffer

This commit is contained in:
Campbell Barton
2020-02-10 01:23:43 +11:00
parent cebd025e02
commit 89ce270f61
2 changed files with 26 additions and 0 deletions

View File

@@ -44,6 +44,11 @@ void BLI_string_split_suffix(const char *string, char *r_body, char *r_suf, cons
void BLI_string_split_prefix(const char *string, char *r_pre, char *r_body, const size_t str_len);
/* Join strings, return newly allocated string. */
char *BLI_string_join_array(char *result,
size_t result_len,
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();
char *BLI_string_join_array_by_sep_charN(char sep,
@@ -54,6 +59,9 @@ char *BLI_string_join_array_by_sep_char_with_tableN(char sep,
const char *strings[],
uint strings_len) ATTR_NONNULL();
/* Take multiple arguments, pass as (array, length). */
#define BLI_string_join(result, result_len, ...) \
BLI_string_join_array( \
result, result_len, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
#define BLI_string_joinN(...) \
BLI_string_join_arrayN(((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
#define BLI_string_join_by_sep_charN(sep, ...) \

View File

@@ -410,12 +410,30 @@ bool BLI_uniquename(
/** \name Join Strings
*
* For non array versions of these functions, use the macros:
* - #BLI_string_join
* - #BLI_string_joinN
* - #BLI_string_join_by_sep_charN
* - #BLI_string_join_by_sep_char_with_tableN
*
* \{ */
char *BLI_string_join_array(char *result,
size_t result_len,
const char *strings[],
uint strings_len)
{
char *c = result;
char *c_end = &result[result_len - 1];
for (uint i = 0; i < strings_len; i++) {
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.
*/