Commit: 92b2f4d24999d463d20e7a134bff414f0a8a9d77 Author: Campbell Barton Date: Thu Nov 3 14:17:54 2022 +1100 Branches: master https://developer.blender.org/rB92b2f4d24999d463d20e7a134bff414f0a8a9d77
BLI_string: C++ support for string joining macros C++ doesn't support taking the temporary address of a temporary array, use inline functions instead. Also change array joining functions to return the length of the string instead of returning the pointer (matching BLI_path_join). =================================================================== M source/blender/blenlib/BLI_string_utils.h M source/blender/blenlib/intern/string_utils.c M source/blender/editors/space_sequencer/sequencer_draw.c =================================================================== diff --git a/source/blender/blenlib/BLI_string_utils.h b/source/blender/blenlib/BLI_string_utils.h index df82e94ae2e..936e892a9e2 100644 --- a/source/blender/blenlib/BLI_string_utils.h +++ b/source/blender/blenlib/BLI_string_utils.h @@ -47,33 +47,6 @@ void BLI_string_split_suffix(const char *string, char *r_body, char *r_suf, size */ void BLI_string_split_prefix(const char *string, char *r_pre, char *r_body, 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(); -/** - * 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) ATTR_NONNULL(); - -/** - * Join an array of strings into a newly allocated, null terminated string. - */ -char *BLI_string_join_arrayN(const char *strings[], uint strings_len) ATTR_WARN_UNUSED_RESULT - ATTR_NONNULL(); -/** - * A version of #BLI_string_joinN that takes a separator which can be any character including '\0'. - */ -char *BLI_string_join_array_by_sep_charN(char sep, - const char *strings[], - uint strings_len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /** * A version of #BLI_string_join_array_by_sep_charN that takes a table array. * The new location of each string is written into this array. @@ -82,17 +55,7 @@ char *BLI_string_join_array_by_sep_char_with_tableN(char sep, char *table[], 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, ...) \ - BLI_string_join_array_by_sep_charN( \ - sep, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__)) + #define BLI_string_join_by_sep_char_with_tableN(sep, table, ...) \ BLI_string_join_array_by_sep_char_with_tableN( \ sep, table, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__)) @@ -149,6 +112,360 @@ bool BLI_uniquename(struct ListBase *list, int name_offset, size_t name_len); +/* Expand array functions. */ + +/* Intentionally no comma after `_BLI_STRING_ARGS_0` to allow it to be empty. */ +#define _BLI_STRING_ARGS_1 _BLI_STRING_ARGS_0 const char *a +#define _BLI_STRING_ARGS_2 _BLI_STRING_ARGS_1, const char *b +#define _BLI_STRING_ARGS_3 _BLI_STRING_ARGS_2, const char *c +#define _BLI_STRING_ARGS_4 _BLI_STRING_ARGS_3, const char *d +#define _BLI_STRING_ARGS_5 _BLI_STRING_ARGS_4, const char *e +#define _BLI_STRING_ARGS_6 _BLI_STRING_ARGS_5, const char *f +#define _BLI_STRING_ARGS_7 _BLI_STRING_ARGS_6, const char *g +#define _BLI_STRING_ARGS_8 _BLI_STRING_ARGS_7, const char *h +#define _BLI_STRING_ARGS_9 _BLI_STRING_ARGS_8, const char *i +#define _BLI_STRING_ARGS_10 _BLI_STRING_ARGS_9, const char *j + +/* ------------------------------------------------------------------------- */ +/** \name Implement: `BLI_string_join(..)` + * \{ */ + +#define _BLI_STRING_ARGS_0 char *__restrict dst, const size_t dst_len, + +/** + * Join strings, return the length of the resulting string. + */ +size_t BLI_string_join_array(char *result, + size_t result_len, + const char *strings[], + uint strings_len) ATTR_NONNULL(); + +#define BLI_string_join(...) VA_NARGS_CALL_OVERLOAD(_BLI_string_join_, __VA_ARGS__) + +BLI_INLINE size_t _BLI_string_join_3(_BLI_STRING_ARGS_1) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_4(_BLI_STRING_ARGS_2) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_5(_BLI_STRING_ARGS_3) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_6(_BLI_STRING_ARGS_4) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_7(_BLI_STRING_ARGS_5) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_8(_BLI_STRING_ARGS_6) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_9(_BLI_STRING_ARGS_7) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_10(_BLI_STRING_ARGS_8) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_11(_BLI_STRING_ARGS_9) ATTR_NONNULL(); +BLI_INLINE size_t _BLI_string_join_12(_BLI_STRING_ARGS_10) ATTR_NONNULL(); + +BLI_INLINE size_t _BLI_string_join_3(_BLI_STRING_ARGS_1) +{ + const char *string_array[] = {a}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_4(_BLI_STRING_ARGS_2) +{ + const char *string_array[] = {a, b}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_5(_BLI_STRING_ARGS_3) +{ + const char *string_array[] = {a, b, c}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_6(_BLI_STRING_ARGS_4) +{ + const char *string_array[] = {a, b, c, d}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_7(_BLI_STRING_ARGS_5) +{ + const char *string_array[] = {a, b, c, d, e}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_8(_BLI_STRING_ARGS_6) +{ + const char *string_array[] = {a, b, c, d, e, f}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_9(_BLI_STRING_ARGS_7) +{ + const char *string_array[] = {a, b, c, d, e, f, g}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_10(_BLI_STRING_ARGS_8) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_11(_BLI_STRING_ARGS_9) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h, i}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE size_t _BLI_string_join_12(_BLI_STRING_ARGS_10) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h, i, j}; + return BLI_string_join_array(dst, dst_len, string_array, ARRAY_SIZE(string_array)); +} + +#undef _BLI_STRING_ARGS_0 + +/** \} */ + +/* ------------------------------------------------------------------------- */ +/** \name Implement: `BLI_string_joinN(..)` + * \{ */ + +/** + * Join an array of strings into a newly allocated, null terminated string. + */ +char *BLI_string_join_arrayN(const char *strings[], uint strings_len) ATTR_WARN_UNUSED_RESULT + ATTR_NONNULL(); + +#define BLI_string_joinN(...) VA_NARGS_CALL_OVERLOAD(_BLI_string_joinN_, __VA_ARGS__) + +#define _BLI_STRING_ARGS_0 + +BLI_INLINE char *_BLI_string_joinN_1(_BLI_STRING_ARGS_1) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_2(_BLI_STRING_ARGS_2) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_3(_BLI_STRING_ARGS_3) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_4(_BLI_STRING_ARGS_4) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_5(_BLI_STRING_ARGS_5) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_6(_BLI_STRING_ARGS_6) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_7(_BLI_STRING_ARGS_7) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_8(_BLI_STRING_ARGS_8) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_9(_BLI_STRING_ARGS_9) ATTR_NONNULL(); +BLI_INLINE char *_BLI_string_joinN_10(_BLI_STRING_ARGS_10) ATTR_NONNULL(); + +BLI_INLINE char *_BLI_string_joinN_1(_BLI_STRING_ARGS_1) +{ + const char *string_array[] = {a}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_2(_BLI_STRING_ARGS_2) +{ + const char *string_array[] = {a, b}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_3(_BLI_STRING_ARGS_3) +{ + const char *string_array[] = {a, b, c}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_4(_BLI_STRING_ARGS_4) +{ + const char *string_array[] = {a, b, c, d}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_5(_BLI_STRING_ARGS_5) +{ + const char *string_array[] = {a, b, c, d, e}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_6(_BLI_STRING_ARGS_6) +{ + const char *string_array[] = {a, b, c, d, e, f}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_7(_BLI_STRING_ARGS_7) +{ + const char *string_array[] = {a, b, c, d, e, f, g}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_8(_BLI_STRING_ARGS_8) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_9(_BLI_STRING_ARGS_9) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h, i}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} +BLI_INLINE char *_BLI_string_joinN_10(_BLI_STRING_ARGS_10) +{ + const char *string_array[] = {a, b, c, d, e, f, g, h, i, j}; + return BLI_string_join_arrayN(string_array, ARRAY_SIZE(string_array)); +} + +#undef _BLI_STRING_ARGS_0 + +/** \} */ + +/* @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs