fix RocStr.capacity() in zig

This commit is contained in:
Folkert 2022-07-04 13:39:51 +02:00
parent 5904934887
commit 4f965ec263
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -228,7 +228,11 @@ pub const RocStr = extern struct {
}
pub fn capacity(self: RocStr) usize {
return self.str_capacity ^ MASK;
if (self.isSmallStr()) {
return SMALL_STR_MAX_LENGTH;
} else {
return self.str_capacity;
}
}
// This does a small string check, but no bounds checking whatsoever!
@ -2350,3 +2354,19 @@ test "ReverseUtf8View: empty" {
try expect(false);
}
}
test "capacity: small string" {
const data_bytes = "foobar";
var data = RocStr.init(data_bytes, data_bytes.len);
defer data.deinit();
try expectEqual(data.capacity(), SMALL_STR_MAX_LENGTH);
}
test "capacity: big string" {
const data_bytes = "a string so large that it must be heap-allocated";
var data = RocStr.init(data_bytes, data_bytes.len);
defer data.deinit();
try expectEqual(data.capacity(), data_bytes.len);
}