# HG changeset patch # User rubidium # Date 2014-04-24 18:21:32 # Node ID b11f0e5075ec58f8ec8be8bd2a0b3ab108520f29 # Parent 0567743089333eec8bef50d732233af27211483c (svn r26498) -Add: method for duplicating strings with similar behaviours as strecpy and strecat; give end pointer instead of a size diff --git a/src/string.cpp b/src/string.cpp --- a/src/string.cpp +++ b/src/string.cpp @@ -114,6 +114,21 @@ char *strecpy(char *dst, const char *src } /** + * Create a duplicate of the given string. + * @param s The string to duplicate. + * @param last The last character that is safe to duplicate. If NULL, the whole string is duplicated. + * @note The maximum length of the resulting string might therefore be last - s + 1. + * @return The duplicate of the string. + */ +char *stredup(const char *s, const char *last) +{ + size_t len = last == NULL ? strlen(s) : ttd_strnlen(s, last - s + 1); + char *tmp = CallocT(len + 1); + memcpy(tmp, s, len); + return tmp; +} + +/** * Format, "printf", into a newly allocated string. * @param str The formatting string. * @return The formatted string. You must free this! diff --git a/src/string_func.h b/src/string_func.h --- a/src/string_func.h +++ b/src/string_func.h @@ -31,6 +31,7 @@ char *strecat(char *dst, const char *src, const char *last); char *strecpy(char *dst, const char *src, const char *last); +char *stredup(const char *src, const char *last = NULL); int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);