summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-09-14 14:05:46 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-09-14 14:05:46 -0400
commit9bb28ab3be23aa486cff6ce024a88e7b3e343fa4 (patch)
tree6b5f62dd6eb2f476c29af19cff75a57042c400f9 /src
parent170aa3df69a64544ef5fcc24ac22de1aa7303562 (diff)
downloadbfs-9bb28ab3be23aa486cff6ce024a88e7b3e343fa4.tar.xz
dstring: New dstrepeat() function
Diffstat (limited to 'src')
-rw-r--r--src/dstring.c17
-rw-r--r--src/dstring.h5
2 files changed, 22 insertions, 0 deletions
diff --git a/src/dstring.c b/src/dstring.c
index 36ed2c7..af499fd 100644
--- a/src/dstring.c
+++ b/src/dstring.c
@@ -278,3 +278,20 @@ void dstrfree(dchar *dstr) {
free(dstrheader(dstr));
}
}
+
+dchar *dstrepeat(const char *str, size_t n) {
+ size_t len = strlen(str);
+ dchar *ret = dstralloc(n * len);
+ if (!ret) {
+ return NULL;
+ }
+
+ for (size_t i = 0; i < n; ++i) {
+ if (dstrxcat(&ret, str, len) < 0) {
+ dstrfree(ret);
+ return NULL;
+ }
+ }
+
+ return ret;
+}
diff --git a/src/dstring.h b/src/dstring.h
index 6d50bc8..79458e4 100644
--- a/src/dstring.h
+++ b/src/dstring.h
@@ -320,4 +320,9 @@ int dstrescat(dchar **dest, const char *str, enum wesc_flags flags);
*/
int dstrnescat(dchar **dest, const char *str, size_t n, enum wesc_flags flags);
+/**
+ * Repeat a string n times.
+ */
+dchar *dstrepeat(const char *str, size_t n);
+
#endif // BFS_DSTRING_H