From 382ca0941fe53db8d01e145cff12bed91f51aaea Mon Sep 17 00:00:00 2001 From: rubidium Date: Thu, 24 Apr 2014 18:21:32 +0000 Subject: (svn r26498) -Add: method for duplicating strings with similar behaviours as strecpy and strecat; give end pointer instead of a size --- src/string.cpp | 15 +++++++++++++++ src/string_func.h | 1 + 2 files changed, 16 insertions(+) (limited to 'src') diff --git a/src/string.cpp b/src/string.cpp index 6f9e4e2f0..90005071d 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -113,6 +113,21 @@ char *strecpy(char *dst, const char *src, const char *last) return dst; } +/** + * 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. diff --git a/src/string_func.h b/src/string_func.h index df9062cef..8354d67cd 100644 --- 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); -- cgit v1.2.3-54-g00ecf