mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-23 08:48:03 +00:00
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:
parent
e81c896d2a
commit
8fcd482901
5 changed files with 52 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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].{
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
22
test/snapshots/repl/str_to_utf8.md
Normal file
22
test/snapshots/repl/str_to_utf8.md
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue