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