mirror of https://github.com/OpenTTD/OpenTTD
(svn r26498) -Add: method for duplicating strings with similar behaviours as strecpy and strecat; give end pointer instead of a size
parent
389d61f2fa
commit
382ca0941f
|
@ -113,6 +113,21 @@ char *strecpy(char *dst, const char *src, const char *last)
|
||||||
return dst;
|
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<char>(len + 1);
|
||||||
|
memcpy(tmp, s, len);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format, "printf", into a newly allocated string.
|
* Format, "printf", into a newly allocated string.
|
||||||
* @param str The formatting string.
|
* @param str The formatting string.
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
|
|
||||||
char *strecat(char *dst, const char *src, const char *last);
|
char *strecat(char *dst, const char *src, const char *last);
|
||||||
char *strecpy(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);
|
int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue