~sircmpwn/hare-dev

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

[PATCH hare] types::c: add tostrn and tostrn_unsafe

Details
Message ID
<20230513011712.10870-1-sebastian@sebsite.pw>
DKIM signature
pass
Download raw message
Patch: +15 -4
For when the length of the C string is already known, so it doesn't have
to be recalculated.

Signed-off-by: Sebastian <sebastian@sebsite.pw>
---
 types/c/strings.ha | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/types/c/strings.ha b/types/c/strings.ha
index 20dae411..dce99c3c 100644
--- a/types/c/strings.ha
+++ b/types/c/strings.ha
@@ -13,11 +13,16 @@ export fn strlen(cstr: *const char) size = {
// Converts a C string to a Hare string in O(n), and does not check if it's
// valid UTF-8.
export fn tostr_unsafe(cstr: *const char) const str = {
	const l = strlen(cstr);
	return tostrn_unsafe(cstr, strlen(cstr));
};

// Converts a C string with a given length to a Hare string, and does not check
// if it's valid UTF-8.
export fn tostrn_unsafe(cstr: *const char, length: size) const str = {
	const s = types::string {
		data = cstr: *[*]u8,
		length = l,
		capacity = l + 1,
		length = length,
		capacity = length + 1,
	};
	return *(&s: *const str);
};
@@ -25,7 +30,13 @@ export fn tostr_unsafe(cstr: *const char) const str = {
// Converts a C string to a Hare string in O(n). If the string is not valid
// UTF-8, return [[encoding::utf8::invalid]].
export fn tostr(cstr: *const char) (const str | utf8::invalid) = {
	let s = tostr_unsafe(cstr);
	return tostrn(cstr, strlen(cstr));
};

// Converts a C string with a given length to a Hare string. If the string is
// not valid UTF-8, return [[encoding::utf8::invalid]].
export fn tostrn(cstr: *const char, length: size) (const str | utf8::invalid) = {
	let s = tostrn_unsafe(cstr, length);
	return if (utf8::valid(s)) s else utf8::invalid;
};

-- 
2.38.5

[hare/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CSKR6ETC9KIQ.VMSBR8DXKQ65@cirno2>
In-Reply-To
<20230513011712.10870-1-sebastian@sebsite.pw> (view parent)
DKIM signature
missing
Download raw message
hare/patches: SUCCESS in 1m44s

[types::c: add tostrn and tostrn_unsafe][0] from [Sebastian][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/41099
[1]: sebastian@sebsite.pw

✓ #988959 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/988959
✓ #988960 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/988960
Details
Message ID
<CSKZYQ22E3R4.DI2BBT5VZESM@gura>
In-Reply-To
<20230513011712.10870-1-sebastian@sebsite.pw> (view parent)
DKIM signature
pass
Download raw message
+1. I also think that it would be nice to have a function here which can
convert strings to C strings without allocation if the caller ensures
that the nul delimiter is there. Should be simple so that it can be
easily used for interop:

printf(c::string("hello world!\n\0"));
Details
Message ID
<CSL79GBH81BP.1VIDHW1DGYFCM@monch>
In-Reply-To
<CSKZYQ22E3R4.DI2BBT5VZESM@gura> (view parent)
DKIM signature
pass
Download raw message
On Sat May 13, 2023 at 8:12 AM UTC, Drew DeVault wrote:
> +1. I also think that it would be nice to have a function here which can
> convert strings to C strings without allocation if the caller ensures
> that the nul delimiter is there. Should be simple so that it can be
> easily used for interop:
>
> printf(c::string("hello world!\n\0"));

c::tostr currently does that, maybe we should rename it? i'm also not a
huge fan of tostrn as a name, but i don't have ideas on how it could be
better
Details
Message ID
<CSLFOZR3S1XX.1NKY06WXKVLE0@notmylaptop>
In-Reply-To
<CSL79GBH81BP.1VIDHW1DGYFCM@monch> (view parent)
DKIM signature
pass
Download raw message
On Sat May 13, 2023 at 9:55 AM EDT, Ember Sawady wrote:
> On Sat May 13, 2023 at 8:12 AM UTC, Drew DeVault wrote:
> > +1. I also think that it would be nice to have a function here which can
> > convert strings to C strings without allocation if the caller ensures
> > that the nul delimiter is there. Should be simple so that it can be
> > easily used for interop:
> >
> > printf(c::string("hello world!\n\0"));
>
> c::tostr currently does that, maybe we should rename it? i'm also not a
> huge fan of tostrn as a name, but i don't have ideas on how it could be
> better

No, c::fromstr is what Drew is suggesting (converting from a Hare string
to a C string), but it allocates. I agree that an alternative that
doesn't allocate would be nice, but maybe fromstr should be renamed.

Agreed that tostrn isn't great, open to suggestions.
Reply to thread Export thread (mbox)