~tomterl/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH thp] Restore stdlib functions strings::pad*

Carlos Une <une@fastmail.fm>
Details
Message ID
<20231129022643.9332-1-une@fastmail.fm>
DKIM signature
missing
Download raw message
Patch: +66 -4
They were removed by commit
https://git.sr.ht/~sircmpwn/hare/commit/4b5d45099f87315a879a3787ad0d6e23b3ea254e
---
 git/buffer.ha    |  4 ++--
 prompt/prompt.ha |  5 ++--
 undead/pad.ha    | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 4 deletions(-)
 create mode 100644 undead/pad.ha

diff --git a/git/buffer.ha b/git/buffer.ha
index a9b4245..da4cc68 100644
--- a/git/buffer.ha
+++ b/git/buffer.ha
@@ -3,7 +3,7 @@
//
// SPDX-FileCopyrightText:  2022 Tom Regner <tomte@tomsdiner.org>
// SPDX-License-Identifier: GPL-3.0-or-laterP
use strings;
use undead;
use types;
use types::c;

@@ -17,7 +17,7 @@ export type buf = struct {
export fn get_out() *buf = {
	// start with a sensible size
	// the ptr will be freed by libgit2
	const buffer = strings::padend("", '\0', 256);
	const buffer = undead::padend("", '\0', 256);
	const b = &buffer: *types::string;
	return alloc(buf {
		ptr = b.data: const *c::char,
diff --git a/prompt/prompt.ha b/prompt/prompt.ha
index 808a471..2adb211 100644
--- a/prompt/prompt.ha
+++ b/prompt/prompt.ha
@@ -11,6 +11,7 @@ use strings;
use term::*;
use env;
use mods;
use undead;

fn call_mod(n: str, pe: *env::env) str = {
	match(mods::call(n, pe)) {
@@ -90,9 +91,9 @@ export fn prompt(pe: *env::env) str = {
		};
		let sidx = 0u;
		const diff = rem[l] - (exps * ct[l]);
		const lastexp = if (diff < 1 ) { yield strings::dup(""); } else { yield strings::padend("", ' ', diff);};
		const lastexp = if (diff < 1 ) { yield strings::dup(""); } else { yield undead::padend("", ' ', diff);};
		defer free(lastexp);
		const exp = if (exps < 1) { yield strings::dup(""); } else { yield strings::padend("", ' ', exps);};
		const exp = if (exps < 1) { yield strings::dup(""); } else { yield undead::padend("", ' ', exps);};
		defer free(exp);
		for (i < len(parts); i += 1) {
			switch(parts[i]) {
diff --git a/undead/pad.ha b/undead/pad.ha
new file mode 100644
index 0000000..62bb735
--- /dev/null
+++ b/undead/pad.ha
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use encoding::utf8;
use strings;

// 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 strings::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, strings::toutf8(s)...);
	return strings::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 strings::dup(s);
	};
	let res: []u8 = alloc([], target_len);
	append(res, strings::toutf8(s)...);
	for (let i = 0z; i < target_len - len(s); i += 1) {
		append(res, utf8::encoderune(prefix)...);
	};
	return strings::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);
};
-- 
2.39.2
Details
Message ID
<87leahf6i9.fsf@tomsdiner.org>
In-Reply-To
<20231129022643.9332-1-une@fastmail.fm> (view parent)
DKIM signature
missing
Download raw message
Thanks!

I opted to replace the padend calls with the means of the fmt module.
It runs fine, as far as I can tell.

The tests do not work here ATM (0/0 tests run) - something to do with the module handling changes I guess; will have to figure out where to put them :)
Reply to thread Export thread (mbox)