diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-06-29 13:53:03 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-06-29 13:53:03 -0400 |
commit | 174a2027ff0db579e18d5efacb17c3addf6473f6 (patch) | |
tree | 3d9839faac282c4fbc1a9fd5c08d9f74027c4da1 /src/dstring.c | |
parent | 4ad724391daeff5b86ad420f1e1a8b35d65fd7e0 (diff) | |
download | bfs-174a2027ff0db579e18d5efacb17c3addf6473f6.tar.xz |
dstring: Add some exact-size utility functions
Diffstat (limited to 'src/dstring.c')
-rw-r--r-- | src/dstring.c | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/src/dstring.c b/src/dstring.c index f019ba9..dada70b 100644 --- a/src/dstring.c +++ b/src/dstring.c @@ -54,12 +54,18 @@ char *dstralloc(size_t capacity) { } char *dstrdup(const char *str) { - size_t len = strlen(str); - return dstralloc_impl(len, len, str); + return dstrxdup(str, strlen(str)); } char *dstrndup(const char *str, size_t n) { - size_t len = strnlen(str, n); + return dstrxdup(str, strnlen(str, n)); +} + +char *dstrddup(const char *dstr) { + return dstrxdup(dstr, dstrlen(dstr)); +} + +char *dstrxdup(const char *str, size_t len) { return dstralloc_impl(len, len, str); } @@ -98,37 +104,56 @@ int dstresize(char **dstr, size_t length) { struct dstring *header = dstrheader(*dstr); header->length = length; header->data[length] = '\0'; - return 0; } -/** Common implementation of dstr{cat,ncat,app}. */ -static int dstrcat_impl(char **dest, const char *src, size_t srclen) { +int dstrcat(char **dest, const char *src) { + return dstrxcat(dest, src, strlen(src)); +} + +int dstrncat(char **dest, const char *src, size_t n) { + return dstrxcat(dest, src, strnlen(src, n)); +} + +int dstrdcat(char **dest, const char *src) { + return dstrxcat(dest, src, dstrlen(src)); +} + +int dstrxcat(char **dest, const char *src, size_t len) { size_t oldlen = dstrlen(*dest); - size_t newlen = oldlen + srclen; + size_t newlen = oldlen + len; if (dstresize(dest, newlen) != 0) { return -1; } - memcpy(*dest + oldlen, src, srclen); + memcpy(*dest + oldlen, src, len); return 0; } -int dstrcat(char **dest, const char *src) { - return dstrcat_impl(dest, src, strlen(src)); +int dstrapp(char **str, char c) { + return dstrxcat(str, &c, 1); } -int dstrncat(char **dest, const char *src, size_t n) { - return dstrcat_impl(dest, src, strnlen(src, n)); +int dstrcpy(char **dest, const char *src) { + return dstrxcpy(dest, src, strlen(src)); } -int dstrdcat(char **dest, const char *src) { - return dstrcat_impl(dest, src, dstrlen(src)); +int dstrncpy(char **dest, const char *src, size_t n) { + return dstrxcpy(dest, src, strnlen(src, n)); } -int dstrapp(char **str, char c) { - return dstrcat_impl(str, &c, 1); +int dstrdcpy(char **dest, const char *src) { + return dstrxcpy(dest, src, dstrlen(src)); +} + +int dstrxcpy(char **dest, const char *src, size_t len) { + if (dstresize(dest, len) != 0) { + return -1; + } + + memcpy(*dest, src, len); + return 0; } char *dstrprintf(const char *format, ...) { |