hare

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

commit cbeb756c2cd04f78ac2a15d772a1b5b38674be1a
parent 37db9da0ed6ab486b6496d1ee5bf9f07224e5ed2
Author: Tom Regner <tomte@tomsdiner.org>
Date:   Thu, 20 Oct 2022 07:27:01 +0200

strings: splitn: don't add non existing tokens

Using void as no-op, as suggested.

Diffstat:
Mstrings/tokenize.ha | 13++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/strings/tokenize.ha b/strings/tokenize.ha @@ -86,7 +86,11 @@ export fn splitn(in: str, delim: str, n: size) []str = { return toks; }; }; - append(toks, remaining_tokens(&tok)); + match(peek_token(&tok)) { + case void => void; + case let s: str => + append(toks, remaining_tokens(&tok)); + }; return toks; }; @@ -111,6 +115,13 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX); for (let i = 0z; i < len(expected2); i += 1) { assert(expected2[i] == actual2[i]); }; + + const expected3 = ["one"]; + const actual3 = splitn("one", "=", 2z); + assert(len(expected3) == len(actual3)); + for (let i = 0z; i < len(expected3); i += 1) { + assert(expected3[i] == actual3[i]); + }; }; // Returns a string "cut" along the first instance of a delimiter, returning