mirror of
https://github.com/ziglang/zig.git
synced 2026-02-18 23:39:17 +00:00
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:
parent
3c151f0b1c
commit
8f7fc63847
@ -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());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user