mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
Drop RocStr when necessary
This commit is contained in:
parent
bc48f72760
commit
bf142c4c58
1 changed files with 26 additions and 0 deletions
|
@ -5,6 +5,7 @@ const expectEqual = testing.expectEqual;
|
|||
const expect = testing.expect;
|
||||
|
||||
extern fn malloc(size: usize) ?*u8;
|
||||
extern fn free([*]u8) void;
|
||||
|
||||
const RocStr = struct {
|
||||
str_bytes: ?[*]u8,
|
||||
|
@ -57,6 +58,14 @@ const RocStr = struct {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn drop(self: RocStr) void {
|
||||
if (!self.is_small_str()) {
|
||||
const str_bytes: [*]u8 = self.str_bytes orelse unreachable;
|
||||
|
||||
free(str_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eq(self: RocStr, other: RocStr) bool {
|
||||
const self_bytes_ptr: ?[*]const u8 = self.str_bytes;
|
||||
const other_bytes_ptr: ?[*]const u8 = other.str_bytes;
|
||||
|
@ -147,6 +156,9 @@ const RocStr = struct {
|
|||
|
||||
// TODO: fix those tests
|
||||
// expect(roc_str1.eq(roc_str2));
|
||||
|
||||
roc_str1.drop();
|
||||
roc_str2.drop();
|
||||
}
|
||||
|
||||
test "RocStr.eq: not equal different length" {
|
||||
|
@ -161,6 +173,9 @@ const RocStr = struct {
|
|||
var roc_str2 = RocStr.init(str2_ptr, str2_len);
|
||||
|
||||
expect(!roc_str1.eq(roc_str2));
|
||||
|
||||
roc_str1.drop();
|
||||
roc_str2.drop();
|
||||
}
|
||||
|
||||
test "RocStr.eq: not equal same length" {
|
||||
|
@ -176,6 +191,9 @@ const RocStr = struct {
|
|||
|
||||
// TODO: fix those tests
|
||||
// expect(!roc_str1.eq(roc_str2));
|
||||
|
||||
roc_str1.drop();
|
||||
roc_str2.drop();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -255,6 +273,14 @@ test "strSplitInPlace: no delimiter" {
|
|||
expectEqual(array.len, expected.len);
|
||||
// TODO: fix those tests
|
||||
//expect(array[0].eq(expected[0]));
|
||||
|
||||
for (array) |roc_str| {
|
||||
roc_str.drop();
|
||||
}
|
||||
|
||||
for (expected) |roc_str| {
|
||||
roc_str.drop();
|
||||
}
|
||||
}
|
||||
|
||||
test "strSplitInPlace: empty end" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue