commit 762926142712b609a448c08eb4e07291107718af
parent 8099b144cbbccd960ecfe48a74f5ca297b5c01b8
Author: Sebastian <sebastian@sebsite.pw>
Date: Fri, 8 Apr 2022 15:11:08 -0400
slices, strings: add cap function
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
4 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -1068,6 +1068,7 @@ shlex() {
slices() {
gen_srcs slices \
+ cap.ha \
reverse.ha \
trunc.ha \
void.ha
@@ -1108,6 +1109,7 @@ strconv() {
strings() {
gen_srcs strings \
+ cap.ha \
concat.ha \
contains.ha \
cstrings.ha \
diff --git a/slices/cap.ha b/slices/cap.ha
@@ -0,0 +1,9 @@
+// License: MPL-2.0
+// (c) 2022 Sebastian <sebastian@sebsite.pw>
+use types;
+
+// Returns the capacity of a slice.
+export fn cap(slice: []const void) size = {
+ const slice = &slice: *types::slice;
+ return slice.capacity;
+};
diff --git a/stdlib.mk b/stdlib.mk
@@ -1640,6 +1640,7 @@ $(HARECACHE)/shlex/shlex-any.ssa: $(stdlib_shlex_any_srcs) $(stdlib_rt) $(stdlib
# slices (+any)
stdlib_slices_any_srcs= \
+ $(STDLIB)/slices/cap.ha \
$(STDLIB)/slices/reverse.ha \
$(STDLIB)/slices/trunc.ha \
$(STDLIB)/slices/void.ha
@@ -1683,6 +1684,7 @@ $(HARECACHE)/strconv/strconv-any.ssa: $(stdlib_strconv_any_srcs) $(stdlib_rt) $(
# strings (+any)
stdlib_strings_any_srcs= \
+ $(STDLIB)/strings/cap.ha \
$(STDLIB)/strings/concat.ha \
$(STDLIB)/strings/contains.ha \
$(STDLIB)/strings/cstrings.ha \
@@ -3579,6 +3581,7 @@ $(TESTCACHE)/shlex/shlex-any.ssa: $(testlib_shlex_any_srcs) $(testlib_rt) $(test
# slices (+any)
testlib_slices_any_srcs= \
+ $(STDLIB)/slices/cap.ha \
$(STDLIB)/slices/reverse.ha \
$(STDLIB)/slices/trunc.ha \
$(STDLIB)/slices/void.ha
@@ -3625,6 +3628,7 @@ $(TESTCACHE)/strconv/strconv-any.ssa: $(testlib_strconv_any_srcs) $(testlib_rt)
# strings (+any)
testlib_strings_any_srcs= \
+ $(STDLIB)/strings/cap.ha \
$(STDLIB)/strings/concat.ha \
$(STDLIB)/strings/contains.ha \
$(STDLIB)/strings/cstrings.ha \
diff --git a/strings/cap.ha b/strings/cap.ha
@@ -0,0 +1,9 @@
+// License: MPL-2.0
+// (c) 2022 Sebastian <sebastian@sebsite.pw>
+use types;
+
+// Returns the capacity of a string.
+export fn cap(s: str) size = {
+ const s = &s: *types::string;
+ return s.capacity;
+};