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
+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"));
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
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.