add Str.count_utf8_bytes 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:06:48 +11:00
parent aed631129d
commit 3b07bd35a4
No known key found for this signature in database
GPG key ID: 54A7324B1B975757
5 changed files with 47 additions and 0 deletions

View file

@ -140,6 +140,9 @@ fn replaceStrIsEmptyWithLowLevel(env: *ModuleEnv) !std.ArrayList(CIR.Def.Idx) {
if (env.common.findIdent("Builtin.Str.drop_suffix")) |str_drop_suffix_ident| {
try low_level_map.put(str_drop_suffix_ident, .str_drop_suffix);
}
if (env.common.findIdent("Builtin.Str.count_utf8_bytes")) |str_count_utf8_bytes_ident| {
try low_level_map.put(str_count_utf8_bytes_ident, .str_count_utf8_bytes);
}
if (env.common.findIdent("Builtin.List.len")) |list_len_ident| {
try low_level_map.put(list_len_ident, .list_len);
}

View file

@ -15,6 +15,7 @@ Builtin :: [].{
with_prefix : Str, Str -> Str
drop_prefix : Str, Str -> Str
drop_suffix : Str, Str -> Str
count_utf8_bytes : Str -> U64
}
List(_item) :: [ProvidedByCompiler].{

View file

@ -415,6 +415,7 @@ pub const Expr = union(enum) {
str_with_prefix,
str_drop_prefix,
str_drop_suffix,
str_count_utf8_bytes,
// Numeric to_str operations
u8_to_str,

View file

@ -3091,6 +3091,23 @@ pub const Interpreter = struct {
out.is_initialized = true;
return out;
},
.str_count_utf8_bytes => {
// Str.count_utf8_bytes : Str -> U64
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 byte_count = builtins.str.countUtf8Bytes(string.*);
const result_layout = layout.Layout.int(.u64);
var out = try self.pushRaw(result_layout, 0);
out.is_initialized = false;
try out.setInt(@intCast(byte_count));
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,25 @@
# META
~~~ini
description=Str.count_utf8_bytes should return the number of bytes in the string
type=repl
~~~
# SOURCE
~~~roc
» Str.count_utf8_bytes("")
» Str.count_utf8_bytes("hello")
» Str.count_utf8_bytes("hello world")
» Str.count_utf8_bytes("é")
» Str.count_utf8_bytes("🎉")
~~~
# OUTPUT
0
---
5
---
11
---
2
---
4
# PROBLEMS
NIL