lib/strings: add cut and rcut

This commit is contained in:
2026-06-01 16:26:11 +09:00
parent 2b893b9353
commit 38a906cd9b
8 changed files with 232 additions and 0 deletions

View File

@@ -2697,6 +2697,23 @@ export fn remaining_tokens(s: *tokenizer) str = {
return frombytes(bytes.remaining_tokens(b));
};
// cut — split `in` along the first instance of `delim`, returning the
// portions before and after it. When `delim` is absent the whole input
// is the first half and the second is empty. Both halves are borrowed
// from `in`; caller ensures `delim` is non-empty.
// ref/hare/strings/tokenize.ha:288.
export fn cut(in: str, delim: str) (str, str) = {
let (a, b) = bytes.cut(toutf8(in), toutf8(delim));
return (frombytes(a), frombytes(b));
};
// rcut — like [[cut]] but split along the LAST instance of `delim`.
// ref/hare/strings/tokenize.ha:302.
export fn rcut(in: str, delim: str) (str, str) = {
let (a, b) = bytes.rcut(toutf8(in), toutf8(delim));
return (frombytes(a), frombytes(b));
};
// rt_ensure is the runtime slice-growth helper invoked by the
// `append(s, v)` builtin. Direct bind for the same reason as
// lib/shlex.shlex (appendstr, 16B): the builtin's expansion stores