-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrev_slice.zig
40 lines (34 loc) · 1022 Bytes
/
rev_slice.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const zignite = @import("../zignite.zig");
const expect = @import("std").testing.expect;
const ReverseIndex = @import("reverse_index.zig").ReverseIndex;
test "revSlice" {
{
const a = try zignite.revSlice(u8, "ABC").toBoundedArray(10);
try expect(a.get(0) == 'C');
try expect(a.get(1) == 'B');
try expect(a.get(2) == 'A');
try expect(a.len == 3);
}
{
const a = try zignite.revSlice(u8, "ABCDEF"[2..5]).toBoundedArray(10);
try expect(a.get(0) == 'E');
try expect(a.get(1) == 'D');
try expect(a.get(2) == 'C');
try expect(a.len == 3);
}
{
try expect(zignite.revSlice(u8, "").isEmpty());
}
}
pub fn RevSlice(comptime T: type) type {
return struct {
const List = []const T;
const I = ReverseIndex(List, T, len, get);
fn len(list: List) usize {
return list.len;
}
fn get(list: List, index: usize) T {
return list[index];
}
}.I;
}