hare

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

commit a95ddfea79d51aa590edf6948abf4fc988c362d5
parent 8eb1db9bdf26f85748256391cbe64d14342ea76f
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu, 23 Dec 2021 09:41:40 +0100

strings: add cut

Shamelessly stole this idea from Go

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mstrings/tokenize.ha | 32++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+), 0 deletions(-)

diff --git a/strings/tokenize.ha b/strings/tokenize.ha @@ -130,3 +130,35 @@ export fn split(in: str, delim: str) []str = splitN(in, delim, types::SIZE_MAX); assert(expected2[i] == actual2[i]); }; }; + +// Returns a string "cut" along the first instance of a delimiter, returning +// everything up to the delimiter, and everything after the delimiter, in a +// tuple. +// +// strings::cut("hello=world=foobar", "=") // ("hello", "world=foobar") +// strings::cut("hello world", "=") // ("hello world", "") +// +// The return value is borrowed from the 'in' parameter. +export fn cut(in: str, delim: str) (str, str) = { + const tok = tokenize(in, delim); + let res = ("", ""); + match (next_token(&tok)) { + case let s: str => + res.0 = s; + case void => + return res; + }; + res.1 = remaining_tokens(&tok); + return res; +}; + +@test fn cut() void = { + const sample = cut("hello=world", "="); + assert(sample.0 == "hello" && sample.1 == "world"); + const sample = cut("hello=world=foobar", "="); + assert(sample.0 == "hello" && sample.1 == "world=foobar"); + const sample = cut("hello world", "="); + assert(sample.0 == "hello world" && sample.1 == ""); + const sample = cut("", "="); + assert(sample.0 == "" && sample.1 == ""); +};