Changeset - r21399:b11f0e5075ec
[Not reviewed]
master
0 2 0
rubidium - 10 years ago 2014-04-24 18:21:32
rubidium@openttd.org
(svn r26498) -Add: method for duplicating strings with similar behaviours as strecpy and strecat; give end pointer instead of a size
2 files changed with 16 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/string.cpp
Show inline comments
 
@@ -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<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!
src/string_func.h
Show inline comments
 
@@ -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);
 

	
0 comments (0 inline, 0 general)