hare

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

commit aaa6b93379bb4b064238da67f298e6dbb91e7a56
parent c08cc7e751106913fe3c146f9091fb4b84ddc454
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun,  7 Feb 2021 12:01:16 -0500

strings::concat

Diffstat:
Astrings/concat.ha | 42++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+), 0 deletions(-)

diff --git a/strings/concat.ha b/strings/concat.ha @@ -0,0 +1,42 @@ +use rt; +use types; + +// Concatenates two or more strings. The caller must free the return value. +export fn concat(strs: str...) str = { + let z = 0z; + for (let i = 0z; i < len(strs); i += 1z) { + z += len(strs[i]); + }; + let new = alloc([]u8, [], z + 1z); + let buf = new: *[*]u8; + for (let i = 0z, j = 0z; i < len(strs); i += 1z) { + rt::memcpy(&buf[j], strs[i]: *const char: *[*]u8, len(strs[i])); + j += len(strs[i]); + }; + buf[z + 1z] = 0u8; + (&new: *types::string).length = z; + return *(&new: *str); +}; + +@test fn concat() void = { + let s = concat("hello ", "world"); + assert(s == "hello world"); + assert((s: *const char: *[*]u8)[len(s)] == 0u8); + free(s); + + s = concat("hello", " ", "world"); + assert(s == "hello world"); + free(s); + + s = concat("hello", "", "world"); + assert(s == "helloworld"); + free(s); + + s = concat("", ""); + assert(s == ""); + free(s); + + s = concat(); + assert(s == ""); + free(s); +};