~sircmpwn/hare

4b5d45099f87315a879a3787ad0d6e23b3ea254e — Sebastian 10 months ago 4e47d42
strings: drop pad functions

>From what I could find, these were used in exactly one place:
hautils:nl.ha, where it was used to allocate a string of one character
repeated n times (so not even using the function for padding).

Signed-off-by: Sebastian <sebastian@sebsite.pw>
M makefiles/freebsd.aarch64.mk => makefiles/freebsd.aarch64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

M makefiles/freebsd.riscv64.mk => makefiles/freebsd.riscv64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

M makefiles/freebsd.x86_64.mk => makefiles/freebsd.x86_64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

M makefiles/linux.aarch64.mk => makefiles/linux.aarch64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

M makefiles/linux.riscv64.mk => makefiles/linux.riscv64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

M makefiles/linux.x86_64.mk => makefiles/linux.x86_64.mk +1 -1
@@ 39,7 39,7 @@ $(HARECACHE)/sort_cmp.ssa: $(sort_cmp_ha)
	@printf 'HAREC\t%s\n' "$@"
	@$(TDENV) $(HAREC) $(HARECFLAGS) -o $(HARECACHE)/sort_cmp.ssa -t $(HARECACHE)/sort_cmp.td.tmp -N sort::cmp $(sort_cmp_ha)

strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/pad.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
strings_ha = strings/compare.ha strings/concat.ha strings/contains.ha strings/dup.ha strings/index.ha strings/iter.ha strings/replace.ha strings/runes.ha strings/sub.ha strings/suffix.ha strings/tokenize.ha strings/trim.ha strings/utf8.ha
$(HARECACHE)/strings.ssa: $(strings_ha) $(HARECACHE)/bytes.td $(HARECACHE)/encoding_utf8.td $(HARECACHE)/sort_cmp.td $(HARECACHE)/types.td
	@mkdir -p -- "$(HARECACHE)"
	@printf 'HAREC\t%s\n' "$@"

D strings/pad.ha => strings/pad.ha +0 -60
@@ 1,60 0,0 @@
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use encoding::utf8;

// Pads a string's start with 'prefix' until it reaches length 'target_len'.
// The caller must free the return value.
export fn padstart(s: str, prefix: rune, target_len: size) str = {
	if (len(s) >= target_len) {
		return dup(s);
	};
	let res: []u8 = alloc([], target_len);
	for (let i = 0z; i < target_len - len(s); i += 1) {
		append(res, utf8::encoderune(prefix)...);
	};
	append(res, toutf8(s)...);
	return fromutf8_unsafe(res[..target_len]);
};

@test fn padstart() void = {
	let s = padstart("2", '0', 5);
	assert(s == "00002");
	free(s);

	let s = padstart("12345", '0', 5);
	assert(s == "12345");
	free(s);

	let s = padstart("", '0', 5);
	assert(s == "00000");
	free(s);
};

// Pads a string's end with 'prefix' until it reaches length 'target_len'.
// The caller must free the return value.
export fn padend(s: str, prefix: rune, target_len: size) str = {
	if (len(s) >= target_len) {
		return dup(s);
	};
	let res: []u8 = alloc([], target_len);
	append(res, toutf8(s)...);
	for (let i = 0z; i < target_len - len(s); i += 1) {
		append(res, utf8::encoderune(prefix)...);
	};
	return fromutf8_unsafe(res[..target_len]);
};

@test fn padend() void = {
	let s = padend("2", '0', 5);
	assert(s == "20000");
	free(s);

	let s = padend("12345", '0', 5);
	assert(s == "12345");
	free(s);

	let s = padend("", '0', 5);
	assert(s == "00000");
	free(s);
};