hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 2e2be8aef1a769d22595831ed0a040789f61eeb3
parent 860eff72eb9039802ad792a9d9607fb6c131fcfe
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 30 Jan 2021 18:49:04 -0500

strings: add UTF-8 helper functions

Diffstat:
Astrings/utf8.ha | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/strings/utf8.ha b/strings/utf8.ha @@ -0,0 +1,23 @@ +use types; + +// Converts a byte slice into a string WITHOUT checking that the byte slice is a +// valid UTF-8 string. +export fn from_utf8_unsafe(in: []u8) str = { + const s = struct { // TODO: Use types::string + data: *[*]u8 = (&in: *types::slice).data: *[*]u8, + length: size = len(in), + capacity: size = len(in), + }; + return *(&s: *const str); +}; + +// Converts a byte slice into a string. Aborts if the bytes contain invalid +// UTF-8. To handle such an error without aborting, see +// [encoding::utf8::decode] instead. +export fn from_utf8(in: []u8) str = { + // TODO: Validate string + return from_utf8_unsafe(in); +}; + +// Converts a string to a UTF-8 slice. +export fn to_utf8(in: str) []u8 = *(&in: *[]u8);