reverse.ha (549B)
1 // License: MPL-2.0 2 // (c) 2021 Drew DeVault <sir@cmpwn.com> 3 4 // Reverses a slice. 5 export fn reverse(b: []void, membsz: size) void = { 6 if (len(b) == 0) { 7 return; 8 }; 9 let a = b: *[*]u8; 10 for (let s = 0z, e = len(b) - 1; s < e) { 11 for (let i = 0z; i < membsz; i += 1z) { 12 let z = a[s * membsz + i]; 13 a[s * membsz + i] = a[e * membsz + i]; 14 a[e * membsz + i] = z; 15 }; 16 s += 1; 17 e -= 1; 18 }; 19 }; 20 21 @test fn reverse() void = { 22 let a: []int = [1, 2, 3, 4]; 23 reverse(a, size(int)); 24 assert(a[0] == 4 && a[1] == 3 && a[2] == 2 && a[3] == 1); 25 };