summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/string.cpp15
-rw-r--r--src/string_func.h1
2 files changed, 16 insertions, 0 deletions
diff --git a/src/string.cpp b/src/string.cpp
index 6f9e4e2f0..90005071d 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -114,6 +114,21 @@ char *strecpy(char *dst, const char *src, const char *last)
}
/**
+ * 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<char>(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
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);