Package.Hash.toSlice: consistent pointer

Makes the returned pointer always point inside `Hash.bytes` even when it
is length zero.
This commit is contained in:
Andrew Kelley 2025-06-06 11:13:36 -07:00
parent 3c151f0b1c
commit 8f7fc63847

View File

@ -66,11 +66,11 @@ pub const Hash = struct {
pub fn toSlice(ph: *const Hash) []const u8 {
var end: usize = ph.bytes.len;
while (true) {
if (end == 0) return &.{};
while (end > 0) {
end -= 1;
if (ph.bytes[end] != 0) return ph.bytes[0 .. end + 1];
}
return ph.bytes[0..0];
}
pub fn eql(a: *const Hash, b: *const Hash) bool {
@ -196,7 +196,7 @@ test Hash {
try std.testing.expectEqualStrings("nasm-2.16.1-3-vrr-ygAAoADH9XG3tOdvPNuHen_d-XeHndOG-nNXmved", result.toSlice());
}
test "EmptyHash" {
test "empty hash" {
const hash = Hash.fromSlice("");
try std.testing.expectEqualStrings("", hash.toSlice());
}