authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-23 11:16:53 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-23 11:16:53 -08:00
logaa4f14760e80efa703f12cff0eca6f3d23103ec7
treec2d52310f22950b92dfd8fb26db473a7dba74148
parenta605a3ea057212f0e1cd825d0e18f78ae8ef8a2b

opslice: handle pointer to array


1 files changed, 14 insertions(+), 1 deletions(-)

src/opslice.zig+14-1
......@@ -2,7 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44
5pub fn opslice(slice: anytype, index: usize) ?std.meta.Child(@TypeOf(slice)) {
5pub fn opslice(slice: anytype, index: usize) ?std.meta.Elem(@TypeOf(slice)) {
66 if (slice.len <= index) return null;
77 return slice[index];
88}
......@@ -19,3 +19,16 @@ test {
1919 try std.testing.expect(opslice(slice, 5) == 5);
2020 try std.testing.expect(opslice(slice, 6) == null);
2121}
22
23test {
24 const array = [_]u8{ 0, 1, 2, 3, 4, 5 };
25 const slice = &array;
26
27 try std.testing.expect(opslice(slice, 0) == 0);
28 try std.testing.expect(opslice(slice, 1) == 1);
29 try std.testing.expect(opslice(slice, 2) == 2);
30 try std.testing.expect(opslice(slice, 3) == 3);
31 try std.testing.expect(opslice(slice, 4) == 4);
32 try std.testing.expect(opslice(slice, 5) == 5);
33 try std.testing.expect(opslice(slice, 6) == null);
34}