diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2016-05-22 13:14:27 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2016-05-22 13:14:27 -0400 |
commit | a36577183430607196e88c2b951f2dc71a06dbaf (patch) | |
tree | c523e4dff632214692dd93a417ee889a7623c9d3 /dstring.c | |
parent | 120fc56d436a4b8791fc82389192a6ca2efdece5 (diff) | |
download | bfs-a36577183430607196e88c2b951f2dc71a06dbaf.tar.xz |
dstring: Clean up the API a bit.
Diffstat (limited to 'dstring.c')
-rw-r--r-- | dstring.c | 23 |
1 files changed, 13 insertions, 10 deletions
@@ -75,23 +75,26 @@ int dstresize(char **dstr, size_t length) { return 0; } -int dstrcat(char **dest, const char *src) { - struct dstring *header = dstrheader(*dest); - return dstrcatat(dest, header->length, src); -} +static int dstrcat_impl(char **dest, const char *src, size_t srclen) { + size_t oldlen = dstrlen(*dest); + size_t newlen = oldlen + srclen; -int dstrcatat(char **dest, size_t pos, const char *src) { - size_t srclen = strlen(src); - size_t destlen = pos + srclen; - - if (dstresize(dest, destlen) != 0) { + if (dstresize(dest, newlen) != 0) { return -1; } - memcpy(*dest + pos, src, srclen); + memcpy(*dest + oldlen, src, srclen); return 0; } +int dstrcat(char **dest, const char *src) { + return dstrcat_impl(dest, src, strlen(src)); +} + +int dstrncat(char **dest, const char *src, size_t n) { + return dstrcat_impl(dest, src, strnlen(src, n)); +} + void dstrfree(char *dstr) { if (dstr) { free(dstrheader(dstr)); |