Drop RocStr when necessary

This commit is contained in:
Richard Feldman 2020-11-22 22:41:23 -05:00
parent bc48f72760
commit bf142c4c58

View file

@ -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" {