hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 657b5b3149ad495742df6b788d652cfdc6248e94
parent 78098e670b8645ac2c336811ab6c791890c7c3c8
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri,  4 Aug 2023 00:29:41 -0400

Remove slices:: and strings::cap

Closes: https://todo.sr.ht/~sircmpwn/hare/828
Co-authored-by: Ember Sawady <ecs@d2evs.net>
Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mscripts/gen-stdlib | 11-----------
Mscripts/install-mods | 1-
Dslices/README | 6------
Dslices/reverse.ha | 25-------------------------
Dslices/trunc.ha | 10----------
Dslices/void.ha | 164-------------------------------------------------------------------------------
Mstdlib.mk | 42------------------------------------------
Dstrings/cap.ha | 9---------
8 files changed, 0 insertions(+), 268 deletions(-)

diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1271,15 +1271,6 @@ shlex() { gen_ssa shlex ascii encoding::utf8 io strings strio } -slices() { - gen_srcs slices \ - cap.ha \ - reverse.ha \ - trunc.ha \ - void.ha - gen_ssa slices types rt -} - gensrcs_sort() { gen_srcs sort \ bisect.ha \ @@ -1314,7 +1305,6 @@ strconv() { strings() { gen_srcs strings \ - cap.ha \ concat.ha \ contains.ha \ dup.ha \ @@ -1654,7 +1644,6 @@ os::exec linux freebsd path regex shlex -slices sort strconv strings diff --git a/scripts/install-mods b/scripts/install-mods @@ -26,7 +26,6 @@ path regex rt shlex -slices sort strconv strings diff --git a/slices/README b/slices/README @@ -1,6 +0,0 @@ -The slices module provides some utility functions for working with slices. In -order to work with a user-supplied slice of an arbitrary type, the slice must be -cast to []void and the size of the member type passed alongside it. These -functions provide support code for common operations such as indexing and -appending, which are normally provided by language features, but which are not -available for []void slices. diff --git a/slices/reverse.ha b/slices/reverse.ha @@ -1,25 +0,0 @@ -// License: MPL-2.0 -// (c) 2021 Drew DeVault <sir@cmpwn.com> - -// Reverses a slice. -export fn reverse(b: []void, membsz: size) void = { - if (len(b) == 0) { - return; - }; - let a = b: *[*]u8; - for (let s = 0z, e = len(b) - 1; s < e) { - for (let i = 0z; i < membsz; i += 1z) { - let z = a[s * membsz + i]; - a[s * membsz + i] = a[e * membsz + i]; - a[e * membsz + i] = z; - }; - s += 1; - e -= 1; - }; -}; - -@test fn reverse() void = { - let a: []int = [1, 2, 3, 4]; - reverse(a, size(int)); - assert(a[0] == 4 && a[1] == 3 && a[2] == 2 && a[3] == 1); -}; diff --git a/slices/trunc.ha b/slices/trunc.ha @@ -1,10 +0,0 @@ -// License: MPL-2.0 -// (c) 2021 Drew DeVault <sir@cmpwn.com> -use types; - -// Truncates a slice, setting its length to zero without freeing the underlying -// storage or altering its capacity. -export fn trunc(sl: *[]void) void = { - let sl = sl: *types::slice; - sl.length = 0; -}; diff --git a/slices/void.ha b/slices/void.ha @@ -1,164 +0,0 @@ -// License: MPL-2.0 -// (c) 2021 Drew DeVault <sir@cmpwn.com> -use rt; -use types; - -// Appends an item, or multiple items, to a slice, reallocating if necessary. -export fn appendto(sl: *[]void, itemsz: size, items: const *void...) void = { - const repr = sl: *types::slice; - insertinto(sl, itemsz, repr.length, items...); -}; - -@test fn appendto() void = { - let input: []int = []; - let num = 1337; - appendto(&input: *[]void, size(int), &num, &num); - assert(len(input) == 2 && input[0] == 1337 && input[1] == 1337); - num = 7331; - appendto(&input: *[]void, size(int), &num); - assert(len(input) == 3 && input[0] == 1337 && input[1] == 1337 - && input[2] == 7331); - free(input); -}; - -// Appends an item, or multiple items, to a slice. Aborts if the slice's -// capacity isn't large enough to fit the items. -export fn static_appendto( - sl: *[]void, - itemsz: size, - items: const *void... -) void = { - const repr = sl: *types::slice; - static_insertinto(sl, itemsz, repr.length, items...); -}; - -// Inserts an item, or multiple items, to a slice, in O(n) time, reallocating if -// necessary. -export fn insertinto( - sl: *[]void, - itemsz: size, - idx: size, - items: const *void... -) void = { - if (len(items) == 0) { - return; - }; - let sl = sl: *types::slice; - sl.length += len(items); - rt::ensure(sl, itemsz); - let data = sl.data: *[*]u8; - rt::memmove(&data[(idx + len(items)) * itemsz], &data[idx * itemsz], - (sl.length - len(items) - idx) * itemsz); - for (let i = 0z; i < len(items); i += 1) { - rt::memcpy(&data[(idx + i) * itemsz], items[i], itemsz); - }; -}; - -@test fn insertinto() void = { - let input: []int = alloc([1, 3], 2); - let num = 2; - insertinto(&input: *[]void, size(int), 1, &num, &num); - assert(len(input) == 4 && input[0] == 1 && input[1] == 2 - && input[2] == 2 && input[3] == 3); - free(input); -}; - -// Inserts an item, or multiple items, into a slice, in O(n) time. Aborts if the -// slice's capacity isn't large enough to fit the items. -export fn static_insertinto( - sl: *[]void, - itemsz: size, - idx: size, - items: const *void... -) void = { - if (len(items) == 0) { - return; - }; - let sl = sl: *types::slice; - sl.length += len(items); - assert(sl.length <= sl.capacity, - "static insert/append exceeds slice capacity"); - let data = sl.data: *[*]u8; - rt::memmove(&data[(idx + len(items)) * itemsz], &data[idx * itemsz], - (sl.length - len(items) - idx) * itemsz); - for (let i = 0z; i < len(items); i += 1) { - rt::memcpy(&data[(idx + i) * itemsz], items[i], itemsz); - }; -}; - -// Deletes a range of items from a slice, in O(n) time. The slice may be -// reallocated. Reallocation will never fail. -export fn deletefrom(sl: *[]void, itemsz: size, start: size, end: size) void = { - static_deletefrom(sl, itemsz, start, end); - let sl = sl: *types::slice; - if (sl.length <= sl.capacity / 2) { - // TODO: switch to using alloc() once it's possible to handle - // copy allocation errors - match (rt::realloc(sl.data, sl.length * itemsz)) { - case null => void; - case let p: *void => - sl.data = p; - sl.capacity = sl.length; - }; - }; -}; - -@test fn deletefrom() void = { - let input: []int = alloc([1, 2, 3, 4, 5], 5); - deletefrom(&input: *[]void, size(int), 1, 1); - assert(len(input) == 5); - deletefrom(&input: *[]void, size(int), 1, 3); - assert(len(input) == 3 && input[0] == 1 && input[1] == 4 - && input[2] == 5); - free(input); -}; - -// Deletes a range of items from a slice, in O(n) time, without freeing memory. -export fn static_deletefrom( - sl: *[]void, - itemsz: size, - start: size, - end: size, -) void = { - assert(start <= end); - assert(itemsz != 0); - if (start == end) { - return; - }; - let sl = sl: *types::slice; - let data = sl.data: *[*]u8; - rt::memmove(&data[start * itemsz], &data[end * itemsz], - (sl.length - end) * itemsz); - sl.length -= end - start; -}; - -// Swaps two elements of a slice. -export fn swap(sl: []void, itemsz: size, a: size, b: size) void = { - assert(a < len(sl) && b < len(sl)); - let sl = sl: *[*]u8; - let a = &sl[a * itemsz]: *[*]u8, b = &sl[b * itemsz]: *[*]u8; - for (let i = 0z; i < itemsz; i += 1) { - let c = a[i]; - a[i] = b[i]; - b[i] = c; - }; -}; - -@test fn swap() void = { - let x: []int = [1, 2, 3]; - swap(x: []void, size(int), 0, 2); - assert(x[0] == 3 && x[2] == 1); -}; - -// Returns a pointer to the nth item of a slice. -export fn index(sl: []void, itemsz: size, n: size) *void = { - assert(n < len(sl)); - let ba = sl: *[*]u8; - return &ba[n * itemsz]; -}; - -@test fn index() void = { - let x: []int = [1, 2, 3]; - let ptr = index(x, size(int), 1): *int; - assert(*ptr == 2); -}; diff --git a/stdlib.mk b/stdlib.mk @@ -722,13 +722,6 @@ stdlib_deps_any += $(stdlib_shlex_any) stdlib_shlex_linux = $(stdlib_shlex_any) stdlib_shlex_freebsd = $(stdlib_shlex_any) -# gen_lib slices (any) -stdlib_slices_any = $(HARECACHE)/slices/slices-any.o -stdlib_env += HARE_TD_slices=$(HARECACHE)/slices/slices.td -stdlib_deps_any += $(stdlib_slices_any) -stdlib_slices_linux = $(stdlib_slices_any) -stdlib_slices_freebsd = $(stdlib_slices_any) - # gen_lib sort (any) stdlib_sort_any = $(HARECACHE)/sort/sort-any.o stdlib_env += HARE_TD_sort=$(HARECACHE)/sort/sort.td @@ -2005,19 +1998,6 @@ $(HARECACHE)/shlex/shlex-any.ssa: $(stdlib_shlex_any_srcs) $(stdlib_rt) $(stdlib @$(stdlib_env) $(HAREC) $(HAREFLAGS) -o $@ -Nshlex \ -t$(HARECACHE)/shlex/shlex.td $(stdlib_shlex_any_srcs) -# slices (+any) -stdlib_slices_any_srcs = \ - $(STDLIB)/slices/cap.ha \ - $(STDLIB)/slices/reverse.ha \ - $(STDLIB)/slices/trunc.ha \ - $(STDLIB)/slices/void.ha - -$(HARECACHE)/slices/slices-any.ssa: $(stdlib_slices_any_srcs) $(stdlib_rt) $(stdlib_types_$(PLATFORM)) $(stdlib_rt_$(PLATFORM)) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/slices - @$(stdlib_env) $(HAREC) $(HAREFLAGS) -o $@ -Nslices \ - -t$(HARECACHE)/slices/slices.td $(stdlib_slices_any_srcs) - # sort (+any) stdlib_sort_any_srcs = \ $(STDLIB)/sort/bisect.ha \ @@ -2051,7 +2031,6 @@ $(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/dup.ha \ @@ -3147,13 +3126,6 @@ testlib_deps_any += $(testlib_shlex_any) testlib_shlex_linux = $(testlib_shlex_any) testlib_shlex_freebsd = $(testlib_shlex_any) -# gen_lib slices (any) -testlib_slices_any = $(TESTCACHE)/slices/slices-any.o -testlib_env += HARE_TD_slices=$(TESTCACHE)/slices/slices.td -testlib_deps_any += $(testlib_slices_any) -testlib_slices_linux = $(testlib_slices_any) -testlib_slices_freebsd = $(testlib_slices_any) - # gen_lib sort (any) testlib_sort_any = $(TESTCACHE)/sort/sort-any.o testlib_env += HARE_TD_sort=$(TESTCACHE)/sort/sort.td @@ -4489,19 +4461,6 @@ $(TESTCACHE)/shlex/shlex-any.ssa: $(testlib_shlex_any_srcs) $(testlib_rt) $(test @$(testlib_env) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nshlex \ -t$(TESTCACHE)/shlex/shlex.td $(testlib_shlex_any_srcs) -# slices (+any) -testlib_slices_any_srcs = \ - $(STDLIB)/slices/cap.ha \ - $(STDLIB)/slices/reverse.ha \ - $(STDLIB)/slices/trunc.ha \ - $(STDLIB)/slices/void.ha - -$(TESTCACHE)/slices/slices-any.ssa: $(testlib_slices_any_srcs) $(testlib_rt) $(testlib_types_$(PLATFORM)) $(testlib_rt_$(PLATFORM)) - @printf 'HAREC \t$@\n' - @mkdir -p $(TESTCACHE)/slices - @$(testlib_env) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nslices \ - -t$(TESTCACHE)/slices/slices.td $(testlib_slices_any_srcs) - # sort (+any) testlib_sort_any_srcs = \ $(STDLIB)/sort/bisect.ha \ @@ -4538,7 +4497,6 @@ $(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/dup.ha \ diff --git a/strings/cap.ha b/strings/cap.ha @@ -1,9 +0,0 @@ -// 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; -};