add Str.to_utf8 builtin

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Luke Boswell 2025-11-26 12:38:54 +11:00
parent e81c896d2a
commit 8fcd482901
No known key found for this signature in database
GPG key ID: 54A7324B1B975757
5 changed files with 52 additions and 0 deletions

View file

@ -152,6 +152,9 @@ fn replaceStrIsEmptyWithLowLevel(env: *ModuleEnv) !std.ArrayList(CIR.Def.Idx) {
if (env.common.findIdent("Builtin.Str.release_excess_capacity")) |str_release_excess_capacity_ident| {
try low_level_map.put(str_release_excess_capacity_ident, .str_release_excess_capacity);
}
if (env.common.findIdent("Builtin.Str.to_utf8")) |str_to_utf8_ident| {
try low_level_map.put(str_to_utf8_ident, .str_to_utf8);
}
if (env.common.findIdent("Builtin.List.len")) |list_len_ident| {
try low_level_map.put(list_len_ident, .list_len);
}

View file

@ -19,6 +19,7 @@ Builtin :: [].{
with_capacity : U64 -> Str
reserve : Str, U64 -> Str
release_excess_capacity : Str -> Str
to_utf8 : Str -> List(U8)
}
List(_item) :: [ProvidedByCompiler].{

View file

@ -419,6 +419,7 @@ pub const Expr = union(enum) {
str_with_capacity,
str_reserve,
str_release_excess_capacity,
str_to_utf8,
// Numeric to_str operations
u8_to_str,

View file

@ -3173,6 +3173,31 @@ pub const Interpreter = struct {
out.is_initialized = true;
return out;
},
.str_to_utf8 => {
// Str.to_utf8 : Str -> List(U8)
std.debug.assert(args.len == 1);
const string_arg = args[0];
std.debug.assert(string_arg.ptr != null);
const string: *const RocStr = @ptrCast(@alignCast(string_arg.ptr.?));
const result_list = builtins.str.strToUtf8C(string.*, roc_ops);
const result_rt_var = return_rt_var orelse {
self.triggerCrash("str_to_utf8 requires return type info", false, roc_ops);
return error.Crash;
};
const result_layout = try self.getRuntimeLayout(result_rt_var);
var out = try self.pushRaw(result_layout, 0);
out.is_initialized = false;
const result_ptr: *builtins.list.RocList = @ptrCast(@alignCast(out.ptr.?));
result_ptr.* = result_list;
out.is_initialized = true;
return out;
},
.list_len => {
// List.len : List(a) -> U64
// Note: listLen returns usize, but List.len always returns U64.

View file

@ -0,0 +1,22 @@
# META
~~~ini
description=Str.to_utf8 should convert a string to a list of UTF-8 bytes
type=repl
~~~
# SOURCE
~~~roc
» List.len(Str.to_utf8(""))
» List.len(Str.to_utf8("hello"))
» List.len(Str.to_utf8("é"))
» List.len(Str.to_utf8("🎉"))
~~~
# OUTPUT
0
---
5
---
2
---
4
# PROBLEMS
NIL