commit 2fc75eeeddf7dcc4688221616c72da18c69072fc
parent 2ae0ad812024e0bb48139a78ff283951401ebc38
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 31 Jan 2021 15:20:44 -0500
strings::dup: new function
Diffstat:
1 file changed, 19 insertions(+), 0 deletions(-)
diff --git a/strings/dup.ha b/strings/dup.ha
@@ -0,0 +1,19 @@
+use bytes;
+use rt;
+use types;
+
+// Duplicates a string. Aborts on allocation failure.
+export fn dup(s: const str) str = {
+ let in = &s: *types::string;
+ let buf: *[*]u8 = match (rt::malloc(in.length + 1z)) {
+ null => abort("Out of memory"),
+ v: *void => v: *[*]u8,
+ };
+ bytes::copy(buf[..in.length + 1z], in.data[..in.length + 1z]);
+ let out = types::string {
+ data = buf,
+ length = in.length,
+ capacity = in.length,
+ };
+ return *(&out: *str);
+};