summaryrefslogtreecommitdiffstats
path: root/dstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'dstring.c')
-rw-r--r--dstring.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/dstring.c b/dstring.c
index d7ce456..8b226bc 100644
--- a/dstring.c
+++ b/dstring.c
@@ -49,7 +49,10 @@ static char *dstralloc_impl(size_t capacity, size_t length, const char *data) {
header->capacity = capacity;
header->length = length;
- return memcpy(header->data, data, length + 1);
+
+ memcpy(header->data, data, length);
+ header->data[length] = '\0';
+ return header->data;
}
char *dstralloc(size_t capacity) {
@@ -61,6 +64,11 @@ char *dstrdup(const char *str) {
return dstralloc_impl(len, len, str);
}
+char *dstrndup(const char *str, size_t n) {
+ size_t len = strnlen(str, n);
+ return dstralloc_impl(len, len, str);
+}
+
size_t dstrlen(const char *dstr) {
return dstrheader(dstr)->length;
}