commit 8c71e3535fcf4a64c33ece2b8d7f8c61718689ae parent afd8a104c5819f71f064c10daaa39d8f794c796f Author: Drew DeVault <sir@cmpwn.com> Date: Sat, 30 Jan 2021 15:03:38 -0500 strings: new module Diffstat:
A | strings/cstrings.ha | | | 17 | +++++++++++++++++ |
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/strings/cstrings.ha b/strings/cstrings.ha @@ -0,0 +1,17 @@ +export fn c_strlen(cstr: *const char) size = { + const ptr = cstr: *[*]u8; + let ln = 0z; + for (ptr[ln] != 0u8; ln += 1z) void; + return ln; +}; + +// Wraps a C string in the appropriate Hare structures +export fn from_c(cstr: *const char) const str = { + const l = c_strlen(cstr); + const s = struct { // TODO: Use types::string + data: *[*]u8 = cstr: *[*]u8, + length: size = l, + capacity: size = l, + }; + return *(&s: *const str); +};