~sircmpwn/hare

4ec162a5b798f33e0a836f71b5d1c1313d14640c — Sebastian 1 year, 14 days ago 55fcd75
strings: improve docstrings

Signed-off-by: Sebastian <sebastian@sebsite.pw>
2 files changed, 13 insertions(+), 16 deletions(-)

M strings/concat.ha
M strings/tokenize.ha
M strings/concat.ha => strings/concat.ha +1 -1
@@ 1,7 1,7 @@
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

// Concatenates two or more strings. The caller must free the return value.
// Concatenates multiple 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 += 1) {

M strings/tokenize.ha => strings/tokenize.ha +12 -15
@@ 30,7 30,7 @@ export fn tokenize(s: str, delim: str) tokenizer =
// 	assert(strings::next_token(&tok) as str == "name");
// 	assert(strings::remaining_tokens(&tok) == "hello, my");
//
// The caller must ensure that 'delimiter' is not an empty string.
// The caller must ensure that 'delim' is not an empty string.
export fn rtokenize(s: str, delim: str) tokenizer =
	bytes::rtokenize(toutf8(s), toutf8(delim));



@@ 95,10 95,9 @@ export fn remaining_tokens(s: *tokenizer) str = {

// Splits a string into tokens delimited by 'delim', starting at the beginning
// of the string, and returning a slice of up to N tokens. The caller must free
// this slice. The strings within the slice are borrowed from 'in', and needn't
// be freed - but should be [[dupall]]'d if they should outlive 'in'.
// this slice. The strings within the slice are borrowed from 'in'.
//
// The caller must ensure that 'delimiter' is not an empty string.
// The caller must ensure that 'delim' is not an empty string.
export fn splitn(in: str, delim: str, n: size) []str = {
	let toks: []str = [];
	let tok = tokenize(in, delim);


@@ 120,10 119,9 @@ export fn splitn(in: str, delim: str, n: size) []str = {

// Splits a string into tokens delimited by 'delim', starting at the end
// of the string, and returning a slice of up to N tokens. The caller must free
// this slice. The strings within the slice are borrowed from 'in', and needn't
// be freed - but should be [[dupall]]'d if they should outlive 'in'.
// this slice. The strings within the slice are borrowed from 'in'.
//
// The caller must ensure that 'delimiter' is not an empty string.
// The caller must ensure that 'delim' is not an empty string.
export fn rsplitn(in: str, delim: str, n: size) []str = {
	let toks: []str = [];
	let tok = rtokenize(in, delim);


@@ 143,11 141,10 @@ export fn rsplitn(in: str, delim: str, n: size) []str = {
	return toks;
};

// Splits a string into tokens delimited by 'delim'.  The caller must free the
// returned slice. The strings within the slice are borrowed from 'in', and
// needn't be freed - but must be [[dupall]]'d if they should outlive 'in'.
// Splits a string into tokens delimited by 'delim'. The caller must free the
// returned slice. The strings within the slice are borrowed from 'in'.
//
// The caller must ensure that 'delimiter' is not an empty string.
// The caller must ensure that 'delim' is not an empty string.
export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX);

@test fn split() void = {


@@ 187,8 184,8 @@ export fn split(in: str, delim: str) []str = splitn(in, delim, types::SIZE_MAX);
// 	strings::cut("hello=world=foobar", "=")	// ("hello", "world=foobar")
// 	strings::cut("hello world", "=")	// ("hello world", "")
//
// The return value is borrowed from the 'in' parameter.  The caller must ensure
// that 'delimiter' is not an empty string.
// The return value is borrowed from the 'in' parameter. The caller must ensure
// that 'delim' is not an empty string.
export fn cut(in: str, delim: str) (str, str) = {
	let c = bytes::cut(toutf8(in), toutf8(delim));
	return (fromutf8_unsafe(c.0), fromutf8_unsafe(c.1));


@@ 201,8 198,8 @@ export fn cut(in: str, delim: str) (str, str) = {
// 	strings::rcut("hello=world=foobar", "=")	// ("hello=world", "foobar")
// 	strings::rcut("hello world", "=")	// ("hello world", "")
//
// The return value is borrowed from the 'in' parameter.  The caller must ensure
// that 'delimiter' is not an empty string.
// The return value is borrowed from the 'in' parameter. The caller must ensure
// that 'delim' is not an empty string.
export fn rcut(in: str, delim: str) (str, str) = {
	let c = bytes::rcut(toutf8(in), toutf8(delim));
	return (fromutf8_unsafe(c.0), fromutf8_unsafe(c.1));