wasm: Implement Str.split

This commit is contained in:
Brian Carroll 2022-07-04 09:29:36 +01:00
parent d2dbb0001a
commit 02ec30425c
No known key found for this signature in database
GPG key ID: 9CF4E3BF9C4722C7
7 changed files with 267 additions and 259 deletions

View file

@ -143,6 +143,7 @@ const str = @import("str.zig");
comptime {
exportStrFn(str.init, "init");
exportStrFn(str.strToScalarsC, "to_scalars");
exportStrFn(str.strSplit, "str_split");
exportStrFn(str.strSplitInPlaceC, "str_split_in_place");
exportStrFn(str.countSegments, "count_segments");
exportStrFn(str.countGraphemeClusters, "count_grapheme_clusters");

View file

@ -744,6 +744,21 @@ fn strFromFloatHelp(comptime T: type, float: T) RocStr {
}
// Str.split
// For dev backends
pub fn strSplit(string: RocStr, delimiter: RocStr) callconv(.C) RocList {
const segment_count = countSegments(string, delimiter);
const list = RocList.allocate(@alignOf(RocStr), segment_count, @sizeOf(RocStr));
if (list.bytes) |bytes| {
const strings = @ptrCast([*]RocStr, @alignCast(@alignOf(RocStr), bytes));
strSplitInPlace(strings, string, delimiter);
}
return list;
}
// For LLVM backend
pub fn strSplitInPlaceC(opt_array: ?[*]RocStr, string: RocStr, delimiter: RocStr) callconv(.C) void {
if (opt_array) |array| {
return @call(.{ .modifier = always_inline }, strSplitInPlace, .{ array, string, delimiter });