lib/strings: add cut and rcut
This commit is contained in:
@@ -720,6 +720,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
|
||||
|
||||
Reference in New Issue
Block a user