diff --git a/lib/inc/str.h b/lib/inc/str.h index bab7b66..6408c0f 100644 --- a/lib/inc/str.h +++ b/lib/inc/str.h @@ -31,5 +31,6 @@ ssize_t str_pos (const str_t *str, const char *find); /* find the index of a sub bool str_is_blank (const str_t *str); /* check if a string only consists of whitespace characters. */ void str_downcase (str_t *str); /* transform all uppercase characters in a str_t string to lowercase */ void str_upcase (str_t *str); /* transform all lowercase characters in a str_t string to uppercase */ +void str_clone (const str_t *str, str_t *new); /* clone the contents of a str_t string into a new str_t string. */ #endif // CUTILS_STR_H_ diff --git a/lib/str.c b/lib/str.c index 90dbd24..4416160 100644 --- a/lib/str.c +++ b/lib/str.c @@ -10,7 +10,8 @@ /** * initialize a str_t string and allocte some memory for the data * - * @param str: a pointer to the string to initialize + * @param str: a pointer to the string to initialize. `str` has to be + * caller-allocated. */ void str_new (str_t *str) @@ -25,7 +26,8 @@ str_new (str_t *str) * initialize a str_t string with a given capacity (allocation size). can be * used in oder to reduce the amount of calls to realloc. * - * @param str: a pointer to the string to initialize + * @param str: a pointer to the string to initialize. `str` has to be + * caller-allocated. */ void str_new_cap (str_t *str, size_t want_cap) @@ -43,7 +45,8 @@ str_new_cap (str_t *str, size_t want_cap) * initialize a str_t string with a given data. the data will be copied into * the string. * - * @param str: a pointer to the string to initialize + * @param str: a pointer to the string to initialize. `str` has to be + * caller-allocated. * @param src: the data to put into the string */ inline void @@ -56,7 +59,8 @@ str_new_from (str_t *str, const char *src) * initialize a str_t string with a given data. the data will be copied into * the string. the length of the data is provided as a parameter. * - * @param str: a pointer to the string to initialize + * @param str: a pointer to the string to initialize. `str` has to be + * caller-allocated. * @param src: the data to put into the string * @param len: the length of `src`. undefined behaviour occurs if `len != * strlen (src)` @@ -283,3 +287,15 @@ str_upcase (str_t *str) { strupcase (str->str); } + +/** + * clone the contents of a str_t string into a new str_t string. + * + * @param str: the original str_t string + * @param new: the new str_t string. `new` has to be caller-allocated. + */ +inline void +str_clone (const str_t *str, str_t *new) +{ + str_new_from_len (new, str->str, str->len); +}